Linux 4.19.133
[linux/fpc-iii.git] / drivers / base / core.c
blob928fc1532a70695640ac404c756109076bc2fb61
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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.
9 */
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>
21 #include <linux/of.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>
30 #include "base.h"
31 #include "power/power.h"
33 #ifdef CONFIG_SYSFS_DEPRECATED
34 #ifdef CONFIG_SYSFS_DEPRECATED_V2
35 long sysfs_deprecated = 1;
36 #else
37 long sysfs_deprecated = 0;
38 #endif
39 static int __init sysfs_deprecated_setup(char *arg)
41 return kstrtol(arg, 10, &sysfs_deprecated);
43 early_param("sysfs.deprecated", sysfs_deprecated_setup);
44 #endif
46 /* Device links support. */
48 #ifdef CONFIG_SRCU
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);
87 return 0;
90 void device_links_read_unlock(int not_used)
92 up_read(&device_links_lock);
94 #endif /* !CONFIG_SRCU */
96 /**
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;
107 int ret;
109 if (dev == target)
110 return 1;
112 ret = device_for_each_child(dev, target, device_is_dependent);
113 if (ret)
114 return ret;
116 list_for_each_entry(link, &dev->links.consumers, s_node) {
117 if (link->consumer == target)
118 return 1;
120 ret = device_is_dependent(link->consumer, target);
121 if (ret)
122 break;
124 return ret;
127 static void device_link_init_status(struct device_link *link,
128 struct device *consumer,
129 struct device *supplier)
131 switch (supplier->links.status) {
132 case DL_DEV_PROBING:
133 switch (consumer->links.status) {
134 case DL_DEV_PROBING:
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
140 * supplier).
142 link->status = DL_STATE_CONSUMER_PROBE;
143 break;
144 default:
145 link->status = DL_STATE_DORMANT;
146 break;
148 break;
149 case DL_DEV_DRIVER_BOUND:
150 switch (consumer->links.status) {
151 case DL_DEV_PROBING:
152 link->status = DL_STATE_CONSUMER_PROBE;
153 break;
154 case DL_DEV_DRIVER_BOUND:
155 link->status = DL_STATE_ACTIVE;
156 break;
157 default:
158 link->status = DL_STATE_AVAILABLE;
159 break;
161 break;
162 case DL_DEV_UNBINDING:
163 link->status = DL_STATE_SUPPLIER_UNBIND;
164 break;
165 default:
166 link->status = DL_STATE_DORMANT;
167 break;
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);
189 return 0;
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)
203 int idx;
205 idx = device_links_read_lock();
206 device_pm_lock();
207 device_reorder_to_tail(dev, NULL);
208 device_pm_unlock();
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
231 * ignored.
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)))
285 return NULL;
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);
290 return NULL;
294 if (!(flags & DL_FLAG_STATELESS))
295 flags |= DL_FLAG_MANAGED;
297 device_links_write_lock();
298 device_pm_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)) {
307 link = NULL;
308 goto out;
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)
321 continue;
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);
335 goto out;
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);
357 goto out;
360 link = kzalloc(sizeof(*link), GFP_KERNEL);
361 if (!link)
362 goto out;
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);
379 link->flags = flags;
380 kref_init(&link->kref);
382 /* Determine the initial link state. */
383 if (flags & DL_FLAG_STATELESS)
384 link->status = DL_STATE_NONE;
385 else
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));
410 out:
411 device_pm_unlock();
412 device_links_write_unlock();
414 if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
415 pm_runtime_put(supplier);
417 return link;
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);
428 kfree(link);
431 #ifdef CONFIG_SRCU
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);
472 else
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();
488 device_pm_lock();
489 device_link_put_kref(link);
490 device_pm_unlock();
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
501 * PM.
503 void device_link_remove(void *consumer, struct device *supplier)
505 struct device_link *link;
507 if (WARN_ON(consumer == supplier))
508 return;
510 device_links_write_lock();
511 device_pm_lock();
513 list_for_each_entry(link, &supplier->links.consumers, s_node) {
514 if (link->consumer == consumer) {
515 device_link_put_kref(link);
516 break;
520 device_pm_unlock();
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;
553 int ret = 0;
555 device_links_write_lock();
557 list_for_each_entry(link, &dev->links.suppliers, c_node) {
558 if (!(link->flags & DL_FLAG_MANAGED))
559 continue;
561 if (link->status != DL_STATE_AVAILABLE) {
562 device_links_missing_supplier(dev);
563 ret = -EPROBE_DEFER;
564 break;
566 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
568 dev->links.status = DL_DEV_PROBING;
570 device_links_write_unlock();
571 return ret;
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))
593 continue;
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)
603 continue;
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))
614 continue;
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))
650 continue;
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
668 * appropriate.
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))
680 continue;
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
705 * appropriate.
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))
717 continue;
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;
756 bool ret = false;
758 device_links_write_lock();
760 list_for_each_entry(link, &dev->links.consumers, s_node) {
761 if (!(link->flags & DL_FLAG_MANAGED))
762 continue;
764 if (link->status == DL_STATE_CONSUMER_PROBE
765 || link->status == DL_STATE_ACTIVE) {
766 ret = true;
767 break;
769 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
772 dev->links.status = DL_DEV_UNBINDING;
774 device_links_write_unlock();
775 return ret;
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
784 * and start over.
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;
797 start:
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))
804 continue;
806 status = link->status;
807 if (status == DL_STATE_CONSUMER_PROBE) {
808 device_links_write_unlock();
810 wait_for_device_probe();
811 goto start;
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,
822 consumer->parent);
823 put_device(consumer);
824 goto start;
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))
882 return 0;
884 /* Avoid busy looping (5 ms of sleep should do). */
885 msleep(5);
886 return restart_syscall();
889 #ifdef CONFIG_BLOCK
890 static inline int device_is_not_partition(struct device *dev)
892 return !(dev->type == &part_type);
894 #else
895 static inline int device_is_not_partition(struct device *dev)
897 return 1;
899 #endif
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,
928 char *buf)
930 struct device_attribute *dev_attr = to_dev_attr(attr);
931 struct device *dev = kobj_to_dev(kobj);
932 ssize_t ret = -EIO;
934 if (dev_attr->show)
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",
938 dev_attr->show);
940 return ret;
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);
948 ssize_t ret = -EIO;
950 if (dev_attr->store)
951 ret = dev_attr->store(dev, dev_attr, buf, count);
952 return ret;
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);
967 char *end;
968 unsigned long new = simple_strtoul(buf, &end, 0);
969 if (end == buf)
970 return -EINVAL;
971 *(unsigned long *)(ea->var) = new;
972 /* Always return full write size even if we didn't consume all */
973 return size;
975 EXPORT_SYMBOL_GPL(device_store_ulong);
977 ssize_t device_show_ulong(struct device *dev,
978 struct device_attribute *attr,
979 char *buf)
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);
991 char *end;
992 long new = simple_strtol(buf, &end, 0);
993 if (end == buf || new > INT_MAX || new < INT_MIN)
994 return -EINVAL;
995 *(int *)(ea->var) = new;
996 /* Always return full write size even if we didn't consume all */
997 return size;
999 EXPORT_SYMBOL_GPL(device_store_int);
1001 ssize_t device_show_int(struct device *dev,
1002 struct device_attribute *attr,
1003 char *buf)
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)
1017 return -EINVAL;
1019 return size;
1021 EXPORT_SYMBOL_GPL(device_store_bool);
1023 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
1024 char *buf)
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);
1056 if (dev->release)
1057 dev->release(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);
1062 else
1063 WARN(1, KERN_ERR "Device '%s' does not have a release() "
1064 "function, it is broken and must be fixed.\n",
1065 dev_name(dev));
1066 kfree(p);
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);
1077 return ns;
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);
1102 if (dev->bus)
1103 return 1;
1104 if (dev->class)
1105 return 1;
1107 return 0;
1110 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
1112 struct device *dev = kobj_to_dev(kobj);
1114 if (dev->bus)
1115 return dev->bus->name;
1116 if (dev->class)
1117 return dev->class->name;
1118 return NULL;
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);
1125 int retval = 0;
1127 /* add device node properties if present */
1128 if (MAJOR(dev->devt)) {
1129 const char *tmp;
1130 const char *name;
1131 umode_t mode = 0;
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);
1138 if (name) {
1139 add_uevent_var(env, "DEVNAME=%s", name);
1140 if (mode)
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));
1146 kfree(tmp);
1150 if (dev->type && dev->type->name)
1151 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
1153 if (dev->driver)
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);
1162 if (retval)
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);
1170 if (retval)
1171 pr_debug("device: '%s': %s: class uevent() "
1172 "returned %d\n", dev_name(dev),
1173 __func__, retval);
1176 /* have the device type specific function add its stuff */
1177 if (dev->type && dev->type->uevent) {
1178 retval = dev->type->uevent(dev, env);
1179 if (retval)
1180 pr_debug("device: '%s': %s: dev_type uevent() "
1181 "returned %d\n", dev_name(dev),
1182 __func__, retval);
1185 return retval;
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,
1195 char *buf)
1197 struct kobject *top_kobj;
1198 struct kset *kset;
1199 struct kobj_uevent_env *env = NULL;
1200 int i;
1201 size_t count = 0;
1202 int retval;
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)
1209 goto out;
1211 kset = top_kobj->kset;
1212 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
1213 goto out;
1215 /* respect filter */
1216 if (kset->uevent_ops && kset->uevent_ops->filter)
1217 if (!kset->uevent_ops->filter(kset, &dev->kobj))
1218 goto out;
1220 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
1221 if (!env)
1222 return -ENOMEM;
1224 /* let the kset specific function add its keys */
1225 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
1226 if (retval)
1227 goto out;
1229 /* copy keys to file */
1230 for (i = 0; i < env->envp_idx; i++)
1231 count += sprintf(&buf[count], "%s\n", env->envp[i]);
1232 out:
1233 kfree(env);
1234 return count;
1237 static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
1238 const char *buf, size_t count)
1240 int rc;
1242 rc = kobject_synth_uevent(&dev->kobj, buf, count);
1244 if (rc) {
1245 dev_err(dev, "uevent: failed to send synthetic uevent\n");
1246 return rc;
1249 return count;
1251 static DEVICE_ATTR_RW(uevent);
1253 static ssize_t online_show(struct device *dev, struct device_attribute *attr,
1254 char *buf)
1256 bool val;
1258 device_lock(dev);
1259 val = !dev->offline;
1260 device_unlock(dev);
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)
1267 bool val;
1268 int ret;
1270 ret = strtobool(buf, &val);
1271 if (ret < 0)
1272 return ret;
1274 ret = lock_device_hotplug_sysfs();
1275 if (ret)
1276 return ret;
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;
1338 int error;
1340 devres = devres_alloc(devm_attr_group_remove,
1341 sizeof(*devres), GFP_KERNEL);
1342 if (!devres)
1343 return -ENOMEM;
1345 error = sysfs_create_group(&dev->kobj, grp);
1346 if (error) {
1347 devres_free(devres);
1348 return error;
1351 devres->group = grp;
1352 devres_add(dev, devres);
1353 return 0;
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;
1391 int error;
1393 devres = devres_alloc(devm_attr_groups_remove,
1394 sizeof(*devres), GFP_KERNEL);
1395 if (!devres)
1396 return -ENOMEM;
1398 error = sysfs_create_groups(&dev->kobj, groups);
1399 if (error) {
1400 devres_free(devres);
1401 return error;
1404 devres->groups = groups;
1405 devres_add(dev, devres);
1406 return 0;
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;
1431 int error;
1433 if (class) {
1434 error = device_add_groups(dev, class->dev_groups);
1435 if (error)
1436 return error;
1439 if (type) {
1440 error = device_add_groups(dev, type->groups);
1441 if (error)
1442 goto err_remove_class_groups;
1445 error = device_add_groups(dev, dev->groups);
1446 if (error)
1447 goto err_remove_type_groups;
1449 if (device_supports_offline(dev) && !dev->offline_disabled) {
1450 error = device_create_file(dev, &dev_attr_online);
1451 if (error)
1452 goto err_remove_dev_groups;
1455 return 0;
1457 err_remove_dev_groups:
1458 device_remove_groups(dev, dev->groups);
1459 err_remove_type_groups:
1460 if (type)
1461 device_remove_groups(dev, type->groups);
1462 err_remove_class_groups:
1463 if (class)
1464 device_remove_groups(dev, class->dev_groups);
1466 return error;
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);
1477 if (type)
1478 device_remove_groups(dev, type->groups);
1480 if (class)
1481 device_remove_groups(dev, class->dev_groups);
1484 static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
1485 char *buf)
1487 return print_dev_t(buf, dev->devt);
1489 static DEVICE_ATTR_RO(dev);
1491 /* /sys/devices/ */
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)
1501 if (!devices_kset)
1502 return;
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)
1517 if (!devices_kset)
1518 return;
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)
1532 if (!devices_kset)
1533 return;
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.
1542 * @dev: device.
1543 * @attr: device attribute descriptor.
1545 int device_create_file(struct device *dev,
1546 const struct device_attribute *attr)
1548 int error = 0;
1550 if (dev) {
1551 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
1552 "Attribute %s: write permission without 'store'\n",
1553 attr->attr.name);
1554 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
1555 "Attribute %s: read permission without 'show'\n",
1556 attr->attr.name);
1557 error = sysfs_create_file(&dev->kobj, &attr->attr);
1560 return error;
1562 EXPORT_SYMBOL_GPL(device_create_file);
1565 * device_remove_file - remove sysfs attribute file.
1566 * @dev: device.
1567 * @attr: device attribute descriptor.
1569 void device_remove_file(struct device *dev,
1570 const struct device_attribute *attr)
1572 if (dev)
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.
1579 * @dev: device.
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)
1587 if (dev)
1588 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
1589 else
1590 return false;
1592 EXPORT_SYMBOL_GPL(device_remove_file_self);
1595 * device_create_bin_file - create sysfs binary attribute file for device.
1596 * @dev: 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;
1603 if (dev)
1604 error = sysfs_create_bin_file(&dev->kobj, attr);
1605 return error;
1607 EXPORT_SYMBOL_GPL(device_create_bin_file);
1610 * device_remove_bin_file - remove sysfs binary attribute file
1611 * @dev: device.
1612 * @attr: device binary attribute descriptor.
1614 void device_remove_bin_file(struct device *dev,
1615 const struct bin_attribute *attr)
1617 if (dev)
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;
1627 get_device(dev);
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;
1635 put_device(dev);
1639 * device_initialize - init device structure.
1640 * @dev: device.
1642 * This prepares the device for use by other layers by initializing
1643 * its fields.
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
1648 * function.
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
1653 * @dev.
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);
1671 #endif
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;
1682 if (!virtual_dir)
1683 virtual_dir = kobject_create_and_add("virtual",
1684 &devices_kset->kobj);
1686 return virtual_dir;
1689 struct class_dir {
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);
1699 kfree(dir);
1702 static const
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;
1719 int retval;
1721 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1722 if (!dir)
1723 return ERR_PTR(-ENOMEM);
1725 dir->class = class;
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);
1731 if (retval < 0) {
1732 kobject_put(&dir->kobj);
1733 return ERR_PTR(retval);
1735 return &dir->kobj;
1738 static DEFINE_MUTEX(gdp_mutex);
1740 static struct kobject *get_device_parent(struct device *dev,
1741 struct device *parent)
1743 if (dev->class) {
1744 struct kobject *kobj = NULL;
1745 struct kobject *parent_kobj;
1746 struct kobject *k;
1748 #ifdef CONFIG_BLOCK
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;
1755 #endif
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.
1762 if (parent == NULL)
1763 parent_kobj = virtual_device_parent(dev);
1764 else if (parent->class && !dev->class->ns_type)
1765 return &parent->kobj;
1766 else
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);
1776 break;
1778 spin_unlock(&dev->class->p->glue_dirs.list_lock);
1779 if (kobj) {
1780 mutex_unlock(&gdp_mutex);
1781 return kobj;
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);
1788 return k;
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;
1795 if (parent)
1796 return &parent->kobj;
1797 return NULL;
1800 static inline bool live_in_glue_dir(struct kobject *kobj,
1801 struct device *dev)
1803 if (!kobj || !dev->class ||
1804 kobj->kset != &dev->class->p->glue_dirs)
1805 return false;
1806 return true;
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
1817 * global lock
1819 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
1821 unsigned int ref;
1823 /* see if we live in a "glue" directory */
1824 if (!live_in_glue_dir(glue_dir, dev))
1825 return;
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.
1832 * CPU1: CPU2:
1834 * device_add()
1835 * get_device_parent()
1836 * class_dir_create_and_add()
1837 * kobject_add_internal()
1838 * create_dir() // create glue_dir
1840 * device_add()
1841 * get_device_parent()
1842 * kobject_get() // get glue_dir
1844 * device_del()
1845 * cleanup_glue_dir()
1846 * kobject_del(glue_dir)
1848 * kobject_add()
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
1857 * // sd is freed
1858 * kernfs_new_node(sd)
1859 * kernfs_get(glue_dir)
1860 * kernfs_add_one()
1861 * kernfs_put()
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);
1886 int error;
1888 if (of_node) {
1889 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
1890 if (error)
1891 dev_warn(dev, "Error %d creating of_node link\n",error);
1892 /* An error here doesn't warrant bringing down the device */
1895 if (!dev->class)
1896 return 0;
1898 error = sysfs_create_link(&dev->kobj,
1899 &dev->class->p->subsys.kobj,
1900 "subsystem");
1901 if (error)
1902 goto out_devnode;
1904 if (dev->parent && device_is_not_partition(dev)) {
1905 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
1906 "device");
1907 if (error)
1908 goto out_subsys;
1911 #ifdef CONFIG_BLOCK
1912 /* /sys/block has directories and does not need symlinks */
1913 if (sysfs_deprecated && dev->class == &block_class)
1914 return 0;
1915 #endif
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));
1920 if (error)
1921 goto out_device;
1923 return 0;
1925 out_device:
1926 sysfs_remove_link(&dev->kobj, "device");
1928 out_subsys:
1929 sysfs_remove_link(&dev->kobj, "subsystem");
1930 out_devnode:
1931 sysfs_remove_link(&dev->kobj, "of_node");
1932 return error;
1935 static void device_remove_class_symlinks(struct device *dev)
1937 if (dev_of_node(dev))
1938 sysfs_remove_link(&dev->kobj, "of_node");
1940 if (!dev->class)
1941 return;
1943 if (dev->parent && device_is_not_partition(dev))
1944 sysfs_remove_link(&dev->kobj, "device");
1945 sysfs_remove_link(&dev->kobj, "subsystem");
1946 #ifdef CONFIG_BLOCK
1947 if (sysfs_deprecated && dev->class == &block_class)
1948 return;
1949 #endif
1950 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
1954 * dev_set_name - set a device name
1955 * @dev: device
1956 * @fmt: format string for the device's name
1958 int dev_set_name(struct device *dev, const char *fmt, ...)
1960 va_list vargs;
1961 int err;
1963 va_start(vargs, fmt);
1964 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
1965 va_end(vargs);
1966 return err;
1968 EXPORT_SYMBOL_GPL(dev_set_name);
1971 * device_to_dev_kobj - select a /sys/dev/ directory for the device
1972 * @dev: 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
1979 * the link.
1981 static struct kobject *device_to_dev_kobj(struct device *dev)
1983 struct kobject *kobj;
1985 if (dev->class)
1986 kobj = dev->class->dev_kobj;
1987 else
1988 kobj = sysfs_dev_char_kobj;
1990 return kobj;
1993 static int device_create_sys_dev_entry(struct device *dev)
1995 struct kobject *kobj = device_to_dev_kobj(dev);
1996 int error = 0;
1997 char devt_str[15];
1999 if (kobj) {
2000 format_dev_t(devt_str, dev->devt);
2001 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
2004 return error;
2007 static void device_remove_sys_dev_entry(struct device *dev)
2009 struct kobject *kobj = device_to_dev_kobj(dev);
2010 char devt_str[15];
2012 if (kobj) {
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);
2021 if (!dev->p)
2022 return -ENOMEM;
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);
2027 return 0;
2031 * device_add - add device to device hierarchy.
2032 * @dev: device.
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);
2061 if (!dev)
2062 goto done;
2064 if (!dev->p) {
2065 error = device_private_init(dev);
2066 if (error)
2067 goto done;
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)) {
2085 error = -EINVAL;
2086 goto name_error;
2089 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2091 parent = get_device(dev->parent);
2092 kobj = get_device_parent(dev, parent);
2093 if (IS_ERR(kobj)) {
2094 error = PTR_ERR(kobj);
2095 goto parent_error;
2097 if (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);
2107 if (error) {
2108 glue_dir = get_glue_dir(dev);
2109 goto Error;
2112 /* notify platform of device entry */
2113 if (platform_notify)
2114 platform_notify(dev);
2116 error = device_create_file(dev, &dev_attr_uevent);
2117 if (error)
2118 goto attrError;
2120 error = device_add_class_symlinks(dev);
2121 if (error)
2122 goto SymlinkError;
2123 error = device_add_attrs(dev);
2124 if (error)
2125 goto AttrsError;
2126 error = bus_add_device(dev);
2127 if (error)
2128 goto BusError;
2129 error = dpm_sysfs_add(dev);
2130 if (error)
2131 goto DPMError;
2132 device_pm_add(dev);
2134 if (MAJOR(dev->devt)) {
2135 error = device_create_file(dev, &dev_attr_dev);
2136 if (error)
2137 goto DevAttrError;
2139 error = device_create_sys_dev_entry(dev);
2140 if (error)
2141 goto SysEntryError;
2143 devtmpfs_create_node(dev);
2146 /* Notify clients of device addition. This call must come
2147 * after dpm_sysfs_add() and before kobject_uevent().
2149 if (dev->bus)
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);
2155 if (parent)
2156 klist_add_tail(&dev->p->knode_parent,
2157 &parent->p->klist_children);
2159 if (dev->class) {
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);
2172 done:
2173 put_device(dev);
2174 return error;
2175 SysEntryError:
2176 if (MAJOR(dev->devt))
2177 device_remove_file(dev, &dev_attr_dev);
2178 DevAttrError:
2179 device_pm_remove(dev);
2180 dpm_sysfs_remove(dev);
2181 DPMError:
2182 bus_remove_device(dev);
2183 BusError:
2184 device_remove_attrs(dev);
2185 AttrsError:
2186 device_remove_class_symlinks(dev);
2187 SymlinkError:
2188 device_remove_file(dev, &dev_attr_uevent);
2189 attrError:
2190 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2191 glue_dir = get_glue_dir(dev);
2192 kobject_del(&dev->kobj);
2193 Error:
2194 cleanup_glue_dir(dev, glue_dir);
2195 parent_error:
2196 put_device(parent);
2197 name_error:
2198 kfree(dev->p);
2199 dev->p = NULL;
2200 goto done;
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()
2216 * and device_add().
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.
2231 * @dev: 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(); */
2250 if (dev)
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);
2266 if (dev->p->dead)
2267 return false;
2268 dev->p->dead = true;
2269 return true;
2271 EXPORT_SYMBOL_GPL(kill_device);
2274 * device_del - delete device from system.
2275 * @dev: device.
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;
2292 device_lock(dev);
2293 kill_device(dev);
2294 device_unlock(dev);
2296 /* Notify clients of device removal. This call must come
2297 * before dpm_sysfs_remove().
2299 if (dev->bus)
2300 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2301 BUS_NOTIFY_DEL_DEVICE, dev);
2303 dpm_sysfs_remove(dev);
2304 if (parent)
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);
2311 if (dev->class) {
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);
2337 if (dev->bus)
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);
2344 put_device(parent);
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__);
2362 device_del(dev);
2363 put_device(dev);
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;
2373 if (n) {
2374 p = to_device_private_parent(n);
2375 dev = p->device;
2377 return dev;
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;
2386 if (n) {
2387 p = to_device_private_parent(n);
2388 dev = p->device;
2390 return dev;
2394 * device_get_devnode - path of device node file
2395 * @dev: device
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,
2408 const char **tmp)
2410 char *s;
2412 *tmp = NULL;
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);
2417 if (*tmp)
2418 return *tmp;
2420 /* the class may provide a specific name */
2421 if (dev->class && dev->class->devnode)
2422 *tmp = dev->class->devnode(dev, mode);
2423 if (*tmp)
2424 return *tmp;
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);
2432 if (!s)
2433 return NULL;
2434 strreplace(s, '!', '/');
2435 return *tmp = 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,
2445 * passing it @data.
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;
2455 int error = 0;
2457 if (!parent->p)
2458 return 0;
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);
2464 return error;
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,
2475 * passing it @data.
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;
2485 int error = 0;
2487 if (!parent->p)
2488 return 0;
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);
2494 return error;
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;
2521 if (!parent)
2522 return NULL;
2524 klist_iter_init(&parent->p->klist_children, &i);
2525 while ((child = next_device(&i)))
2526 if (match(child, data) && get_device(child))
2527 break;
2528 klist_iter_exit(&i);
2529 return child;
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);
2536 if (!devices_kset)
2537 return -ENOMEM;
2538 dev_kobj = kobject_create_and_add("dev", NULL);
2539 if (!dev_kobj)
2540 goto dev_kobj_err;
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)
2546 goto char_kobj_err;
2548 return 0;
2550 char_kobj_err:
2551 kobject_put(sysfs_dev_block_kobj);
2552 block_kobj_err:
2553 kobject_put(dev_kobj);
2554 dev_kobj_err:
2555 kset_unregister(devices_kset);
2556 return -ENOMEM;
2559 static int device_check_offline(struct device *dev, void *not_used)
2561 int ret;
2563 ret = device_for_each_child(dev, NULL, device_check_offline);
2564 if (ret)
2565 return ret;
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
2577 * is executed.
2579 * Call under device_hotplug_lock.
2581 int device_offline(struct device *dev)
2583 int ret;
2585 if (dev->offline_disabled)
2586 return -EPERM;
2588 ret = device_for_each_child(dev, NULL, device_check_offline);
2589 if (ret)
2590 return ret;
2592 device_lock(dev);
2593 if (device_supports_offline(dev)) {
2594 if (dev->offline) {
2595 ret = 1;
2596 } else {
2597 ret = dev->bus->offline(dev);
2598 if (!ret) {
2599 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2600 dev->offline = true;
2604 device_unlock(dev);
2606 return ret;
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)
2621 int ret = 0;
2623 device_lock(dev);
2624 if (device_supports_offline(dev)) {
2625 if (dev->offline) {
2626 ret = dev->bus->online(dev);
2627 if (!ret) {
2628 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2629 dev->offline = false;
2631 } else {
2632 ret = 1;
2635 device_unlock(dev);
2637 return ret;
2640 struct root_device {
2641 struct device dev;
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
2671 * in sysfs.
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;
2680 int err = -ENOMEM;
2682 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
2683 if (!root)
2684 return ERR_PTR(err);
2686 err = dev_set_name(&root->dev, "%s", name);
2687 if (err) {
2688 kfree(root);
2689 return ERR_PTR(err);
2692 root->dev.release = root_device_release;
2694 err = device_register(&root->dev);
2695 if (err) {
2696 put_device(&root->dev);
2697 return ERR_PTR(err);
2700 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
2701 if (owner) {
2702 struct module_kobject *mk = &owner->mkobj;
2704 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
2705 if (err) {
2706 device_unregister(&root->dev);
2707 return ERR_PTR(err);
2709 root->owner = owner;
2711 #endif
2713 return &root->dev;
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);
2728 if (root->owner)
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__);
2739 kfree(dev);
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))
2752 goto error;
2754 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2755 if (!dev) {
2756 retval = -ENOMEM;
2757 goto error;
2760 device_initialize(dev);
2761 dev->devt = devt;
2762 dev->class = class;
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);
2769 if (retval)
2770 goto error;
2772 retval = device_add(dev);
2773 if (retval)
2774 goto error;
2776 return dev;
2778 error:
2779 put_device(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
2801 * pointer.
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,
2810 va_list args)
2812 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
2813 fmt, args);
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
2834 * pointer.
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, ...)
2844 va_list vargs;
2845 struct device *dev;
2847 va_start(vargs, fmt);
2848 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
2849 va_end(vargs);
2850 return dev;
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
2874 * pointer.
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,
2883 void *drvdata,
2884 const struct attribute_group **groups,
2885 const char *fmt, ...)
2887 va_list vargs;
2888 struct device *dev;
2890 va_start(vargs, fmt);
2891 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
2892 fmt, vargs);
2893 va_end(vargs);
2894 return dev;
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)
2915 struct device *dev;
2917 dev = class_find_device(class, NULL, &devt, __match_devt);
2918 if (dev) {
2919 put_device(dev);
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
2937 * some insight:
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
2956 * reasons.
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;
2968 int error;
2970 dev = get_device(dev);
2971 if (!dev)
2972 return -EINVAL;
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) {
2978 error = -ENOMEM;
2979 goto out;
2982 if (dev->class) {
2983 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
2984 kobj, old_device_name,
2985 new_name, kobject_namespace(kobj));
2986 if (error)
2987 goto out;
2990 error = kobject_rename(kobj, new_name);
2991 if (error)
2992 goto out;
2994 out:
2995 put_device(dev);
2997 kfree(old_device_name);
2999 return error;
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)
3007 int error = 0;
3009 if (old_parent)
3010 sysfs_remove_link(&dev->kobj, "device");
3011 if (new_parent)
3012 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
3013 "device");
3014 return error;
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)
3026 int error;
3027 struct device *old_parent;
3028 struct kobject *new_parent_kobj;
3030 dev = get_device(dev);
3031 if (!dev)
3032 return -EINVAL;
3034 device_pm_lock();
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);
3040 goto out;
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);
3046 if (error) {
3047 cleanup_glue_dir(dev, new_parent_kobj);
3048 put_device(new_parent);
3049 goto out;
3051 old_parent = dev->parent;
3052 dev->parent = new_parent;
3053 if (old_parent)
3054 klist_remove(&dev->p->knode_parent);
3055 if (new_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));
3061 if (dev->class) {
3062 error = device_move_class_links(dev, old_parent, new_parent);
3063 if (error) {
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)) {
3067 if (new_parent)
3068 klist_remove(&dev->p->knode_parent);
3069 dev->parent = old_parent;
3070 if (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);
3078 goto out;
3081 switch (dpm_order) {
3082 case DPM_ORDER_NONE:
3083 break;
3084 case DPM_ORDER_DEV_AFTER_PARENT:
3085 device_pm_move_after(dev, new_parent);
3086 devices_kset_move_after(dev, new_parent);
3087 break;
3088 case DPM_ORDER_PARENT_BEFORE_DEV:
3089 device_pm_move_before(new_parent, dev);
3090 devices_kset_move_before(new_parent, dev);
3091 break;
3092 case DPM_ORDER_DEV_LAST:
3093 device_pm_move_last(dev);
3094 devices_kset_move_last(dev);
3095 break;
3098 put_device(old_parent);
3099 out:
3100 device_pm_unlock();
3101 put_device(dev);
3102 return error;
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();
3116 cpufreq_suspend();
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,
3126 kobj.entry);
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);
3134 get_device(dev);
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 */
3143 if (parent)
3144 device_lock(parent);
3145 device_lock(dev);
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) {
3152 if (initcall_debug)
3153 dev_info(dev, "shutdown_pre\n");
3154 dev->class->shutdown_pre(dev);
3156 if (dev->bus && dev->bus->shutdown) {
3157 if (initcall_debug)
3158 dev_info(dev, "shutdown\n");
3159 dev->bus->shutdown(dev);
3160 } else if (dev->driver && dev->driver->shutdown) {
3161 if (initcall_debug)
3162 dev_info(dev, "shutdown\n");
3163 dev->driver->shutdown(dev);
3166 device_unlock(dev);
3167 if (parent)
3168 device_unlock(parent);
3170 put_device(dev);
3171 put_device(parent);
3173 spin_lock(&devices_kset->list_lock);
3175 spin_unlock(&devices_kset->list_lock);
3179 * Device logging functions
3182 #ifdef CONFIG_PRINTK
3183 static int
3184 create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
3186 const char *subsys;
3187 size_t pos = 0;
3189 if (dev->class)
3190 subsys = dev->class->name;
3191 else if (dev->bus)
3192 subsys = dev->bus->name;
3193 else
3194 return 0;
3196 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
3197 if (pos >= hdrlen)
3198 goto overflow;
3201 * Add device identifier DEVICE=:
3202 * b12:8 block dev_t
3203 * c127:3 char dev_t
3204 * n8 netdev ifindex
3205 * +sound:card0 subsystem:devname
3207 if (MAJOR(dev->devt)) {
3208 char c;
3210 if (strcmp(subsys, "block") == 0)
3211 c = 'b';
3212 else
3213 c = 'c';
3214 pos++;
3215 pos += snprintf(hdr + pos, hdrlen - pos,
3216 "DEVICE=%c%u:%u",
3217 c, MAJOR(dev->devt), MINOR(dev->devt));
3218 } else if (strcmp(subsys, "net") == 0) {
3219 struct net_device *net = to_net_dev(dev);
3221 pos++;
3222 pos += snprintf(hdr + pos, hdrlen - pos,
3223 "DEVICE=n%u", net->ifindex);
3224 } else {
3225 pos++;
3226 pos += snprintf(hdr + pos, hdrlen - pos,
3227 "DEVICE=+%s:%s", subsys, dev_name(dev));
3230 if (pos >= hdrlen)
3231 goto overflow;
3233 return pos;
3235 overflow:
3236 dev_WARN(dev, "device/subsystem name too long");
3237 return 0;
3240 int dev_vprintk_emit(int level, const struct device *dev,
3241 const char *fmt, va_list args)
3243 char hdr[128];
3244 size_t hdrlen;
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, ...)
3254 va_list args;
3255 int r;
3257 va_start(args, fmt);
3259 r = dev_vprintk_emit(level, dev, fmt, args);
3261 va_end(args);
3263 return r;
3265 EXPORT_SYMBOL(dev_printk_emit);
3267 static void __dev_printk(const char *level, const struct device *dev,
3268 struct va_format *vaf)
3270 if (dev)
3271 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
3272 dev_driver_string(dev), dev_name(dev), vaf);
3273 else
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;
3281 va_list args;
3283 va_start(args, fmt);
3285 vaf.fmt = fmt;
3286 vaf.va = &args;
3288 __dev_printk(level, dev, &vaf);
3290 va_end(args);
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; \
3298 va_list args; \
3300 va_start(args, fmt); \
3302 vaf.fmt = fmt; \
3303 vaf.va = &args; \
3305 __dev_printk(kern_level, dev, &vaf); \
3307 va_end(args); \
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);
3319 #endif
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)
3336 if (fwnode) {
3337 struct fwnode_handle *fn = dev->fwnode;
3339 if (fwnode_is_primary(fn))
3340 fn = fn->secondary;
3342 if (fn) {
3343 WARN_ON(fwnode->secondary);
3344 fwnode->secondary = fn;
3346 dev->fwnode = fwnode;
3347 } else {
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
3361 * @fwnode.
3363 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3365 if (fwnode)
3366 fwnode->secondary = ERR_PTR(-ENODEV);
3368 if (fwnode_is_primary(dev->fwnode))
3369 dev->fwnode->secondary = fwnode;
3370 else
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);