Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * drivers/base/core.c - core driver model code (device registration, etc)
4 *
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 */
10
11#include <linux/acpi.h>
12#include <linux/cpufreq.h>
13#include <linux/device.h>
14#include <linux/err.h>
15#include <linux/fwnode.h>
16#include <linux/init.h>
17#include <linux/kstrtox.h>
18#include <linux/module.h>
19#include <linux/slab.h>
20#include <linux/kdev_t.h>
21#include <linux/notifier.h>
22#include <linux/of.h>
23#include <linux/of_device.h>
24#include <linux/blkdev.h>
25#include <linux/mutex.h>
26#include <linux/pm_runtime.h>
27#include <linux/netdevice.h>
28#include <linux/sched/signal.h>
29#include <linux/sched/mm.h>
30#include <linux/string_helpers.h>
31#include <linux/swiotlb.h>
32#include <linux/sysfs.h>
33#include <linux/dma-map-ops.h> /* for dma_default_coherent */
34
35#include "base.h"
36#include "physical_location.h"
37#include "power/power.h"
38
39/* Device links support. */
40static LIST_HEAD(deferred_sync);
41static unsigned int defer_sync_state_count = 1;
42static DEFINE_MUTEX(fwnode_link_lock);
43static bool fw_devlink_is_permissive(void);
44static void __fw_devlink_link_to_consumers(struct device *dev);
45static bool fw_devlink_drv_reg_done;
46static bool fw_devlink_best_effort;
47
48/**
49 * __fwnode_link_add - Create a link between two fwnode_handles.
50 * @con: Consumer end of the link.
51 * @sup: Supplier end of the link.
52 *
53 * Create a fwnode link between fwnode handles @con and @sup. The fwnode link
54 * represents the detail that the firmware lists @sup fwnode as supplying a
55 * resource to @con.
56 *
57 * The driver core will use the fwnode link to create a device link between the
58 * two device objects corresponding to @con and @sup when they are created. The
59 * driver core will automatically delete the fwnode link between @con and @sup
60 * after doing that.
61 *
62 * Attempts to create duplicate links between the same pair of fwnode handles
63 * are ignored and there is no reference counting.
64 */
65static int __fwnode_link_add(struct fwnode_handle *con,
66 struct fwnode_handle *sup, u8 flags)
67{
68 struct fwnode_link *link;
69
70 list_for_each_entry(link, &sup->consumers, s_hook)
71 if (link->consumer == con) {
72 link->flags |= flags;
73 return 0;
74 }
75
76 link = kzalloc(sizeof(*link), GFP_KERNEL);
77 if (!link)
78 return -ENOMEM;
79
80 link->supplier = sup;
81 INIT_LIST_HEAD(&link->s_hook);
82 link->consumer = con;
83 INIT_LIST_HEAD(&link->c_hook);
84 link->flags = flags;
85
86 list_add(&link->s_hook, &sup->consumers);
87 list_add(&link->c_hook, &con->suppliers);
88 pr_debug("%pfwf Linked as a fwnode consumer to %pfwf\n",
89 con, sup);
90
91 return 0;
92}
93
94int fwnode_link_add(struct fwnode_handle *con, struct fwnode_handle *sup)
95{
96 int ret;
97
98 mutex_lock(&fwnode_link_lock);
99 ret = __fwnode_link_add(con, sup, 0);
100 mutex_unlock(&fwnode_link_lock);
101 return ret;
102}
103
104/**
105 * __fwnode_link_del - Delete a link between two fwnode_handles.
106 * @link: the fwnode_link to be deleted
107 *
108 * The fwnode_link_lock needs to be held when this function is called.
109 */
110static void __fwnode_link_del(struct fwnode_link *link)
111{
112 pr_debug("%pfwf Dropping the fwnode link to %pfwf\n",
113 link->consumer, link->supplier);
114 list_del(&link->s_hook);
115 list_del(&link->c_hook);
116 kfree(link);
117}
118
119/**
120 * __fwnode_link_cycle - Mark a fwnode link as being part of a cycle.
121 * @link: the fwnode_link to be marked
122 *
123 * The fwnode_link_lock needs to be held when this function is called.
124 */
125static void __fwnode_link_cycle(struct fwnode_link *link)
126{
127 pr_debug("%pfwf: Relaxing link with %pfwf\n",
128 link->consumer, link->supplier);
129 link->flags |= FWLINK_FLAG_CYCLE;
130}
131
132/**
133 * fwnode_links_purge_suppliers - Delete all supplier links of fwnode_handle.
134 * @fwnode: fwnode whose supplier links need to be deleted
135 *
136 * Deletes all supplier links connecting directly to @fwnode.
137 */
138static void fwnode_links_purge_suppliers(struct fwnode_handle *fwnode)
139{
140 struct fwnode_link *link, *tmp;
141
142 mutex_lock(&fwnode_link_lock);
143 list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook)
144 __fwnode_link_del(link);
145 mutex_unlock(&fwnode_link_lock);
146}
147
148/**
149 * fwnode_links_purge_consumers - Delete all consumer links of fwnode_handle.
150 * @fwnode: fwnode whose consumer links need to be deleted
151 *
152 * Deletes all consumer links connecting directly to @fwnode.
153 */
154static void fwnode_links_purge_consumers(struct fwnode_handle *fwnode)
155{
156 struct fwnode_link *link, *tmp;
157
158 mutex_lock(&fwnode_link_lock);
159 list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook)
160 __fwnode_link_del(link);
161 mutex_unlock(&fwnode_link_lock);
162}
163
164/**
165 * fwnode_links_purge - Delete all links connected to a fwnode_handle.
166 * @fwnode: fwnode whose links needs to be deleted
167 *
168 * Deletes all links connecting directly to a fwnode.
169 */
170void fwnode_links_purge(struct fwnode_handle *fwnode)
171{
172 fwnode_links_purge_suppliers(fwnode);
173 fwnode_links_purge_consumers(fwnode);
174}
175
176void fw_devlink_purge_absent_suppliers(struct fwnode_handle *fwnode)
177{
178 struct fwnode_handle *child;
179
180 /* Don't purge consumer links of an added child */
181 if (fwnode->dev)
182 return;
183
184 fwnode->flags |= FWNODE_FLAG_NOT_DEVICE;
185 fwnode_links_purge_consumers(fwnode);
186
187 fwnode_for_each_available_child_node(fwnode, child)
188 fw_devlink_purge_absent_suppliers(child);
189}
190EXPORT_SYMBOL_GPL(fw_devlink_purge_absent_suppliers);
191
192/**
193 * __fwnode_links_move_consumers - Move consumer from @from to @to fwnode_handle
194 * @from: move consumers away from this fwnode
195 * @to: move consumers to this fwnode
196 *
197 * Move all consumer links from @from fwnode to @to fwnode.
198 */
199static void __fwnode_links_move_consumers(struct fwnode_handle *from,
200 struct fwnode_handle *to)
201{
202 struct fwnode_link *link, *tmp;
203
204 list_for_each_entry_safe(link, tmp, &from->consumers, s_hook) {
205 __fwnode_link_add(link->consumer, to, link->flags);
206 __fwnode_link_del(link);
207 }
208}
209
210/**
211 * __fw_devlink_pickup_dangling_consumers - Pick up dangling consumers
212 * @fwnode: fwnode from which to pick up dangling consumers
213 * @new_sup: fwnode of new supplier
214 *
215 * If the @fwnode has a corresponding struct device and the device supports
216 * probing (that is, added to a bus), then we want to let fw_devlink create
217 * MANAGED device links to this device, so leave @fwnode and its descendant's
218 * fwnode links alone.
219 *
220 * Otherwise, move its consumers to the new supplier @new_sup.
221 */
222static void __fw_devlink_pickup_dangling_consumers(struct fwnode_handle *fwnode,
223 struct fwnode_handle *new_sup)
224{
225 struct fwnode_handle *child;
226
227 if (fwnode->dev && fwnode->dev->bus)
228 return;
229
230 fwnode->flags |= FWNODE_FLAG_NOT_DEVICE;
231 __fwnode_links_move_consumers(fwnode, new_sup);
232
233 fwnode_for_each_available_child_node(fwnode, child)
234 __fw_devlink_pickup_dangling_consumers(child, new_sup);
235}
236
237static DEFINE_MUTEX(device_links_lock);
238DEFINE_STATIC_SRCU(device_links_srcu);
239
240static inline void device_links_write_lock(void)
241{
242 mutex_lock(&device_links_lock);
243}
244
245static inline void device_links_write_unlock(void)
246{
247 mutex_unlock(&device_links_lock);
248}
249
250int device_links_read_lock(void) __acquires(&device_links_srcu)
251{
252 return srcu_read_lock(&device_links_srcu);
253}
254
255void device_links_read_unlock(int idx) __releases(&device_links_srcu)
256{
257 srcu_read_unlock(&device_links_srcu, idx);
258}
259
260int device_links_read_lock_held(void)
261{
262 return srcu_read_lock_held(&device_links_srcu);
263}
264
265static void device_link_synchronize_removal(void)
266{
267 synchronize_srcu(&device_links_srcu);
268}
269
270static void device_link_remove_from_lists(struct device_link *link)
271{
272 list_del_rcu(&link->s_node);
273 list_del_rcu(&link->c_node);
274}
275
276static bool device_is_ancestor(struct device *dev, struct device *target)
277{
278 while (target->parent) {
279 target = target->parent;
280 if (dev == target)
281 return true;
282 }
283 return false;
284}
285
286static inline bool device_link_flag_is_sync_state_only(u32 flags)
287{
288 return (flags & ~(DL_FLAG_INFERRED | DL_FLAG_CYCLE)) ==
289 (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED);
290}
291
292/**
293 * device_is_dependent - Check if one device depends on another one
294 * @dev: Device to check dependencies for.
295 * @target: Device to check against.
296 *
297 * Check if @target depends on @dev or any device dependent on it (its child or
298 * its consumer etc). Return 1 if that is the case or 0 otherwise.
299 */
300int device_is_dependent(struct device *dev, void *target)
301{
302 struct device_link *link;
303 int ret;
304
305 /*
306 * The "ancestors" check is needed to catch the case when the target
307 * device has not been completely initialized yet and it is still
308 * missing from the list of children of its parent device.
309 */
310 if (dev == target || device_is_ancestor(dev, target))
311 return 1;
312
313 ret = device_for_each_child(dev, target, device_is_dependent);
314 if (ret)
315 return ret;
316
317 list_for_each_entry(link, &dev->links.consumers, s_node) {
318 if (device_link_flag_is_sync_state_only(link->flags))
319 continue;
320
321 if (link->consumer == target)
322 return 1;
323
324 ret = device_is_dependent(link->consumer, target);
325 if (ret)
326 break;
327 }
328 return ret;
329}
330
331static void device_link_init_status(struct device_link *link,
332 struct device *consumer,
333 struct device *supplier)
334{
335 switch (supplier->links.status) {
336 case DL_DEV_PROBING:
337 switch (consumer->links.status) {
338 case DL_DEV_PROBING:
339 /*
340 * A consumer driver can create a link to a supplier
341 * that has not completed its probing yet as long as it
342 * knows that the supplier is already functional (for
343 * example, it has just acquired some resources from the
344 * supplier).
345 */
346 link->status = DL_STATE_CONSUMER_PROBE;
347 break;
348 default:
349 link->status = DL_STATE_DORMANT;
350 break;
351 }
352 break;
353 case DL_DEV_DRIVER_BOUND:
354 switch (consumer->links.status) {
355 case DL_DEV_PROBING:
356 link->status = DL_STATE_CONSUMER_PROBE;
357 break;
358 case DL_DEV_DRIVER_BOUND:
359 link->status = DL_STATE_ACTIVE;
360 break;
361 default:
362 link->status = DL_STATE_AVAILABLE;
363 break;
364 }
365 break;
366 case DL_DEV_UNBINDING:
367 link->status = DL_STATE_SUPPLIER_UNBIND;
368 break;
369 default:
370 link->status = DL_STATE_DORMANT;
371 break;
372 }
373}
374
375static int device_reorder_to_tail(struct device *dev, void *not_used)
376{
377 struct device_link *link;
378
379 /*
380 * Devices that have not been registered yet will be put to the ends
381 * of the lists during the registration, so skip them here.
382 */
383 if (device_is_registered(dev))
384 devices_kset_move_last(dev);
385
386 if (device_pm_initialized(dev))
387 device_pm_move_last(dev);
388
389 device_for_each_child(dev, NULL, device_reorder_to_tail);
390 list_for_each_entry(link, &dev->links.consumers, s_node) {
391 if (device_link_flag_is_sync_state_only(link->flags))
392 continue;
393 device_reorder_to_tail(link->consumer, NULL);
394 }
395
396 return 0;
397}
398
399/**
400 * device_pm_move_to_tail - Move set of devices to the end of device lists
401 * @dev: Device to move
402 *
403 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
404 *
405 * It moves the @dev along with all of its children and all of its consumers
406 * to the ends of the device_kset and dpm_list, recursively.
407 */
408void device_pm_move_to_tail(struct device *dev)
409{
410 int idx;
411
412 idx = device_links_read_lock();
413 device_pm_lock();
414 device_reorder_to_tail(dev, NULL);
415 device_pm_unlock();
416 device_links_read_unlock(idx);
417}
418
419#define to_devlink(dev) container_of((dev), struct device_link, link_dev)
420
421static ssize_t status_show(struct device *dev,
422 struct device_attribute *attr, char *buf)
423{
424 const char *output;
425
426 switch (to_devlink(dev)->status) {
427 case DL_STATE_NONE:
428 output = "not tracked";
429 break;
430 case DL_STATE_DORMANT:
431 output = "dormant";
432 break;
433 case DL_STATE_AVAILABLE:
434 output = "available";
435 break;
436 case DL_STATE_CONSUMER_PROBE:
437 output = "consumer probing";
438 break;
439 case DL_STATE_ACTIVE:
440 output = "active";
441 break;
442 case DL_STATE_SUPPLIER_UNBIND:
443 output = "supplier unbinding";
444 break;
445 default:
446 output = "unknown";
447 break;
448 }
449
450 return sysfs_emit(buf, "%s\n", output);
451}
452static DEVICE_ATTR_RO(status);
453
454static ssize_t auto_remove_on_show(struct device *dev,
455 struct device_attribute *attr, char *buf)
456{
457 struct device_link *link = to_devlink(dev);
458 const char *output;
459
460 if (link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
461 output = "supplier unbind";
462 else if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
463 output = "consumer unbind";
464 else
465 output = "never";
466
467 return sysfs_emit(buf, "%s\n", output);
468}
469static DEVICE_ATTR_RO(auto_remove_on);
470
471static ssize_t runtime_pm_show(struct device *dev,
472 struct device_attribute *attr, char *buf)
473{
474 struct device_link *link = to_devlink(dev);
475
476 return sysfs_emit(buf, "%d\n", !!(link->flags & DL_FLAG_PM_RUNTIME));
477}
478static DEVICE_ATTR_RO(runtime_pm);
479
480static ssize_t sync_state_only_show(struct device *dev,
481 struct device_attribute *attr, char *buf)
482{
483 struct device_link *link = to_devlink(dev);
484
485 return sysfs_emit(buf, "%d\n",
486 !!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
487}
488static DEVICE_ATTR_RO(sync_state_only);
489
490static struct attribute *devlink_attrs[] = {
491 &dev_attr_status.attr,
492 &dev_attr_auto_remove_on.attr,
493 &dev_attr_runtime_pm.attr,
494 &dev_attr_sync_state_only.attr,
495 NULL,
496};
497ATTRIBUTE_GROUPS(devlink);
498
499static void device_link_release_fn(struct work_struct *work)
500{
501 struct device_link *link = container_of(work, struct device_link, rm_work);
502
503 /* Ensure that all references to the link object have been dropped. */
504 device_link_synchronize_removal();
505
506 pm_runtime_release_supplier(link);
507 /*
508 * If supplier_preactivated is set, the link has been dropped between
509 * the pm_runtime_get_suppliers() and pm_runtime_put_suppliers() calls
510 * in __driver_probe_device(). In that case, drop the supplier's
511 * PM-runtime usage counter to remove the reference taken by
512 * pm_runtime_get_suppliers().
513 */
514 if (link->supplier_preactivated)
515 pm_runtime_put_noidle(link->supplier);
516
517 pm_request_idle(link->supplier);
518
519 put_device(link->consumer);
520 put_device(link->supplier);
521 kfree(link);
522}
523
524static void devlink_dev_release(struct device *dev)
525{
526 struct device_link *link = to_devlink(dev);
527
528 INIT_WORK(&link->rm_work, device_link_release_fn);
529 /*
530 * It may take a while to complete this work because of the SRCU
531 * synchronization in device_link_release_fn() and if the consumer or
532 * supplier devices get deleted when it runs, so put it into the "long"
533 * workqueue.
534 */
535 queue_work(system_long_wq, &link->rm_work);
536}
537
538static struct class devlink_class = {
539 .name = "devlink",
540 .dev_groups = devlink_groups,
541 .dev_release = devlink_dev_release,
542};
543
544static int devlink_add_symlinks(struct device *dev)
545{
546 int ret;
547 size_t len;
548 struct device_link *link = to_devlink(dev);
549 struct device *sup = link->supplier;
550 struct device *con = link->consumer;
551 char *buf;
552
553 len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
554 strlen(dev_bus_name(con)) + strlen(dev_name(con)));
555 len += strlen(":");
556 len += strlen("supplier:") + 1;
557 buf = kzalloc(len, GFP_KERNEL);
558 if (!buf)
559 return -ENOMEM;
560
561 ret = sysfs_create_link(&link->link_dev.kobj, &sup->kobj, "supplier");
562 if (ret)
563 goto out;
564
565 ret = sysfs_create_link(&link->link_dev.kobj, &con->kobj, "consumer");
566 if (ret)
567 goto err_con;
568
569 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
570 ret = sysfs_create_link(&sup->kobj, &link->link_dev.kobj, buf);
571 if (ret)
572 goto err_con_dev;
573
574 snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
575 ret = sysfs_create_link(&con->kobj, &link->link_dev.kobj, buf);
576 if (ret)
577 goto err_sup_dev;
578
579 goto out;
580
581err_sup_dev:
582 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
583 sysfs_remove_link(&sup->kobj, buf);
584err_con_dev:
585 sysfs_remove_link(&link->link_dev.kobj, "consumer");
586err_con:
587 sysfs_remove_link(&link->link_dev.kobj, "supplier");
588out:
589 kfree(buf);
590 return ret;
591}
592
593static void devlink_remove_symlinks(struct device *dev)
594{
595 struct device_link *link = to_devlink(dev);
596 size_t len;
597 struct device *sup = link->supplier;
598 struct device *con = link->consumer;
599 char *buf;
600
601 sysfs_remove_link(&link->link_dev.kobj, "consumer");
602 sysfs_remove_link(&link->link_dev.kobj, "supplier");
603
604 len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
605 strlen(dev_bus_name(con)) + strlen(dev_name(con)));
606 len += strlen(":");
607 len += strlen("supplier:") + 1;
608 buf = kzalloc(len, GFP_KERNEL);
609 if (!buf) {
610 WARN(1, "Unable to properly free device link symlinks!\n");
611 return;
612 }
613
614 if (device_is_registered(con)) {
615 snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
616 sysfs_remove_link(&con->kobj, buf);
617 }
618 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
619 sysfs_remove_link(&sup->kobj, buf);
620 kfree(buf);
621}
622
623static struct class_interface devlink_class_intf = {
624 .class = &devlink_class,
625 .add_dev = devlink_add_symlinks,
626 .remove_dev = devlink_remove_symlinks,
627};
628
629static int __init devlink_class_init(void)
630{
631 int ret;
632
633 ret = class_register(&devlink_class);
634 if (ret)
635 return ret;
636
637 ret = class_interface_register(&devlink_class_intf);
638 if (ret)
639 class_unregister(&devlink_class);
640
641 return ret;
642}
643postcore_initcall(devlink_class_init);
644
645#define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \
646 DL_FLAG_AUTOREMOVE_SUPPLIER | \
647 DL_FLAG_AUTOPROBE_CONSUMER | \
648 DL_FLAG_SYNC_STATE_ONLY | \
649 DL_FLAG_INFERRED | \
650 DL_FLAG_CYCLE)
651
652#define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \
653 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE)
654
655/**
656 * device_link_add - Create a link between two devices.
657 * @consumer: Consumer end of the link.
658 * @supplier: Supplier end of the link.
659 * @flags: Link flags.
660 *
661 * The caller is responsible for the proper synchronization of the link creation
662 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
663 * runtime PM framework to take the link into account. Second, if the
664 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
665 * be forced into the active meta state and reference-counted upon the creation
666 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
667 * ignored.
668 *
669 * If DL_FLAG_STATELESS is set in @flags, the caller of this function is
670 * expected to release the link returned by it directly with the help of either
671 * device_link_del() or device_link_remove().
672 *
673 * If that flag is not set, however, the caller of this function is handing the
674 * management of the link over to the driver core entirely and its return value
675 * can only be used to check whether or not the link is present. In that case,
676 * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
677 * flags can be used to indicate to the driver core when the link can be safely
678 * deleted. Namely, setting one of them in @flags indicates to the driver core
679 * that the link is not going to be used (by the given caller of this function)
680 * after unbinding the consumer or supplier driver, respectively, from its
681 * device, so the link can be deleted at that point. If none of them is set,
682 * the link will be maintained until one of the devices pointed to by it (either
683 * the consumer or the supplier) is unregistered.
684 *
685 * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
686 * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
687 * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
688 * be used to request the driver core to automatically probe for a consumer
689 * driver after successfully binding a driver to the supplier device.
690 *
691 * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER,
692 * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at
693 * the same time is invalid and will cause NULL to be returned upfront.
694 * However, if a device link between the given @consumer and @supplier pair
695 * exists already when this function is called for them, the existing link will
696 * be returned regardless of its current type and status (the link's flags may
697 * be modified then). The caller of this function is then expected to treat
698 * the link as though it has just been created, so (in particular) if
699 * DL_FLAG_STATELESS was passed in @flags, the link needs to be released
700 * explicitly when not needed any more (as stated above).
701 *
702 * A side effect of the link creation is re-ordering of dpm_list and the
703 * devices_kset list by moving the consumer device and all devices depending
704 * on it to the ends of these lists (that does not happen to devices that have
705 * not been registered when this function is called).
706 *
707 * The supplier device is required to be registered when this function is called
708 * and NULL will be returned if that is not the case. The consumer device need
709 * not be registered, however.
710 */
711struct device_link *device_link_add(struct device *consumer,
712 struct device *supplier, u32 flags)
713{
714 struct device_link *link;
715
716 if (!consumer || !supplier || consumer == supplier ||
717 flags & ~DL_ADD_VALID_FLAGS ||
718 (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) ||
719 (flags & DL_FLAG_AUTOPROBE_CONSUMER &&
720 flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
721 DL_FLAG_AUTOREMOVE_SUPPLIER)))
722 return NULL;
723
724 if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
725 if (pm_runtime_get_sync(supplier) < 0) {
726 pm_runtime_put_noidle(supplier);
727 return NULL;
728 }
729 }
730
731 if (!(flags & DL_FLAG_STATELESS))
732 flags |= DL_FLAG_MANAGED;
733
734 if (flags & DL_FLAG_SYNC_STATE_ONLY &&
735 !device_link_flag_is_sync_state_only(flags))
736 return NULL;
737
738 device_links_write_lock();
739 device_pm_lock();
740
741 /*
742 * If the supplier has not been fully registered yet or there is a
743 * reverse (non-SYNC_STATE_ONLY) dependency between the consumer and
744 * the supplier already in the graph, return NULL. If the link is a
745 * SYNC_STATE_ONLY link, we don't check for reverse dependencies
746 * because it only affects sync_state() callbacks.
747 */
748 if (!device_pm_initialized(supplier)
749 || (!(flags & DL_FLAG_SYNC_STATE_ONLY) &&
750 device_is_dependent(consumer, supplier))) {
751 link = NULL;
752 goto out;
753 }
754
755 /*
756 * SYNC_STATE_ONLY links are useless once a consumer device has probed.
757 * So, only create it if the consumer hasn't probed yet.
758 */
759 if (flags & DL_FLAG_SYNC_STATE_ONLY &&
760 consumer->links.status != DL_DEV_NO_DRIVER &&
761 consumer->links.status != DL_DEV_PROBING) {
762 link = NULL;
763 goto out;
764 }
765
766 /*
767 * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
768 * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
769 * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
770 */
771 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
772 flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
773
774 list_for_each_entry(link, &supplier->links.consumers, s_node) {
775 if (link->consumer != consumer)
776 continue;
777
778 if (link->flags & DL_FLAG_INFERRED &&
779 !(flags & DL_FLAG_INFERRED))
780 link->flags &= ~DL_FLAG_INFERRED;
781
782 if (flags & DL_FLAG_PM_RUNTIME) {
783 if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
784 pm_runtime_new_link(consumer);
785 link->flags |= DL_FLAG_PM_RUNTIME;
786 }
787 if (flags & DL_FLAG_RPM_ACTIVE)
788 refcount_inc(&link->rpm_active);
789 }
790
791 if (flags & DL_FLAG_STATELESS) {
792 kref_get(&link->kref);
793 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
794 !(link->flags & DL_FLAG_STATELESS)) {
795 link->flags |= DL_FLAG_STATELESS;
796 goto reorder;
797 } else {
798 link->flags |= DL_FLAG_STATELESS;
799 goto out;
800 }
801 }
802
803 /*
804 * If the life time of the link following from the new flags is
805 * longer than indicated by the flags of the existing link,
806 * update the existing link to stay around longer.
807 */
808 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
809 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
810 link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
811 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
812 }
813 } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
814 link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
815 DL_FLAG_AUTOREMOVE_SUPPLIER);
816 }
817 if (!(link->flags & DL_FLAG_MANAGED)) {
818 kref_get(&link->kref);
819 link->flags |= DL_FLAG_MANAGED;
820 device_link_init_status(link, consumer, supplier);
821 }
822 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
823 !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
824 link->flags &= ~DL_FLAG_SYNC_STATE_ONLY;
825 goto reorder;
826 }
827
828 goto out;
829 }
830
831 link = kzalloc(sizeof(*link), GFP_KERNEL);
832 if (!link)
833 goto out;
834
835 refcount_set(&link->rpm_active, 1);
836
837 get_device(supplier);
838 link->supplier = supplier;
839 INIT_LIST_HEAD(&link->s_node);
840 get_device(consumer);
841 link->consumer = consumer;
842 INIT_LIST_HEAD(&link->c_node);
843 link->flags = flags;
844 kref_init(&link->kref);
845
846 link->link_dev.class = &devlink_class;
847 device_set_pm_not_required(&link->link_dev);
848 dev_set_name(&link->link_dev, "%s:%s--%s:%s",
849 dev_bus_name(supplier), dev_name(supplier),
850 dev_bus_name(consumer), dev_name(consumer));
851 if (device_register(&link->link_dev)) {
852 put_device(&link->link_dev);
853 link = NULL;
854 goto out;
855 }
856
857 if (flags & DL_FLAG_PM_RUNTIME) {
858 if (flags & DL_FLAG_RPM_ACTIVE)
859 refcount_inc(&link->rpm_active);
860
861 pm_runtime_new_link(consumer);
862 }
863
864 /* Determine the initial link state. */
865 if (flags & DL_FLAG_STATELESS)
866 link->status = DL_STATE_NONE;
867 else
868 device_link_init_status(link, consumer, supplier);
869
870 /*
871 * Some callers expect the link creation during consumer driver probe to
872 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
873 */
874 if (link->status == DL_STATE_CONSUMER_PROBE &&
875 flags & DL_FLAG_PM_RUNTIME)
876 pm_runtime_resume(supplier);
877
878 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
879 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
880
881 if (flags & DL_FLAG_SYNC_STATE_ONLY) {
882 dev_dbg(consumer,
883 "Linked as a sync state only consumer to %s\n",
884 dev_name(supplier));
885 goto out;
886 }
887
888reorder:
889 /*
890 * Move the consumer and all of the devices depending on it to the end
891 * of dpm_list and the devices_kset list.
892 *
893 * It is necessary to hold dpm_list locked throughout all that or else
894 * we may end up suspending with a wrong ordering of it.
895 */
896 device_reorder_to_tail(consumer, NULL);
897
898 dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
899
900out:
901 device_pm_unlock();
902 device_links_write_unlock();
903
904 if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
905 pm_runtime_put(supplier);
906
907 return link;
908}
909EXPORT_SYMBOL_GPL(device_link_add);
910
911static void __device_link_del(struct kref *kref)
912{
913 struct device_link *link = container_of(kref, struct device_link, kref);
914
915 dev_dbg(link->consumer, "Dropping the link to %s\n",
916 dev_name(link->supplier));
917
918 pm_runtime_drop_link(link);
919
920 device_link_remove_from_lists(link);
921 device_unregister(&link->link_dev);
922}
923
924static void device_link_put_kref(struct device_link *link)
925{
926 if (link->flags & DL_FLAG_STATELESS)
927 kref_put(&link->kref, __device_link_del);
928 else if (!device_is_registered(link->consumer))
929 __device_link_del(&link->kref);
930 else
931 WARN(1, "Unable to drop a managed device link reference\n");
932}
933
934/**
935 * device_link_del - Delete a stateless link between two devices.
936 * @link: Device link to delete.
937 *
938 * The caller must ensure proper synchronization of this function with runtime
939 * PM. If the link was added multiple times, it needs to be deleted as often.
940 * Care is required for hotplugged devices: Their links are purged on removal
941 * and calling device_link_del() is then no longer allowed.
942 */
943void device_link_del(struct device_link *link)
944{
945 device_links_write_lock();
946 device_link_put_kref(link);
947 device_links_write_unlock();
948}
949EXPORT_SYMBOL_GPL(device_link_del);
950
951/**
952 * device_link_remove - Delete a stateless link between two devices.
953 * @consumer: Consumer end of the link.
954 * @supplier: Supplier end of the link.
955 *
956 * The caller must ensure proper synchronization of this function with runtime
957 * PM.
958 */
959void device_link_remove(void *consumer, struct device *supplier)
960{
961 struct device_link *link;
962
963 if (WARN_ON(consumer == supplier))
964 return;
965
966 device_links_write_lock();
967
968 list_for_each_entry(link, &supplier->links.consumers, s_node) {
969 if (link->consumer == consumer) {
970 device_link_put_kref(link);
971 break;
972 }
973 }
974
975 device_links_write_unlock();
976}
977EXPORT_SYMBOL_GPL(device_link_remove);
978
979static void device_links_missing_supplier(struct device *dev)
980{
981 struct device_link *link;
982
983 list_for_each_entry(link, &dev->links.suppliers, c_node) {
984 if (link->status != DL_STATE_CONSUMER_PROBE)
985 continue;
986
987 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
988 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
989 } else {
990 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
991 WRITE_ONCE(link->status, DL_STATE_DORMANT);
992 }
993 }
994}
995
996static bool dev_is_best_effort(struct device *dev)
997{
998 return (fw_devlink_best_effort && dev->can_match) ||
999 (dev->fwnode && (dev->fwnode->flags & FWNODE_FLAG_BEST_EFFORT));
1000}
1001
1002static struct fwnode_handle *fwnode_links_check_suppliers(
1003 struct fwnode_handle *fwnode)
1004{
1005 struct fwnode_link *link;
1006
1007 if (!fwnode || fw_devlink_is_permissive())
1008 return NULL;
1009
1010 list_for_each_entry(link, &fwnode->suppliers, c_hook)
1011 if (!(link->flags & FWLINK_FLAG_CYCLE))
1012 return link->supplier;
1013
1014 return NULL;
1015}
1016
1017/**
1018 * device_links_check_suppliers - Check presence of supplier drivers.
1019 * @dev: Consumer device.
1020 *
1021 * Check links from this device to any suppliers. Walk the list of the device's
1022 * links to suppliers and see if all of them are available. If not, simply
1023 * return -EPROBE_DEFER.
1024 *
1025 * We need to guarantee that the supplier will not go away after the check has
1026 * been positive here. It only can go away in __device_release_driver() and
1027 * that function checks the device's links to consumers. This means we need to
1028 * mark the link as "consumer probe in progress" to make the supplier removal
1029 * wait for us to complete (or bad things may happen).
1030 *
1031 * Links without the DL_FLAG_MANAGED flag set are ignored.
1032 */
1033int device_links_check_suppliers(struct device *dev)
1034{
1035 struct device_link *link;
1036 int ret = 0, fwnode_ret = 0;
1037 struct fwnode_handle *sup_fw;
1038
1039 /*
1040 * Device waiting for supplier to become available is not allowed to
1041 * probe.
1042 */
1043 mutex_lock(&fwnode_link_lock);
1044 sup_fw = fwnode_links_check_suppliers(dev->fwnode);
1045 if (sup_fw) {
1046 if (!dev_is_best_effort(dev)) {
1047 fwnode_ret = -EPROBE_DEFER;
1048 dev_err_probe(dev, -EPROBE_DEFER,
1049 "wait for supplier %pfwf\n", sup_fw);
1050 } else {
1051 fwnode_ret = -EAGAIN;
1052 }
1053 }
1054 mutex_unlock(&fwnode_link_lock);
1055 if (fwnode_ret == -EPROBE_DEFER)
1056 return fwnode_ret;
1057
1058 device_links_write_lock();
1059
1060 list_for_each_entry(link, &dev->links.suppliers, c_node) {
1061 if (!(link->flags & DL_FLAG_MANAGED))
1062 continue;
1063
1064 if (link->status != DL_STATE_AVAILABLE &&
1065 !(link->flags & DL_FLAG_SYNC_STATE_ONLY)) {
1066
1067 if (dev_is_best_effort(dev) &&
1068 link->flags & DL_FLAG_INFERRED &&
1069 !link->supplier->can_match) {
1070 ret = -EAGAIN;
1071 continue;
1072 }
1073
1074 device_links_missing_supplier(dev);
1075 dev_err_probe(dev, -EPROBE_DEFER,
1076 "supplier %s not ready\n",
1077 dev_name(link->supplier));
1078 ret = -EPROBE_DEFER;
1079 break;
1080 }
1081 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
1082 }
1083 dev->links.status = DL_DEV_PROBING;
1084
1085 device_links_write_unlock();
1086
1087 return ret ? ret : fwnode_ret;
1088}
1089
1090/**
1091 * __device_links_queue_sync_state - Queue a device for sync_state() callback
1092 * @dev: Device to call sync_state() on
1093 * @list: List head to queue the @dev on
1094 *
1095 * Queues a device for a sync_state() callback when the device links write lock
1096 * isn't held. This allows the sync_state() execution flow to use device links
1097 * APIs. The caller must ensure this function is called with
1098 * device_links_write_lock() held.
1099 *
1100 * This function does a get_device() to make sure the device is not freed while
1101 * on this list.
1102 *
1103 * So the caller must also ensure that device_links_flush_sync_list() is called
1104 * as soon as the caller releases device_links_write_lock(). This is necessary
1105 * to make sure the sync_state() is called in a timely fashion and the
1106 * put_device() is called on this device.
1107 */
1108static void __device_links_queue_sync_state(struct device *dev,
1109 struct list_head *list)
1110{
1111 struct device_link *link;
1112
1113 if (!dev_has_sync_state(dev))
1114 return;
1115 if (dev->state_synced)
1116 return;
1117
1118 list_for_each_entry(link, &dev->links.consumers, s_node) {
1119 if (!(link->flags & DL_FLAG_MANAGED))
1120 continue;
1121 if (link->status != DL_STATE_ACTIVE)
1122 return;
1123 }
1124
1125 /*
1126 * Set the flag here to avoid adding the same device to a list more
1127 * than once. This can happen if new consumers get added to the device
1128 * and probed before the list is flushed.
1129 */
1130 dev->state_synced = true;
1131
1132 if (WARN_ON(!list_empty(&dev->links.defer_sync)))
1133 return;
1134
1135 get_device(dev);
1136 list_add_tail(&dev->links.defer_sync, list);
1137}
1138
1139/**
1140 * device_links_flush_sync_list - Call sync_state() on a list of devices
1141 * @list: List of devices to call sync_state() on
1142 * @dont_lock_dev: Device for which lock is already held by the caller
1143 *
1144 * Calls sync_state() on all the devices that have been queued for it. This
1145 * function is used in conjunction with __device_links_queue_sync_state(). The
1146 * @dont_lock_dev parameter is useful when this function is called from a
1147 * context where a device lock is already held.
1148 */
1149static void device_links_flush_sync_list(struct list_head *list,
1150 struct device *dont_lock_dev)
1151{
1152 struct device *dev, *tmp;
1153
1154 list_for_each_entry_safe(dev, tmp, list, links.defer_sync) {
1155 list_del_init(&dev->links.defer_sync);
1156
1157 if (dev != dont_lock_dev)
1158 device_lock(dev);
1159
1160 dev_sync_state(dev);
1161
1162 if (dev != dont_lock_dev)
1163 device_unlock(dev);
1164
1165 put_device(dev);
1166 }
1167}
1168
1169void device_links_supplier_sync_state_pause(void)
1170{
1171 device_links_write_lock();
1172 defer_sync_state_count++;
1173 device_links_write_unlock();
1174}
1175
1176void device_links_supplier_sync_state_resume(void)
1177{
1178 struct device *dev, *tmp;
1179 LIST_HEAD(sync_list);
1180
1181 device_links_write_lock();
1182 if (!defer_sync_state_count) {
1183 WARN(true, "Unmatched sync_state pause/resume!");
1184 goto out;
1185 }
1186 defer_sync_state_count--;
1187 if (defer_sync_state_count)
1188 goto out;
1189
1190 list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) {
1191 /*
1192 * Delete from deferred_sync list before queuing it to
1193 * sync_list because defer_sync is used for both lists.
1194 */
1195 list_del_init(&dev->links.defer_sync);
1196 __device_links_queue_sync_state(dev, &sync_list);
1197 }
1198out:
1199 device_links_write_unlock();
1200
1201 device_links_flush_sync_list(&sync_list, NULL);
1202}
1203
1204static int sync_state_resume_initcall(void)
1205{
1206 device_links_supplier_sync_state_resume();
1207 return 0;
1208}
1209late_initcall(sync_state_resume_initcall);
1210
1211static void __device_links_supplier_defer_sync(struct device *sup)
1212{
1213 if (list_empty(&sup->links.defer_sync) && dev_has_sync_state(sup))
1214 list_add_tail(&sup->links.defer_sync, &deferred_sync);
1215}
1216
1217static void device_link_drop_managed(struct device_link *link)
1218{
1219 link->flags &= ~DL_FLAG_MANAGED;
1220 WRITE_ONCE(link->status, DL_STATE_NONE);
1221 kref_put(&link->kref, __device_link_del);
1222}
1223
1224static ssize_t waiting_for_supplier_show(struct device *dev,
1225 struct device_attribute *attr,
1226 char *buf)
1227{
1228 bool val;
1229
1230 device_lock(dev);
1231 mutex_lock(&fwnode_link_lock);
1232 val = !!fwnode_links_check_suppliers(dev->fwnode);
1233 mutex_unlock(&fwnode_link_lock);
1234 device_unlock(dev);
1235 return sysfs_emit(buf, "%u\n", val);
1236}
1237static DEVICE_ATTR_RO(waiting_for_supplier);
1238
1239/**
1240 * device_links_force_bind - Prepares device to be force bound
1241 * @dev: Consumer device.
1242 *
1243 * device_bind_driver() force binds a device to a driver without calling any
1244 * driver probe functions. So the consumer really isn't going to wait for any
1245 * supplier before it's bound to the driver. We still want the device link
1246 * states to be sensible when this happens.
1247 *
1248 * In preparation for device_bind_driver(), this function goes through each
1249 * supplier device links and checks if the supplier is bound. If it is, then
1250 * the device link status is set to CONSUMER_PROBE. Otherwise, the device link
1251 * is dropped. Links without the DL_FLAG_MANAGED flag set are ignored.
1252 */
1253void device_links_force_bind(struct device *dev)
1254{
1255 struct device_link *link, *ln;
1256
1257 device_links_write_lock();
1258
1259 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
1260 if (!(link->flags & DL_FLAG_MANAGED))
1261 continue;
1262
1263 if (link->status != DL_STATE_AVAILABLE) {
1264 device_link_drop_managed(link);
1265 continue;
1266 }
1267 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
1268 }
1269 dev->links.status = DL_DEV_PROBING;
1270
1271 device_links_write_unlock();
1272}
1273
1274/**
1275 * device_links_driver_bound - Update device links after probing its driver.
1276 * @dev: Device to update the links for.
1277 *
1278 * The probe has been successful, so update links from this device to any
1279 * consumers by changing their status to "available".
1280 *
1281 * Also change the status of @dev's links to suppliers to "active".
1282 *
1283 * Links without the DL_FLAG_MANAGED flag set are ignored.
1284 */
1285void device_links_driver_bound(struct device *dev)
1286{
1287 struct device_link *link, *ln;
1288 LIST_HEAD(sync_list);
1289
1290 /*
1291 * If a device binds successfully, it's expected to have created all
1292 * the device links it needs to or make new device links as it needs
1293 * them. So, fw_devlink no longer needs to create device links to any
1294 * of the device's suppliers.
1295 *
1296 * Also, if a child firmware node of this bound device is not added as a
1297 * device by now, assume it is never going to be added. Make this bound
1298 * device the fallback supplier to the dangling consumers of the child
1299 * firmware node because this bound device is probably implementing the
1300 * child firmware node functionality and we don't want the dangling
1301 * consumers to defer probe indefinitely waiting for a device for the
1302 * child firmware node.
1303 */
1304 if (dev->fwnode && dev->fwnode->dev == dev) {
1305 struct fwnode_handle *child;
1306 fwnode_links_purge_suppliers(dev->fwnode);
1307 mutex_lock(&fwnode_link_lock);
1308 fwnode_for_each_available_child_node(dev->fwnode, child)
1309 __fw_devlink_pickup_dangling_consumers(child,
1310 dev->fwnode);
1311 __fw_devlink_link_to_consumers(dev);
1312 mutex_unlock(&fwnode_link_lock);
1313 }
1314 device_remove_file(dev, &dev_attr_waiting_for_supplier);
1315
1316 device_links_write_lock();
1317
1318 list_for_each_entry(link, &dev->links.consumers, s_node) {
1319 if (!(link->flags & DL_FLAG_MANAGED))
1320 continue;
1321
1322 /*
1323 * Links created during consumer probe may be in the "consumer
1324 * probe" state to start with if the supplier is still probing
1325 * when they are created and they may become "active" if the
1326 * consumer probe returns first. Skip them here.
1327 */
1328 if (link->status == DL_STATE_CONSUMER_PROBE ||
1329 link->status == DL_STATE_ACTIVE)
1330 continue;
1331
1332 WARN_ON(link->status != DL_STATE_DORMANT);
1333 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
1334
1335 if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER)
1336 driver_deferred_probe_add(link->consumer);
1337 }
1338
1339 if (defer_sync_state_count)
1340 __device_links_supplier_defer_sync(dev);
1341 else
1342 __device_links_queue_sync_state(dev, &sync_list);
1343
1344 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
1345 struct device *supplier;
1346
1347 if (!(link->flags & DL_FLAG_MANAGED))
1348 continue;
1349
1350 supplier = link->supplier;
1351 if (link->flags & DL_FLAG_SYNC_STATE_ONLY) {
1352 /*
1353 * When DL_FLAG_SYNC_STATE_ONLY is set, it means no
1354 * other DL_MANAGED_LINK_FLAGS have been set. So, it's
1355 * save to drop the managed link completely.
1356 */
1357 device_link_drop_managed(link);
1358 } else if (dev_is_best_effort(dev) &&
1359 link->flags & DL_FLAG_INFERRED &&
1360 link->status != DL_STATE_CONSUMER_PROBE &&
1361 !link->supplier->can_match) {
1362 /*
1363 * When dev_is_best_effort() is true, we ignore device
1364 * links to suppliers that don't have a driver. If the
1365 * consumer device still managed to probe, there's no
1366 * point in maintaining a device link in a weird state
1367 * (consumer probed before supplier). So delete it.
1368 */
1369 device_link_drop_managed(link);
1370 } else {
1371 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
1372 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
1373 }
1374
1375 /*
1376 * This needs to be done even for the deleted
1377 * DL_FLAG_SYNC_STATE_ONLY device link in case it was the last
1378 * device link that was preventing the supplier from getting a
1379 * sync_state() call.
1380 */
1381 if (defer_sync_state_count)
1382 __device_links_supplier_defer_sync(supplier);
1383 else
1384 __device_links_queue_sync_state(supplier, &sync_list);
1385 }
1386
1387 dev->links.status = DL_DEV_DRIVER_BOUND;
1388
1389 device_links_write_unlock();
1390
1391 device_links_flush_sync_list(&sync_list, dev);
1392}
1393
1394/**
1395 * __device_links_no_driver - Update links of a device without a driver.
1396 * @dev: Device without a drvier.
1397 *
1398 * Delete all non-persistent links from this device to any suppliers.
1399 *
1400 * Persistent links stay around, but their status is changed to "available",
1401 * unless they already are in the "supplier unbind in progress" state in which
1402 * case they need not be updated.
1403 *
1404 * Links without the DL_FLAG_MANAGED flag set are ignored.
1405 */
1406static void __device_links_no_driver(struct device *dev)
1407{
1408 struct device_link *link, *ln;
1409
1410 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1411 if (!(link->flags & DL_FLAG_MANAGED))
1412 continue;
1413
1414 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
1415 device_link_drop_managed(link);
1416 continue;
1417 }
1418
1419 if (link->status != DL_STATE_CONSUMER_PROBE &&
1420 link->status != DL_STATE_ACTIVE)
1421 continue;
1422
1423 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
1424 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
1425 } else {
1426 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
1427 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1428 }
1429 }
1430
1431 dev->links.status = DL_DEV_NO_DRIVER;
1432}
1433
1434/**
1435 * device_links_no_driver - Update links after failing driver probe.
1436 * @dev: Device whose driver has just failed to probe.
1437 *
1438 * Clean up leftover links to consumers for @dev and invoke
1439 * %__device_links_no_driver() to update links to suppliers for it as
1440 * appropriate.
1441 *
1442 * Links without the DL_FLAG_MANAGED flag set are ignored.
1443 */
1444void device_links_no_driver(struct device *dev)
1445{
1446 struct device_link *link;
1447
1448 device_links_write_lock();
1449
1450 list_for_each_entry(link, &dev->links.consumers, s_node) {
1451 if (!(link->flags & DL_FLAG_MANAGED))
1452 continue;
1453
1454 /*
1455 * The probe has failed, so if the status of the link is
1456 * "consumer probe" or "active", it must have been added by
1457 * a probing consumer while this device was still probing.
1458 * Change its state to "dormant", as it represents a valid
1459 * relationship, but it is not functionally meaningful.
1460 */
1461 if (link->status == DL_STATE_CONSUMER_PROBE ||
1462 link->status == DL_STATE_ACTIVE)
1463 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1464 }
1465
1466 __device_links_no_driver(dev);
1467
1468 device_links_write_unlock();
1469}
1470
1471/**
1472 * device_links_driver_cleanup - Update links after driver removal.
1473 * @dev: Device whose driver has just gone away.
1474 *
1475 * Update links to consumers for @dev by changing their status to "dormant" and
1476 * invoke %__device_links_no_driver() to update links to suppliers for it as
1477 * appropriate.
1478 *
1479 * Links without the DL_FLAG_MANAGED flag set are ignored.
1480 */
1481void device_links_driver_cleanup(struct device *dev)
1482{
1483 struct device_link *link, *ln;
1484
1485 device_links_write_lock();
1486
1487 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
1488 if (!(link->flags & DL_FLAG_MANAGED))
1489 continue;
1490
1491 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
1492 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
1493
1494 /*
1495 * autoremove the links between this @dev and its consumer
1496 * devices that are not active, i.e. where the link state
1497 * has moved to DL_STATE_SUPPLIER_UNBIND.
1498 */
1499 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
1500 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
1501 device_link_drop_managed(link);
1502
1503 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1504 }
1505
1506 list_del_init(&dev->links.defer_sync);
1507 __device_links_no_driver(dev);
1508
1509 device_links_write_unlock();
1510}
1511
1512/**
1513 * device_links_busy - Check if there are any busy links to consumers.
1514 * @dev: Device to check.
1515 *
1516 * Check each consumer of the device and return 'true' if its link's status
1517 * is one of "consumer probe" or "active" (meaning that the given consumer is
1518 * probing right now or its driver is present). Otherwise, change the link
1519 * state to "supplier unbind" to prevent the consumer from being probed
1520 * successfully going forward.
1521 *
1522 * Return 'false' if there are no probing or active consumers.
1523 *
1524 * Links without the DL_FLAG_MANAGED flag set are ignored.
1525 */
1526bool device_links_busy(struct device *dev)
1527{
1528 struct device_link *link;
1529 bool ret = false;
1530
1531 device_links_write_lock();
1532
1533 list_for_each_entry(link, &dev->links.consumers, s_node) {
1534 if (!(link->flags & DL_FLAG_MANAGED))
1535 continue;
1536
1537 if (link->status == DL_STATE_CONSUMER_PROBE
1538 || link->status == DL_STATE_ACTIVE) {
1539 ret = true;
1540 break;
1541 }
1542 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1543 }
1544
1545 dev->links.status = DL_DEV_UNBINDING;
1546
1547 device_links_write_unlock();
1548 return ret;
1549}
1550
1551/**
1552 * device_links_unbind_consumers - Force unbind consumers of the given device.
1553 * @dev: Device to unbind the consumers of.
1554 *
1555 * Walk the list of links to consumers for @dev and if any of them is in the
1556 * "consumer probe" state, wait for all device probes in progress to complete
1557 * and start over.
1558 *
1559 * If that's not the case, change the status of the link to "supplier unbind"
1560 * and check if the link was in the "active" state. If so, force the consumer
1561 * driver to unbind and start over (the consumer will not re-probe as we have
1562 * changed the state of the link already).
1563 *
1564 * Links without the DL_FLAG_MANAGED flag set are ignored.
1565 */
1566void device_links_unbind_consumers(struct device *dev)
1567{
1568 struct device_link *link;
1569
1570 start:
1571 device_links_write_lock();
1572
1573 list_for_each_entry(link, &dev->links.consumers, s_node) {
1574 enum device_link_state status;
1575
1576 if (!(link->flags & DL_FLAG_MANAGED) ||
1577 link->flags & DL_FLAG_SYNC_STATE_ONLY)
1578 continue;
1579
1580 status = link->status;
1581 if (status == DL_STATE_CONSUMER_PROBE) {
1582 device_links_write_unlock();
1583
1584 wait_for_device_probe();
1585 goto start;
1586 }
1587 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1588 if (status == DL_STATE_ACTIVE) {
1589 struct device *consumer = link->consumer;
1590
1591 get_device(consumer);
1592
1593 device_links_write_unlock();
1594
1595 device_release_driver_internal(consumer, NULL,
1596 consumer->parent);
1597 put_device(consumer);
1598 goto start;
1599 }
1600 }
1601
1602 device_links_write_unlock();
1603}
1604
1605/**
1606 * device_links_purge - Delete existing links to other devices.
1607 * @dev: Target device.
1608 */
1609static void device_links_purge(struct device *dev)
1610{
1611 struct device_link *link, *ln;
1612
1613 if (dev->class == &devlink_class)
1614 return;
1615
1616 /*
1617 * Delete all of the remaining links from this device to any other
1618 * devices (either consumers or suppliers).
1619 */
1620 device_links_write_lock();
1621
1622 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1623 WARN_ON(link->status == DL_STATE_ACTIVE);
1624 __device_link_del(&link->kref);
1625 }
1626
1627 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
1628 WARN_ON(link->status != DL_STATE_DORMANT &&
1629 link->status != DL_STATE_NONE);
1630 __device_link_del(&link->kref);
1631 }
1632
1633 device_links_write_unlock();
1634}
1635
1636#define FW_DEVLINK_FLAGS_PERMISSIVE (DL_FLAG_INFERRED | \
1637 DL_FLAG_SYNC_STATE_ONLY)
1638#define FW_DEVLINK_FLAGS_ON (DL_FLAG_INFERRED | \
1639 DL_FLAG_AUTOPROBE_CONSUMER)
1640#define FW_DEVLINK_FLAGS_RPM (FW_DEVLINK_FLAGS_ON | \
1641 DL_FLAG_PM_RUNTIME)
1642
1643static u32 fw_devlink_flags = FW_DEVLINK_FLAGS_ON;
1644static int __init fw_devlink_setup(char *arg)
1645{
1646 if (!arg)
1647 return -EINVAL;
1648
1649 if (strcmp(arg, "off") == 0) {
1650 fw_devlink_flags = 0;
1651 } else if (strcmp(arg, "permissive") == 0) {
1652 fw_devlink_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
1653 } else if (strcmp(arg, "on") == 0) {
1654 fw_devlink_flags = FW_DEVLINK_FLAGS_ON;
1655 } else if (strcmp(arg, "rpm") == 0) {
1656 fw_devlink_flags = FW_DEVLINK_FLAGS_RPM;
1657 }
1658 return 0;
1659}
1660early_param("fw_devlink", fw_devlink_setup);
1661
1662static bool fw_devlink_strict;
1663static int __init fw_devlink_strict_setup(char *arg)
1664{
1665 return kstrtobool(arg, &fw_devlink_strict);
1666}
1667early_param("fw_devlink.strict", fw_devlink_strict_setup);
1668
1669#define FW_DEVLINK_SYNC_STATE_STRICT 0
1670#define FW_DEVLINK_SYNC_STATE_TIMEOUT 1
1671
1672#ifndef CONFIG_FW_DEVLINK_SYNC_STATE_TIMEOUT
1673static int fw_devlink_sync_state;
1674#else
1675static int fw_devlink_sync_state = FW_DEVLINK_SYNC_STATE_TIMEOUT;
1676#endif
1677
1678static int __init fw_devlink_sync_state_setup(char *arg)
1679{
1680 if (!arg)
1681 return -EINVAL;
1682
1683 if (strcmp(arg, "strict") == 0) {
1684 fw_devlink_sync_state = FW_DEVLINK_SYNC_STATE_STRICT;
1685 return 0;
1686 } else if (strcmp(arg, "timeout") == 0) {
1687 fw_devlink_sync_state = FW_DEVLINK_SYNC_STATE_TIMEOUT;
1688 return 0;
1689 }
1690 return -EINVAL;
1691}
1692early_param("fw_devlink.sync_state", fw_devlink_sync_state_setup);
1693
1694static inline u32 fw_devlink_get_flags(u8 fwlink_flags)
1695{
1696 if (fwlink_flags & FWLINK_FLAG_CYCLE)
1697 return FW_DEVLINK_FLAGS_PERMISSIVE | DL_FLAG_CYCLE;
1698
1699 return fw_devlink_flags;
1700}
1701
1702static bool fw_devlink_is_permissive(void)
1703{
1704 return fw_devlink_flags == FW_DEVLINK_FLAGS_PERMISSIVE;
1705}
1706
1707bool fw_devlink_is_strict(void)
1708{
1709 return fw_devlink_strict && !fw_devlink_is_permissive();
1710}
1711
1712static void fw_devlink_parse_fwnode(struct fwnode_handle *fwnode)
1713{
1714 if (fwnode->flags & FWNODE_FLAG_LINKS_ADDED)
1715 return;
1716
1717 fwnode_call_int_op(fwnode, add_links);
1718 fwnode->flags |= FWNODE_FLAG_LINKS_ADDED;
1719}
1720
1721static void fw_devlink_parse_fwtree(struct fwnode_handle *fwnode)
1722{
1723 struct fwnode_handle *child = NULL;
1724
1725 fw_devlink_parse_fwnode(fwnode);
1726
1727 while ((child = fwnode_get_next_available_child_node(fwnode, child)))
1728 fw_devlink_parse_fwtree(child);
1729}
1730
1731static void fw_devlink_relax_link(struct device_link *link)
1732{
1733 if (!(link->flags & DL_FLAG_INFERRED))
1734 return;
1735
1736 if (device_link_flag_is_sync_state_only(link->flags))
1737 return;
1738
1739 pm_runtime_drop_link(link);
1740 link->flags = DL_FLAG_MANAGED | FW_DEVLINK_FLAGS_PERMISSIVE;
1741 dev_dbg(link->consumer, "Relaxing link with %s\n",
1742 dev_name(link->supplier));
1743}
1744
1745static int fw_devlink_no_driver(struct device *dev, void *data)
1746{
1747 struct device_link *link = to_devlink(dev);
1748
1749 if (!link->supplier->can_match)
1750 fw_devlink_relax_link(link);
1751
1752 return 0;
1753}
1754
1755void fw_devlink_drivers_done(void)
1756{
1757 fw_devlink_drv_reg_done = true;
1758 device_links_write_lock();
1759 class_for_each_device(&devlink_class, NULL, NULL,
1760 fw_devlink_no_driver);
1761 device_links_write_unlock();
1762}
1763
1764static int fw_devlink_dev_sync_state(struct device *dev, void *data)
1765{
1766 struct device_link *link = to_devlink(dev);
1767 struct device *sup = link->supplier;
1768
1769 if (!(link->flags & DL_FLAG_MANAGED) ||
1770 link->status == DL_STATE_ACTIVE || sup->state_synced ||
1771 !dev_has_sync_state(sup))
1772 return 0;
1773
1774 if (fw_devlink_sync_state == FW_DEVLINK_SYNC_STATE_STRICT) {
1775 dev_warn(sup, "sync_state() pending due to %s\n",
1776 dev_name(link->consumer));
1777 return 0;
1778 }
1779
1780 if (!list_empty(&sup->links.defer_sync))
1781 return 0;
1782
1783 dev_warn(sup, "Timed out. Forcing sync_state()\n");
1784 sup->state_synced = true;
1785 get_device(sup);
1786 list_add_tail(&sup->links.defer_sync, data);
1787
1788 return 0;
1789}
1790
1791void fw_devlink_probing_done(void)
1792{
1793 LIST_HEAD(sync_list);
1794
1795 device_links_write_lock();
1796 class_for_each_device(&devlink_class, NULL, &sync_list,
1797 fw_devlink_dev_sync_state);
1798 device_links_write_unlock();
1799 device_links_flush_sync_list(&sync_list, NULL);
1800}
1801
1802/**
1803 * wait_for_init_devices_probe - Try to probe any device needed for init
1804 *
1805 * Some devices might need to be probed and bound successfully before the kernel
1806 * boot sequence can finish and move on to init/userspace. For example, a
1807 * network interface might need to be bound to be able to mount a NFS rootfs.
1808 *
1809 * With fw_devlink=on by default, some of these devices might be blocked from
1810 * probing because they are waiting on a optional supplier that doesn't have a
1811 * driver. While fw_devlink will eventually identify such devices and unblock
1812 * the probing automatically, it might be too late by the time it unblocks the
1813 * probing of devices. For example, the IP4 autoconfig might timeout before
1814 * fw_devlink unblocks probing of the network interface.
1815 *
1816 * This function is available to temporarily try and probe all devices that have
1817 * a driver even if some of their suppliers haven't been added or don't have
1818 * drivers.
1819 *
1820 * The drivers can then decide which of the suppliers are optional vs mandatory
1821 * and probe the device if possible. By the time this function returns, all such
1822 * "best effort" probes are guaranteed to be completed. If a device successfully
1823 * probes in this mode, we delete all fw_devlink discovered dependencies of that
1824 * device where the supplier hasn't yet probed successfully because they have to
1825 * be optional dependencies.
1826 *
1827 * Any devices that didn't successfully probe go back to being treated as if
1828 * this function was never called.
1829 *
1830 * This also means that some devices that aren't needed for init and could have
1831 * waited for their optional supplier to probe (when the supplier's module is
1832 * loaded later on) would end up probing prematurely with limited functionality.
1833 * So call this function only when boot would fail without it.
1834 */
1835void __init wait_for_init_devices_probe(void)
1836{
1837 if (!fw_devlink_flags || fw_devlink_is_permissive())
1838 return;
1839
1840 /*
1841 * Wait for all ongoing probes to finish so that the "best effort" is
1842 * only applied to devices that can't probe otherwise.
1843 */
1844 wait_for_device_probe();
1845
1846 pr_info("Trying to probe devices needed for running init ...\n");
1847 fw_devlink_best_effort = true;
1848 driver_deferred_probe_trigger();
1849
1850 /*
1851 * Wait for all "best effort" probes to finish before going back to
1852 * normal enforcement.
1853 */
1854 wait_for_device_probe();
1855 fw_devlink_best_effort = false;
1856}
1857
1858static void fw_devlink_unblock_consumers(struct device *dev)
1859{
1860 struct device_link *link;
1861
1862 if (!fw_devlink_flags || fw_devlink_is_permissive())
1863 return;
1864
1865 device_links_write_lock();
1866 list_for_each_entry(link, &dev->links.consumers, s_node)
1867 fw_devlink_relax_link(link);
1868 device_links_write_unlock();
1869}
1870
1871
1872static bool fwnode_init_without_drv(struct fwnode_handle *fwnode)
1873{
1874 struct device *dev;
1875 bool ret;
1876
1877 if (!(fwnode->flags & FWNODE_FLAG_INITIALIZED))
1878 return false;
1879
1880 dev = get_dev_from_fwnode(fwnode);
1881 ret = !dev || dev->links.status == DL_DEV_NO_DRIVER;
1882 put_device(dev);
1883
1884 return ret;
1885}
1886
1887static bool fwnode_ancestor_init_without_drv(struct fwnode_handle *fwnode)
1888{
1889 struct fwnode_handle *parent;
1890
1891 fwnode_for_each_parent_node(fwnode, parent) {
1892 if (fwnode_init_without_drv(parent)) {
1893 fwnode_handle_put(parent);
1894 return true;
1895 }
1896 }
1897
1898 return false;
1899}
1900
1901/**
1902 * __fw_devlink_relax_cycles - Relax and mark dependency cycles.
1903 * @con: Potential consumer device.
1904 * @sup_handle: Potential supplier's fwnode.
1905 *
1906 * Needs to be called with fwnode_lock and device link lock held.
1907 *
1908 * Check if @sup_handle or any of its ancestors or suppliers direct/indirectly
1909 * depend on @con. This function can detect multiple cyles between @sup_handle
1910 * and @con. When such dependency cycles are found, convert all device links
1911 * created solely by fw_devlink into SYNC_STATE_ONLY device links. Also, mark
1912 * all fwnode links in the cycle with FWLINK_FLAG_CYCLE so that when they are
1913 * converted into a device link in the future, they are created as
1914 * SYNC_STATE_ONLY device links. This is the equivalent of doing
1915 * fw_devlink=permissive just between the devices in the cycle. We need to do
1916 * this because, at this point, fw_devlink can't tell which of these
1917 * dependencies is not a real dependency.
1918 *
1919 * Return true if one or more cycles were found. Otherwise, return false.
1920 */
1921static bool __fw_devlink_relax_cycles(struct device *con,
1922 struct fwnode_handle *sup_handle)
1923{
1924 struct device *sup_dev = NULL, *par_dev = NULL;
1925 struct fwnode_link *link;
1926 struct device_link *dev_link;
1927 bool ret = false;
1928
1929 if (!sup_handle)
1930 return false;
1931
1932 /*
1933 * We aren't trying to find all cycles. Just a cycle between con and
1934 * sup_handle.
1935 */
1936 if (sup_handle->flags & FWNODE_FLAG_VISITED)
1937 return false;
1938
1939 sup_handle->flags |= FWNODE_FLAG_VISITED;
1940
1941 sup_dev = get_dev_from_fwnode(sup_handle);
1942
1943 /* Termination condition. */
1944 if (sup_dev == con) {
1945 ret = true;
1946 goto out;
1947 }
1948
1949 /*
1950 * If sup_dev is bound to a driver and @con hasn't started binding to a
1951 * driver, sup_dev can't be a consumer of @con. So, no need to check
1952 * further.
1953 */
1954 if (sup_dev && sup_dev->links.status == DL_DEV_DRIVER_BOUND &&
1955 con->links.status == DL_DEV_NO_DRIVER) {
1956 ret = false;
1957 goto out;
1958 }
1959
1960 list_for_each_entry(link, &sup_handle->suppliers, c_hook) {
1961 if (__fw_devlink_relax_cycles(con, link->supplier)) {
1962 __fwnode_link_cycle(link);
1963 ret = true;
1964 }
1965 }
1966
1967 /*
1968 * Give priority to device parent over fwnode parent to account for any
1969 * quirks in how fwnodes are converted to devices.
1970 */
1971 if (sup_dev)
1972 par_dev = get_device(sup_dev->parent);
1973 else
1974 par_dev = fwnode_get_next_parent_dev(sup_handle);
1975
1976 if (par_dev && __fw_devlink_relax_cycles(con, par_dev->fwnode))
1977 ret = true;
1978
1979 if (!sup_dev)
1980 goto out;
1981
1982 list_for_each_entry(dev_link, &sup_dev->links.suppliers, c_node) {
1983 /*
1984 * Ignore a SYNC_STATE_ONLY flag only if it wasn't marked as
1985 * such due to a cycle.
1986 */
1987 if (device_link_flag_is_sync_state_only(dev_link->flags) &&
1988 !(dev_link->flags & DL_FLAG_CYCLE))
1989 continue;
1990
1991 if (__fw_devlink_relax_cycles(con,
1992 dev_link->supplier->fwnode)) {
1993 fw_devlink_relax_link(dev_link);
1994 dev_link->flags |= DL_FLAG_CYCLE;
1995 ret = true;
1996 }
1997 }
1998
1999out:
2000 sup_handle->flags &= ~FWNODE_FLAG_VISITED;
2001 put_device(sup_dev);
2002 put_device(par_dev);
2003 return ret;
2004}
2005
2006/**
2007 * fw_devlink_create_devlink - Create a device link from a consumer to fwnode
2008 * @con: consumer device for the device link
2009 * @sup_handle: fwnode handle of supplier
2010 * @link: fwnode link that's being converted to a device link
2011 *
2012 * This function will try to create a device link between the consumer device
2013 * @con and the supplier device represented by @sup_handle.
2014 *
2015 * The supplier has to be provided as a fwnode because incorrect cycles in
2016 * fwnode links can sometimes cause the supplier device to never be created.
2017 * This function detects such cases and returns an error if it cannot create a
2018 * device link from the consumer to a missing supplier.
2019 *
2020 * Returns,
2021 * 0 on successfully creating a device link
2022 * -EINVAL if the device link cannot be created as expected
2023 * -EAGAIN if the device link cannot be created right now, but it may be
2024 * possible to do that in the future
2025 */
2026static int fw_devlink_create_devlink(struct device *con,
2027 struct fwnode_handle *sup_handle,
2028 struct fwnode_link *link)
2029{
2030 struct device *sup_dev;
2031 int ret = 0;
2032 u32 flags;
2033
2034 if (con->fwnode == link->consumer)
2035 flags = fw_devlink_get_flags(link->flags);
2036 else
2037 flags = FW_DEVLINK_FLAGS_PERMISSIVE;
2038
2039 /*
2040 * In some cases, a device P might also be a supplier to its child node
2041 * C. However, this would defer the probe of C until the probe of P
2042 * completes successfully. This is perfectly fine in the device driver
2043 * model. device_add() doesn't guarantee probe completion of the device
2044 * by the time it returns.
2045 *
2046 * However, there are a few drivers that assume C will finish probing
2047 * as soon as it's added and before P finishes probing. So, we provide
2048 * a flag to let fw_devlink know not to delay the probe of C until the
2049 * probe of P completes successfully.
2050 *
2051 * When such a flag is set, we can't create device links where P is the
2052 * supplier of C as that would delay the probe of C.
2053 */
2054 if (sup_handle->flags & FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD &&
2055 fwnode_is_ancestor_of(sup_handle, con->fwnode))
2056 return -EINVAL;
2057
2058 /*
2059 * SYNC_STATE_ONLY device links don't block probing and supports cycles.
2060 * So cycle detection isn't necessary and shouldn't be done.
2061 */
2062 if (!(flags & DL_FLAG_SYNC_STATE_ONLY)) {
2063 device_links_write_lock();
2064 if (__fw_devlink_relax_cycles(con, sup_handle)) {
2065 __fwnode_link_cycle(link);
2066 flags = fw_devlink_get_flags(link->flags);
2067 dev_info(con, "Fixed dependency cycle(s) with %pfwf\n",
2068 sup_handle);
2069 }
2070 device_links_write_unlock();
2071 }
2072
2073 if (sup_handle->flags & FWNODE_FLAG_NOT_DEVICE)
2074 sup_dev = fwnode_get_next_parent_dev(sup_handle);
2075 else
2076 sup_dev = get_dev_from_fwnode(sup_handle);
2077
2078 if (sup_dev) {
2079 /*
2080 * If it's one of those drivers that don't actually bind to
2081 * their device using driver core, then don't wait on this
2082 * supplier device indefinitely.
2083 */
2084 if (sup_dev->links.status == DL_DEV_NO_DRIVER &&
2085 sup_handle->flags & FWNODE_FLAG_INITIALIZED) {
2086 dev_dbg(con,
2087 "Not linking %pfwf - dev might never probe\n",
2088 sup_handle);
2089 ret = -EINVAL;
2090 goto out;
2091 }
2092
2093 if (con != sup_dev && !device_link_add(con, sup_dev, flags)) {
2094 dev_err(con, "Failed to create device link (0x%x) with %s\n",
2095 flags, dev_name(sup_dev));
2096 ret = -EINVAL;
2097 }
2098
2099 goto out;
2100 }
2101
2102 /*
2103 * Supplier or supplier's ancestor already initialized without a struct
2104 * device or being probed by a driver.
2105 */
2106 if (fwnode_init_without_drv(sup_handle) ||
2107 fwnode_ancestor_init_without_drv(sup_handle)) {
2108 dev_dbg(con, "Not linking %pfwf - might never become dev\n",
2109 sup_handle);
2110 return -EINVAL;
2111 }
2112
2113 ret = -EAGAIN;
2114out:
2115 put_device(sup_dev);
2116 return ret;
2117}
2118
2119/**
2120 * __fw_devlink_link_to_consumers - Create device links to consumers of a device
2121 * @dev: Device that needs to be linked to its consumers
2122 *
2123 * This function looks at all the consumer fwnodes of @dev and creates device
2124 * links between the consumer device and @dev (supplier).
2125 *
2126 * If the consumer device has not been added yet, then this function creates a
2127 * SYNC_STATE_ONLY link between @dev (supplier) and the closest ancestor device
2128 * of the consumer fwnode. This is necessary to make sure @dev doesn't get a
2129 * sync_state() callback before the real consumer device gets to be added and
2130 * then probed.
2131 *
2132 * Once device links are created from the real consumer to @dev (supplier), the
2133 * fwnode links are deleted.
2134 */
2135static void __fw_devlink_link_to_consumers(struct device *dev)
2136{
2137 struct fwnode_handle *fwnode = dev->fwnode;
2138 struct fwnode_link *link, *tmp;
2139
2140 list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook) {
2141 struct device *con_dev;
2142 bool own_link = true;
2143 int ret;
2144
2145 con_dev = get_dev_from_fwnode(link->consumer);
2146 /*
2147 * If consumer device is not available yet, make a "proxy"
2148 * SYNC_STATE_ONLY link from the consumer's parent device to
2149 * the supplier device. This is necessary to make sure the
2150 * supplier doesn't get a sync_state() callback before the real
2151 * consumer can create a device link to the supplier.
2152 *
2153 * This proxy link step is needed to handle the case where the
2154 * consumer's parent device is added before the supplier.
2155 */
2156 if (!con_dev) {
2157 con_dev = fwnode_get_next_parent_dev(link->consumer);
2158 /*
2159 * However, if the consumer's parent device is also the
2160 * parent of the supplier, don't create a
2161 * consumer-supplier link from the parent to its child
2162 * device. Such a dependency is impossible.
2163 */
2164 if (con_dev &&
2165 fwnode_is_ancestor_of(con_dev->fwnode, fwnode)) {
2166 put_device(con_dev);
2167 con_dev = NULL;
2168 } else {
2169 own_link = false;
2170 }
2171 }
2172
2173 if (!con_dev)
2174 continue;
2175
2176 ret = fw_devlink_create_devlink(con_dev, fwnode, link);
2177 put_device(con_dev);
2178 if (!own_link || ret == -EAGAIN)
2179 continue;
2180
2181 __fwnode_link_del(link);
2182 }
2183}
2184
2185/**
2186 * __fw_devlink_link_to_suppliers - Create device links to suppliers of a device
2187 * @dev: The consumer device that needs to be linked to its suppliers
2188 * @fwnode: Root of the fwnode tree that is used to create device links
2189 *
2190 * This function looks at all the supplier fwnodes of fwnode tree rooted at
2191 * @fwnode and creates device links between @dev (consumer) and all the
2192 * supplier devices of the entire fwnode tree at @fwnode.
2193 *
2194 * The function creates normal (non-SYNC_STATE_ONLY) device links between @dev
2195 * and the real suppliers of @dev. Once these device links are created, the
2196 * fwnode links are deleted.
2197 *
2198 * In addition, it also looks at all the suppliers of the entire fwnode tree
2199 * because some of the child devices of @dev that have not been added yet
2200 * (because @dev hasn't probed) might already have their suppliers added to
2201 * driver core. So, this function creates SYNC_STATE_ONLY device links between
2202 * @dev (consumer) and these suppliers to make sure they don't execute their
2203 * sync_state() callbacks before these child devices have a chance to create
2204 * their device links. The fwnode links that correspond to the child devices
2205 * aren't delete because they are needed later to create the device links
2206 * between the real consumer and supplier devices.
2207 */
2208static void __fw_devlink_link_to_suppliers(struct device *dev,
2209 struct fwnode_handle *fwnode)
2210{
2211 bool own_link = (dev->fwnode == fwnode);
2212 struct fwnode_link *link, *tmp;
2213 struct fwnode_handle *child = NULL;
2214
2215 list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook) {
2216 int ret;
2217 struct fwnode_handle *sup = link->supplier;
2218
2219 ret = fw_devlink_create_devlink(dev, sup, link);
2220 if (!own_link || ret == -EAGAIN)
2221 continue;
2222
2223 __fwnode_link_del(link);
2224 }
2225
2226 /*
2227 * Make "proxy" SYNC_STATE_ONLY device links to represent the needs of
2228 * all the descendants. This proxy link step is needed to handle the
2229 * case where the supplier is added before the consumer's parent device
2230 * (@dev).
2231 */
2232 while ((child = fwnode_get_next_available_child_node(fwnode, child)))
2233 __fw_devlink_link_to_suppliers(dev, child);
2234}
2235
2236static void fw_devlink_link_device(struct device *dev)
2237{
2238 struct fwnode_handle *fwnode = dev->fwnode;
2239
2240 if (!fw_devlink_flags)
2241 return;
2242
2243 fw_devlink_parse_fwtree(fwnode);
2244
2245 mutex_lock(&fwnode_link_lock);
2246 __fw_devlink_link_to_consumers(dev);
2247 __fw_devlink_link_to_suppliers(dev, fwnode);
2248 mutex_unlock(&fwnode_link_lock);
2249}
2250
2251/* Device links support end. */
2252
2253int (*platform_notify)(struct device *dev) = NULL;
2254int (*platform_notify_remove)(struct device *dev) = NULL;
2255static struct kobject *dev_kobj;
2256
2257/* /sys/dev/char */
2258static struct kobject *sysfs_dev_char_kobj;
2259
2260/* /sys/dev/block */
2261static struct kobject *sysfs_dev_block_kobj;
2262
2263static DEFINE_MUTEX(device_hotplug_lock);
2264
2265void lock_device_hotplug(void)
2266{
2267 mutex_lock(&device_hotplug_lock);
2268}
2269
2270void unlock_device_hotplug(void)
2271{
2272 mutex_unlock(&device_hotplug_lock);
2273}
2274
2275int lock_device_hotplug_sysfs(void)
2276{
2277 if (mutex_trylock(&device_hotplug_lock))
2278 return 0;
2279
2280 /* Avoid busy looping (5 ms of sleep should do). */
2281 msleep(5);
2282 return restart_syscall();
2283}
2284
2285#ifdef CONFIG_BLOCK
2286static inline int device_is_not_partition(struct device *dev)
2287{
2288 return !(dev->type == &part_type);
2289}
2290#else
2291static inline int device_is_not_partition(struct device *dev)
2292{
2293 return 1;
2294}
2295#endif
2296
2297static void device_platform_notify(struct device *dev)
2298{
2299 acpi_device_notify(dev);
2300
2301 software_node_notify(dev);
2302
2303 if (platform_notify)
2304 platform_notify(dev);
2305}
2306
2307static void device_platform_notify_remove(struct device *dev)
2308{
2309 if (platform_notify_remove)
2310 platform_notify_remove(dev);
2311
2312 software_node_notify_remove(dev);
2313
2314 acpi_device_notify_remove(dev);
2315}
2316
2317/**
2318 * dev_driver_string - Return a device's driver name, if at all possible
2319 * @dev: struct device to get the name of
2320 *
2321 * Will return the device's driver's name if it is bound to a device. If
2322 * the device is not bound to a driver, it will return the name of the bus
2323 * it is attached to. If it is not attached to a bus either, an empty
2324 * string will be returned.
2325 */
2326const char *dev_driver_string(const struct device *dev)
2327{
2328 struct device_driver *drv;
2329
2330 /* dev->driver can change to NULL underneath us because of unbinding,
2331 * so be careful about accessing it. dev->bus and dev->class should
2332 * never change once they are set, so they don't need special care.
2333 */
2334 drv = READ_ONCE(dev->driver);
2335 return drv ? drv->name : dev_bus_name(dev);
2336}
2337EXPORT_SYMBOL(dev_driver_string);
2338
2339#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
2340
2341static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
2342 char *buf)
2343{
2344 struct device_attribute *dev_attr = to_dev_attr(attr);
2345 struct device *dev = kobj_to_dev(kobj);
2346 ssize_t ret = -EIO;
2347
2348 if (dev_attr->show)
2349 ret = dev_attr->show(dev, dev_attr, buf);
2350 if (ret >= (ssize_t)PAGE_SIZE) {
2351 printk("dev_attr_show: %pS returned bad count\n",
2352 dev_attr->show);
2353 }
2354 return ret;
2355}
2356
2357static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
2358 const char *buf, size_t count)
2359{
2360 struct device_attribute *dev_attr = to_dev_attr(attr);
2361 struct device *dev = kobj_to_dev(kobj);
2362 ssize_t ret = -EIO;
2363
2364 if (dev_attr->store)
2365 ret = dev_attr->store(dev, dev_attr, buf, count);
2366 return ret;
2367}
2368
2369static const struct sysfs_ops dev_sysfs_ops = {
2370 .show = dev_attr_show,
2371 .store = dev_attr_store,
2372};
2373
2374#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
2375
2376ssize_t device_store_ulong(struct device *dev,
2377 struct device_attribute *attr,
2378 const char *buf, size_t size)
2379{
2380 struct dev_ext_attribute *ea = to_ext_attr(attr);
2381 int ret;
2382 unsigned long new;
2383
2384 ret = kstrtoul(buf, 0, &new);
2385 if (ret)
2386 return ret;
2387 *(unsigned long *)(ea->var) = new;
2388 /* Always return full write size even if we didn't consume all */
2389 return size;
2390}
2391EXPORT_SYMBOL_GPL(device_store_ulong);
2392
2393ssize_t device_show_ulong(struct device *dev,
2394 struct device_attribute *attr,
2395 char *buf)
2396{
2397 struct dev_ext_attribute *ea = to_ext_attr(attr);
2398 return sysfs_emit(buf, "%lx\n", *(unsigned long *)(ea->var));
2399}
2400EXPORT_SYMBOL_GPL(device_show_ulong);
2401
2402ssize_t device_store_int(struct device *dev,
2403 struct device_attribute *attr,
2404 const char *buf, size_t size)
2405{
2406 struct dev_ext_attribute *ea = to_ext_attr(attr);
2407 int ret;
2408 long new;
2409
2410 ret = kstrtol(buf, 0, &new);
2411 if (ret)
2412 return ret;
2413
2414 if (new > INT_MAX || new < INT_MIN)
2415 return -EINVAL;
2416 *(int *)(ea->var) = new;
2417 /* Always return full write size even if we didn't consume all */
2418 return size;
2419}
2420EXPORT_SYMBOL_GPL(device_store_int);
2421
2422ssize_t device_show_int(struct device *dev,
2423 struct device_attribute *attr,
2424 char *buf)
2425{
2426 struct dev_ext_attribute *ea = to_ext_attr(attr);
2427
2428 return sysfs_emit(buf, "%d\n", *(int *)(ea->var));
2429}
2430EXPORT_SYMBOL_GPL(device_show_int);
2431
2432ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
2433 const char *buf, size_t size)
2434{
2435 struct dev_ext_attribute *ea = to_ext_attr(attr);
2436
2437 if (kstrtobool(buf, ea->var) < 0)
2438 return -EINVAL;
2439
2440 return size;
2441}
2442EXPORT_SYMBOL_GPL(device_store_bool);
2443
2444ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
2445 char *buf)
2446{
2447 struct dev_ext_attribute *ea = to_ext_attr(attr);
2448
2449 return sysfs_emit(buf, "%d\n", *(bool *)(ea->var));
2450}
2451EXPORT_SYMBOL_GPL(device_show_bool);
2452
2453/**
2454 * device_release - free device structure.
2455 * @kobj: device's kobject.
2456 *
2457 * This is called once the reference count for the object
2458 * reaches 0. We forward the call to the device's release
2459 * method, which should handle actually freeing the structure.
2460 */
2461static void device_release(struct kobject *kobj)
2462{
2463 struct device *dev = kobj_to_dev(kobj);
2464 struct device_private *p = dev->p;
2465
2466 /*
2467 * Some platform devices are driven without driver attached
2468 * and managed resources may have been acquired. Make sure
2469 * all resources are released.
2470 *
2471 * Drivers still can add resources into device after device
2472 * is deleted but alive, so release devres here to avoid
2473 * possible memory leak.
2474 */
2475 devres_release_all(dev);
2476
2477 kfree(dev->dma_range_map);
2478
2479 if (dev->release)
2480 dev->release(dev);
2481 else if (dev->type && dev->type->release)
2482 dev->type->release(dev);
2483 else if (dev->class && dev->class->dev_release)
2484 dev->class->dev_release(dev);
2485 else
2486 WARN(1, KERN_ERR "Device '%s' does not have a release() function, it is broken and must be fixed. See Documentation/core-api/kobject.rst.\n",
2487 dev_name(dev));
2488 kfree(p);
2489}
2490
2491static const void *device_namespace(const struct kobject *kobj)
2492{
2493 const struct device *dev = kobj_to_dev(kobj);
2494 const void *ns = NULL;
2495
2496 if (dev->class && dev->class->ns_type)
2497 ns = dev->class->namespace(dev);
2498
2499 return ns;
2500}
2501
2502static void device_get_ownership(const struct kobject *kobj, kuid_t *uid, kgid_t *gid)
2503{
2504 const struct device *dev = kobj_to_dev(kobj);
2505
2506 if (dev->class && dev->class->get_ownership)
2507 dev->class->get_ownership(dev, uid, gid);
2508}
2509
2510static const struct kobj_type device_ktype = {
2511 .release = device_release,
2512 .sysfs_ops = &dev_sysfs_ops,
2513 .namespace = device_namespace,
2514 .get_ownership = device_get_ownership,
2515};
2516
2517
2518static int dev_uevent_filter(const struct kobject *kobj)
2519{
2520 const struct kobj_type *ktype = get_ktype(kobj);
2521
2522 if (ktype == &device_ktype) {
2523 const struct device *dev = kobj_to_dev(kobj);
2524 if (dev->bus)
2525 return 1;
2526 if (dev->class)
2527 return 1;
2528 }
2529 return 0;
2530}
2531
2532static const char *dev_uevent_name(const struct kobject *kobj)
2533{
2534 const struct device *dev = kobj_to_dev(kobj);
2535
2536 if (dev->bus)
2537 return dev->bus->name;
2538 if (dev->class)
2539 return dev->class->name;
2540 return NULL;
2541}
2542
2543static int dev_uevent(const struct kobject *kobj, struct kobj_uevent_env *env)
2544{
2545 const struct device *dev = kobj_to_dev(kobj);
2546 int retval = 0;
2547
2548 /* add device node properties if present */
2549 if (MAJOR(dev->devt)) {
2550 const char *tmp;
2551 const char *name;
2552 umode_t mode = 0;
2553 kuid_t uid = GLOBAL_ROOT_UID;
2554 kgid_t gid = GLOBAL_ROOT_GID;
2555
2556 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
2557 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
2558 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
2559 if (name) {
2560 add_uevent_var(env, "DEVNAME=%s", name);
2561 if (mode)
2562 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
2563 if (!uid_eq(uid, GLOBAL_ROOT_UID))
2564 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
2565 if (!gid_eq(gid, GLOBAL_ROOT_GID))
2566 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
2567 kfree(tmp);
2568 }
2569 }
2570
2571 if (dev->type && dev->type->name)
2572 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
2573
2574 if (dev->driver)
2575 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
2576
2577 /* Add common DT information about the device */
2578 of_device_uevent(dev, env);
2579
2580 /* have the bus specific function add its stuff */
2581 if (dev->bus && dev->bus->uevent) {
2582 retval = dev->bus->uevent(dev, env);
2583 if (retval)
2584 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
2585 dev_name(dev), __func__, retval);
2586 }
2587
2588 /* have the class specific function add its stuff */
2589 if (dev->class && dev->class->dev_uevent) {
2590 retval = dev->class->dev_uevent(dev, env);
2591 if (retval)
2592 pr_debug("device: '%s': %s: class uevent() "
2593 "returned %d\n", dev_name(dev),
2594 __func__, retval);
2595 }
2596
2597 /* have the device type specific function add its stuff */
2598 if (dev->type && dev->type->uevent) {
2599 retval = dev->type->uevent(dev, env);
2600 if (retval)
2601 pr_debug("device: '%s': %s: dev_type uevent() "
2602 "returned %d\n", dev_name(dev),
2603 __func__, retval);
2604 }
2605
2606 return retval;
2607}
2608
2609static const struct kset_uevent_ops device_uevent_ops = {
2610 .filter = dev_uevent_filter,
2611 .name = dev_uevent_name,
2612 .uevent = dev_uevent,
2613};
2614
2615static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
2616 char *buf)
2617{
2618 struct kobject *top_kobj;
2619 struct kset *kset;
2620 struct kobj_uevent_env *env = NULL;
2621 int i;
2622 int len = 0;
2623 int retval;
2624
2625 /* search the kset, the device belongs to */
2626 top_kobj = &dev->kobj;
2627 while (!top_kobj->kset && top_kobj->parent)
2628 top_kobj = top_kobj->parent;
2629 if (!top_kobj->kset)
2630 goto out;
2631
2632 kset = top_kobj->kset;
2633 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
2634 goto out;
2635
2636 /* respect filter */
2637 if (kset->uevent_ops && kset->uevent_ops->filter)
2638 if (!kset->uevent_ops->filter(&dev->kobj))
2639 goto out;
2640
2641 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
2642 if (!env)
2643 return -ENOMEM;
2644
2645 /* let the kset specific function add its keys */
2646 retval = kset->uevent_ops->uevent(&dev->kobj, env);
2647 if (retval)
2648 goto out;
2649
2650 /* copy keys to file */
2651 for (i = 0; i < env->envp_idx; i++)
2652 len += sysfs_emit_at(buf, len, "%s\n", env->envp[i]);
2653out:
2654 kfree(env);
2655 return len;
2656}
2657
2658static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
2659 const char *buf, size_t count)
2660{
2661 int rc;
2662
2663 rc = kobject_synth_uevent(&dev->kobj, buf, count);
2664
2665 if (rc) {
2666 dev_err(dev, "uevent: failed to send synthetic uevent: %d\n", rc);
2667 return rc;
2668 }
2669
2670 return count;
2671}
2672static DEVICE_ATTR_RW(uevent);
2673
2674static ssize_t online_show(struct device *dev, struct device_attribute *attr,
2675 char *buf)
2676{
2677 bool val;
2678
2679 device_lock(dev);
2680 val = !dev->offline;
2681 device_unlock(dev);
2682 return sysfs_emit(buf, "%u\n", val);
2683}
2684
2685static ssize_t online_store(struct device *dev, struct device_attribute *attr,
2686 const char *buf, size_t count)
2687{
2688 bool val;
2689 int ret;
2690
2691 ret = kstrtobool(buf, &val);
2692 if (ret < 0)
2693 return ret;
2694
2695 ret = lock_device_hotplug_sysfs();
2696 if (ret)
2697 return ret;
2698
2699 ret = val ? device_online(dev) : device_offline(dev);
2700 unlock_device_hotplug();
2701 return ret < 0 ? ret : count;
2702}
2703static DEVICE_ATTR_RW(online);
2704
2705static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
2706 char *buf)
2707{
2708 const char *loc;
2709
2710 switch (dev->removable) {
2711 case DEVICE_REMOVABLE:
2712 loc = "removable";
2713 break;
2714 case DEVICE_FIXED:
2715 loc = "fixed";
2716 break;
2717 default:
2718 loc = "unknown";
2719 }
2720 return sysfs_emit(buf, "%s\n", loc);
2721}
2722static DEVICE_ATTR_RO(removable);
2723
2724int device_add_groups(struct device *dev, const struct attribute_group **groups)
2725{
2726 return sysfs_create_groups(&dev->kobj, groups);
2727}
2728EXPORT_SYMBOL_GPL(device_add_groups);
2729
2730void device_remove_groups(struct device *dev,
2731 const struct attribute_group **groups)
2732{
2733 sysfs_remove_groups(&dev->kobj, groups);
2734}
2735EXPORT_SYMBOL_GPL(device_remove_groups);
2736
2737union device_attr_group_devres {
2738 const struct attribute_group *group;
2739 const struct attribute_group **groups;
2740};
2741
2742static void devm_attr_group_remove(struct device *dev, void *res)
2743{
2744 union device_attr_group_devres *devres = res;
2745 const struct attribute_group *group = devres->group;
2746
2747 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
2748 sysfs_remove_group(&dev->kobj, group);
2749}
2750
2751static void devm_attr_groups_remove(struct device *dev, void *res)
2752{
2753 union device_attr_group_devres *devres = res;
2754 const struct attribute_group **groups = devres->groups;
2755
2756 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
2757 sysfs_remove_groups(&dev->kobj, groups);
2758}
2759
2760/**
2761 * devm_device_add_group - given a device, create a managed attribute group
2762 * @dev: The device to create the group for
2763 * @grp: The attribute group to create
2764 *
2765 * This function creates a group for the first time. It will explicitly
2766 * warn and error if any of the attribute files being created already exist.
2767 *
2768 * Returns 0 on success or error code on failure.
2769 */
2770int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
2771{
2772 union device_attr_group_devres *devres;
2773 int error;
2774
2775 devres = devres_alloc(devm_attr_group_remove,
2776 sizeof(*devres), GFP_KERNEL);
2777 if (!devres)
2778 return -ENOMEM;
2779
2780 error = sysfs_create_group(&dev->kobj, grp);
2781 if (error) {
2782 devres_free(devres);
2783 return error;
2784 }
2785
2786 devres->group = grp;
2787 devres_add(dev, devres);
2788 return 0;
2789}
2790EXPORT_SYMBOL_GPL(devm_device_add_group);
2791
2792/**
2793 * devm_device_add_groups - create a bunch of managed attribute groups
2794 * @dev: The device to create the group for
2795 * @groups: The attribute groups to create, NULL terminated
2796 *
2797 * This function creates a bunch of managed attribute groups. If an error
2798 * occurs when creating a group, all previously created groups will be
2799 * removed, unwinding everything back to the original state when this
2800 * function was called. It will explicitly warn and error if any of the
2801 * attribute files being created already exist.
2802 *
2803 * Returns 0 on success or error code from sysfs_create_group on failure.
2804 */
2805int devm_device_add_groups(struct device *dev,
2806 const struct attribute_group **groups)
2807{
2808 union device_attr_group_devres *devres;
2809 int error;
2810
2811 devres = devres_alloc(devm_attr_groups_remove,
2812 sizeof(*devres), GFP_KERNEL);
2813 if (!devres)
2814 return -ENOMEM;
2815
2816 error = sysfs_create_groups(&dev->kobj, groups);
2817 if (error) {
2818 devres_free(devres);
2819 return error;
2820 }
2821
2822 devres->groups = groups;
2823 devres_add(dev, devres);
2824 return 0;
2825}
2826EXPORT_SYMBOL_GPL(devm_device_add_groups);
2827
2828static int device_add_attrs(struct device *dev)
2829{
2830 const struct class *class = dev->class;
2831 const struct device_type *type = dev->type;
2832 int error;
2833
2834 if (class) {
2835 error = device_add_groups(dev, class->dev_groups);
2836 if (error)
2837 return error;
2838 }
2839
2840 if (type) {
2841 error = device_add_groups(dev, type->groups);
2842 if (error)
2843 goto err_remove_class_groups;
2844 }
2845
2846 error = device_add_groups(dev, dev->groups);
2847 if (error)
2848 goto err_remove_type_groups;
2849
2850 if (device_supports_offline(dev) && !dev->offline_disabled) {
2851 error = device_create_file(dev, &dev_attr_online);
2852 if (error)
2853 goto err_remove_dev_groups;
2854 }
2855
2856 if (fw_devlink_flags && !fw_devlink_is_permissive() && dev->fwnode) {
2857 error = device_create_file(dev, &dev_attr_waiting_for_supplier);
2858 if (error)
2859 goto err_remove_dev_online;
2860 }
2861
2862 if (dev_removable_is_valid(dev)) {
2863 error = device_create_file(dev, &dev_attr_removable);
2864 if (error)
2865 goto err_remove_dev_waiting_for_supplier;
2866 }
2867
2868 if (dev_add_physical_location(dev)) {
2869 error = device_add_group(dev,
2870 &dev_attr_physical_location_group);
2871 if (error)
2872 goto err_remove_dev_removable;
2873 }
2874
2875 return 0;
2876
2877 err_remove_dev_removable:
2878 device_remove_file(dev, &dev_attr_removable);
2879 err_remove_dev_waiting_for_supplier:
2880 device_remove_file(dev, &dev_attr_waiting_for_supplier);
2881 err_remove_dev_online:
2882 device_remove_file(dev, &dev_attr_online);
2883 err_remove_dev_groups:
2884 device_remove_groups(dev, dev->groups);
2885 err_remove_type_groups:
2886 if (type)
2887 device_remove_groups(dev, type->groups);
2888 err_remove_class_groups:
2889 if (class)
2890 device_remove_groups(dev, class->dev_groups);
2891
2892 return error;
2893}
2894
2895static void device_remove_attrs(struct device *dev)
2896{
2897 const struct class *class = dev->class;
2898 const struct device_type *type = dev->type;
2899
2900 if (dev->physical_location) {
2901 device_remove_group(dev, &dev_attr_physical_location_group);
2902 kfree(dev->physical_location);
2903 }
2904
2905 device_remove_file(dev, &dev_attr_removable);
2906 device_remove_file(dev, &dev_attr_waiting_for_supplier);
2907 device_remove_file(dev, &dev_attr_online);
2908 device_remove_groups(dev, dev->groups);
2909
2910 if (type)
2911 device_remove_groups(dev, type->groups);
2912
2913 if (class)
2914 device_remove_groups(dev, class->dev_groups);
2915}
2916
2917static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
2918 char *buf)
2919{
2920 return print_dev_t(buf, dev->devt);
2921}
2922static DEVICE_ATTR_RO(dev);
2923
2924/* /sys/devices/ */
2925struct kset *devices_kset;
2926
2927/**
2928 * devices_kset_move_before - Move device in the devices_kset's list.
2929 * @deva: Device to move.
2930 * @devb: Device @deva should come before.
2931 */
2932static void devices_kset_move_before(struct device *deva, struct device *devb)
2933{
2934 if (!devices_kset)
2935 return;
2936 pr_debug("devices_kset: Moving %s before %s\n",
2937 dev_name(deva), dev_name(devb));
2938 spin_lock(&devices_kset->list_lock);
2939 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
2940 spin_unlock(&devices_kset->list_lock);
2941}
2942
2943/**
2944 * devices_kset_move_after - Move device in the devices_kset's list.
2945 * @deva: Device to move
2946 * @devb: Device @deva should come after.
2947 */
2948static void devices_kset_move_after(struct device *deva, struct device *devb)
2949{
2950 if (!devices_kset)
2951 return;
2952 pr_debug("devices_kset: Moving %s after %s\n",
2953 dev_name(deva), dev_name(devb));
2954 spin_lock(&devices_kset->list_lock);
2955 list_move(&deva->kobj.entry, &devb->kobj.entry);
2956 spin_unlock(&devices_kset->list_lock);
2957}
2958
2959/**
2960 * devices_kset_move_last - move the device to the end of devices_kset's list.
2961 * @dev: device to move
2962 */
2963void devices_kset_move_last(struct device *dev)
2964{
2965 if (!devices_kset)
2966 return;
2967 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
2968 spin_lock(&devices_kset->list_lock);
2969 list_move_tail(&dev->kobj.entry, &devices_kset->list);
2970 spin_unlock(&devices_kset->list_lock);
2971}
2972
2973/**
2974 * device_create_file - create sysfs attribute file for device.
2975 * @dev: device.
2976 * @attr: device attribute descriptor.
2977 */
2978int device_create_file(struct device *dev,
2979 const struct device_attribute *attr)
2980{
2981 int error = 0;
2982
2983 if (dev) {
2984 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
2985 "Attribute %s: write permission without 'store'\n",
2986 attr->attr.name);
2987 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
2988 "Attribute %s: read permission without 'show'\n",
2989 attr->attr.name);
2990 error = sysfs_create_file(&dev->kobj, &attr->attr);
2991 }
2992
2993 return error;
2994}
2995EXPORT_SYMBOL_GPL(device_create_file);
2996
2997/**
2998 * device_remove_file - remove sysfs attribute file.
2999 * @dev: device.
3000 * @attr: device attribute descriptor.
3001 */
3002void device_remove_file(struct device *dev,
3003 const struct device_attribute *attr)
3004{
3005 if (dev)
3006 sysfs_remove_file(&dev->kobj, &attr->attr);
3007}
3008EXPORT_SYMBOL_GPL(device_remove_file);
3009
3010/**
3011 * device_remove_file_self - remove sysfs attribute file from its own method.
3012 * @dev: device.
3013 * @attr: device attribute descriptor.
3014 *
3015 * See kernfs_remove_self() for details.
3016 */
3017bool device_remove_file_self(struct device *dev,
3018 const struct device_attribute *attr)
3019{
3020 if (dev)
3021 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
3022 else
3023 return false;
3024}
3025EXPORT_SYMBOL_GPL(device_remove_file_self);
3026
3027/**
3028 * device_create_bin_file - create sysfs binary attribute file for device.
3029 * @dev: device.
3030 * @attr: device binary attribute descriptor.
3031 */
3032int device_create_bin_file(struct device *dev,
3033 const struct bin_attribute *attr)
3034{
3035 int error = -EINVAL;
3036 if (dev)
3037 error = sysfs_create_bin_file(&dev->kobj, attr);
3038 return error;
3039}
3040EXPORT_SYMBOL_GPL(device_create_bin_file);
3041
3042/**
3043 * device_remove_bin_file - remove sysfs binary attribute file
3044 * @dev: device.
3045 * @attr: device binary attribute descriptor.
3046 */
3047void device_remove_bin_file(struct device *dev,
3048 const struct bin_attribute *attr)
3049{
3050 if (dev)
3051 sysfs_remove_bin_file(&dev->kobj, attr);
3052}
3053EXPORT_SYMBOL_GPL(device_remove_bin_file);
3054
3055static void klist_children_get(struct klist_node *n)
3056{
3057 struct device_private *p = to_device_private_parent(n);
3058 struct device *dev = p->device;
3059
3060 get_device(dev);
3061}
3062
3063static void klist_children_put(struct klist_node *n)
3064{
3065 struct device_private *p = to_device_private_parent(n);
3066 struct device *dev = p->device;
3067
3068 put_device(dev);
3069}
3070
3071/**
3072 * device_initialize - init device structure.
3073 * @dev: device.
3074 *
3075 * This prepares the device for use by other layers by initializing
3076 * its fields.
3077 * It is the first half of device_register(), if called by
3078 * that function, though it can also be called separately, so one
3079 * may use @dev's fields. In particular, get_device()/put_device()
3080 * may be used for reference counting of @dev after calling this
3081 * function.
3082 *
3083 * All fields in @dev must be initialized by the caller to 0, except
3084 * for those explicitly set to some other value. The simplest
3085 * approach is to use kzalloc() to allocate the structure containing
3086 * @dev.
3087 *
3088 * NOTE: Use put_device() to give up your reference instead of freeing
3089 * @dev directly once you have called this function.
3090 */
3091void device_initialize(struct device *dev)
3092{
3093 dev->kobj.kset = devices_kset;
3094 kobject_init(&dev->kobj, &device_ktype);
3095 INIT_LIST_HEAD(&dev->dma_pools);
3096 mutex_init(&dev->mutex);
3097 lockdep_set_novalidate_class(&dev->mutex);
3098 spin_lock_init(&dev->devres_lock);
3099 INIT_LIST_HEAD(&dev->devres_head);
3100 device_pm_init(dev);
3101 set_dev_node(dev, NUMA_NO_NODE);
3102 INIT_LIST_HEAD(&dev->links.consumers);
3103 INIT_LIST_HEAD(&dev->links.suppliers);
3104 INIT_LIST_HEAD(&dev->links.defer_sync);
3105 dev->links.status = DL_DEV_NO_DRIVER;
3106#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
3107 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
3108 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
3109 dev->dma_coherent = dma_default_coherent;
3110#endif
3111 swiotlb_dev_init(dev);
3112}
3113EXPORT_SYMBOL_GPL(device_initialize);
3114
3115struct kobject *virtual_device_parent(struct device *dev)
3116{
3117 static struct kobject *virtual_dir = NULL;
3118
3119 if (!virtual_dir)
3120 virtual_dir = kobject_create_and_add("virtual",
3121 &devices_kset->kobj);
3122
3123 return virtual_dir;
3124}
3125
3126struct class_dir {
3127 struct kobject kobj;
3128 const struct class *class;
3129};
3130
3131#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
3132
3133static void class_dir_release(struct kobject *kobj)
3134{
3135 struct class_dir *dir = to_class_dir(kobj);
3136 kfree(dir);
3137}
3138
3139static const
3140struct kobj_ns_type_operations *class_dir_child_ns_type(const struct kobject *kobj)
3141{
3142 const struct class_dir *dir = to_class_dir(kobj);
3143 return dir->class->ns_type;
3144}
3145
3146static const struct kobj_type class_dir_ktype = {
3147 .release = class_dir_release,
3148 .sysfs_ops = &kobj_sysfs_ops,
3149 .child_ns_type = class_dir_child_ns_type
3150};
3151
3152static struct kobject *class_dir_create_and_add(struct subsys_private *sp,
3153 struct kobject *parent_kobj)
3154{
3155 struct class_dir *dir;
3156 int retval;
3157
3158 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
3159 if (!dir)
3160 return ERR_PTR(-ENOMEM);
3161
3162 dir->class = sp->class;
3163 kobject_init(&dir->kobj, &class_dir_ktype);
3164
3165 dir->kobj.kset = &sp->glue_dirs;
3166
3167 retval = kobject_add(&dir->kobj, parent_kobj, "%s", sp->class->name);
3168 if (retval < 0) {
3169 kobject_put(&dir->kobj);
3170 return ERR_PTR(retval);
3171 }
3172 return &dir->kobj;
3173}
3174
3175static DEFINE_MUTEX(gdp_mutex);
3176
3177static struct kobject *get_device_parent(struct device *dev,
3178 struct device *parent)
3179{
3180 struct subsys_private *sp = class_to_subsys(dev->class);
3181 struct kobject *kobj = NULL;
3182
3183 if (sp) {
3184 struct kobject *parent_kobj;
3185 struct kobject *k;
3186
3187 /*
3188 * If we have no parent, we live in "virtual".
3189 * Class-devices with a non class-device as parent, live
3190 * in a "glue" directory to prevent namespace collisions.
3191 */
3192 if (parent == NULL)
3193 parent_kobj = virtual_device_parent(dev);
3194 else if (parent->class && !dev->class->ns_type) {
3195 subsys_put(sp);
3196 return &parent->kobj;
3197 } else {
3198 parent_kobj = &parent->kobj;
3199 }
3200
3201 mutex_lock(&gdp_mutex);
3202
3203 /* find our class-directory at the parent and reference it */
3204 spin_lock(&sp->glue_dirs.list_lock);
3205 list_for_each_entry(k, &sp->glue_dirs.list, entry)
3206 if (k->parent == parent_kobj) {
3207 kobj = kobject_get(k);
3208 break;
3209 }
3210 spin_unlock(&sp->glue_dirs.list_lock);
3211 if (kobj) {
3212 mutex_unlock(&gdp_mutex);
3213 subsys_put(sp);
3214 return kobj;
3215 }
3216
3217 /* or create a new class-directory at the parent device */
3218 k = class_dir_create_and_add(sp, parent_kobj);
3219 /* do not emit an uevent for this simple "glue" directory */
3220 mutex_unlock(&gdp_mutex);
3221 subsys_put(sp);
3222 return k;
3223 }
3224
3225 /* subsystems can specify a default root directory for their devices */
3226 if (!parent && dev->bus) {
3227 struct device *dev_root = bus_get_dev_root(dev->bus);
3228
3229 if (dev_root) {
3230 kobj = &dev_root->kobj;
3231 put_device(dev_root);
3232 return kobj;
3233 }
3234 }
3235
3236 if (parent)
3237 return &parent->kobj;
3238 return NULL;
3239}
3240
3241static inline bool live_in_glue_dir(struct kobject *kobj,
3242 struct device *dev)
3243{
3244 struct subsys_private *sp;
3245 bool retval;
3246
3247 if (!kobj || !dev->class)
3248 return false;
3249
3250 sp = class_to_subsys(dev->class);
3251 if (!sp)
3252 return false;
3253
3254 if (kobj->kset == &sp->glue_dirs)
3255 retval = true;
3256 else
3257 retval = false;
3258
3259 subsys_put(sp);
3260 return retval;
3261}
3262
3263static inline struct kobject *get_glue_dir(struct device *dev)
3264{
3265 return dev->kobj.parent;
3266}
3267
3268/**
3269 * kobject_has_children - Returns whether a kobject has children.
3270 * @kobj: the object to test
3271 *
3272 * This will return whether a kobject has other kobjects as children.
3273 *
3274 * It does NOT account for the presence of attribute files, only sub
3275 * directories. It also assumes there is no concurrent addition or
3276 * removal of such children, and thus relies on external locking.
3277 */
3278static inline bool kobject_has_children(struct kobject *kobj)
3279{
3280 WARN_ON_ONCE(kref_read(&kobj->kref) == 0);
3281
3282 return kobj->sd && kobj->sd->dir.subdirs;
3283}
3284
3285/*
3286 * make sure cleaning up dir as the last step, we need to make
3287 * sure .release handler of kobject is run with holding the
3288 * global lock
3289 */
3290static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
3291{
3292 unsigned int ref;
3293
3294 /* see if we live in a "glue" directory */
3295 if (!live_in_glue_dir(glue_dir, dev))
3296 return;
3297
3298 mutex_lock(&gdp_mutex);
3299 /**
3300 * There is a race condition between removing glue directory
3301 * and adding a new device under the glue directory.
3302 *
3303 * CPU1: CPU2:
3304 *
3305 * device_add()
3306 * get_device_parent()
3307 * class_dir_create_and_add()
3308 * kobject_add_internal()
3309 * create_dir() // create glue_dir
3310 *
3311 * device_add()
3312 * get_device_parent()
3313 * kobject_get() // get glue_dir
3314 *
3315 * device_del()
3316 * cleanup_glue_dir()
3317 * kobject_del(glue_dir)
3318 *
3319 * kobject_add()
3320 * kobject_add_internal()
3321 * create_dir() // in glue_dir
3322 * sysfs_create_dir_ns()
3323 * kernfs_create_dir_ns(sd)
3324 *
3325 * sysfs_remove_dir() // glue_dir->sd=NULL
3326 * sysfs_put() // free glue_dir->sd
3327 *
3328 * // sd is freed
3329 * kernfs_new_node(sd)
3330 * kernfs_get(glue_dir)
3331 * kernfs_add_one()
3332 * kernfs_put()
3333 *
3334 * Before CPU1 remove last child device under glue dir, if CPU2 add
3335 * a new device under glue dir, the glue_dir kobject reference count
3336 * will be increase to 2 in kobject_get(k). And CPU2 has been called
3337 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
3338 * and sysfs_put(). This result in glue_dir->sd is freed.
3339 *
3340 * Then the CPU2 will see a stale "empty" but still potentially used
3341 * glue dir around in kernfs_new_node().
3342 *
3343 * In order to avoid this happening, we also should make sure that
3344 * kernfs_node for glue_dir is released in CPU1 only when refcount
3345 * for glue_dir kobj is 1.
3346 */
3347 ref = kref_read(&glue_dir->kref);
3348 if (!kobject_has_children(glue_dir) && !--ref)
3349 kobject_del(glue_dir);
3350 kobject_put(glue_dir);
3351 mutex_unlock(&gdp_mutex);
3352}
3353
3354static int device_add_class_symlinks(struct device *dev)
3355{
3356 struct device_node *of_node = dev_of_node(dev);
3357 struct subsys_private *sp;
3358 int error;
3359
3360 if (of_node) {
3361 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
3362 if (error)
3363 dev_warn(dev, "Error %d creating of_node link\n",error);
3364 /* An error here doesn't warrant bringing down the device */
3365 }
3366
3367 sp = class_to_subsys(dev->class);
3368 if (!sp)
3369 return 0;
3370
3371 error = sysfs_create_link(&dev->kobj, &sp->subsys.kobj, "subsystem");
3372 if (error)
3373 goto out_devnode;
3374
3375 if (dev->parent && device_is_not_partition(dev)) {
3376 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
3377 "device");
3378 if (error)
3379 goto out_subsys;
3380 }
3381
3382 /* link in the class directory pointing to the device */
3383 error = sysfs_create_link(&sp->subsys.kobj, &dev->kobj, dev_name(dev));
3384 if (error)
3385 goto out_device;
3386 goto exit;
3387
3388out_device:
3389 sysfs_remove_link(&dev->kobj, "device");
3390out_subsys:
3391 sysfs_remove_link(&dev->kobj, "subsystem");
3392out_devnode:
3393 sysfs_remove_link(&dev->kobj, "of_node");
3394exit:
3395 subsys_put(sp);
3396 return error;
3397}
3398
3399static void device_remove_class_symlinks(struct device *dev)
3400{
3401 struct subsys_private *sp = class_to_subsys(dev->class);
3402
3403 if (dev_of_node(dev))
3404 sysfs_remove_link(&dev->kobj, "of_node");
3405
3406 if (!sp)
3407 return;
3408
3409 if (dev->parent && device_is_not_partition(dev))
3410 sysfs_remove_link(&dev->kobj, "device");
3411 sysfs_remove_link(&dev->kobj, "subsystem");
3412 sysfs_delete_link(&sp->subsys.kobj, &dev->kobj, dev_name(dev));
3413 subsys_put(sp);
3414}
3415
3416/**
3417 * dev_set_name - set a device name
3418 * @dev: device
3419 * @fmt: format string for the device's name
3420 */
3421int dev_set_name(struct device *dev, const char *fmt, ...)
3422{
3423 va_list vargs;
3424 int err;
3425
3426 va_start(vargs, fmt);
3427 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
3428 va_end(vargs);
3429 return err;
3430}
3431EXPORT_SYMBOL_GPL(dev_set_name);
3432
3433/* select a /sys/dev/ directory for the device */
3434static struct kobject *device_to_dev_kobj(struct device *dev)
3435{
3436 if (is_blockdev(dev))
3437 return sysfs_dev_block_kobj;
3438 else
3439 return sysfs_dev_char_kobj;
3440}
3441
3442static int device_create_sys_dev_entry(struct device *dev)
3443{
3444 struct kobject *kobj = device_to_dev_kobj(dev);
3445 int error = 0;
3446 char devt_str[15];
3447
3448 if (kobj) {
3449 format_dev_t(devt_str, dev->devt);
3450 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
3451 }
3452
3453 return error;
3454}
3455
3456static void device_remove_sys_dev_entry(struct device *dev)
3457{
3458 struct kobject *kobj = device_to_dev_kobj(dev);
3459 char devt_str[15];
3460
3461 if (kobj) {
3462 format_dev_t(devt_str, dev->devt);
3463 sysfs_remove_link(kobj, devt_str);
3464 }
3465}
3466
3467static int device_private_init(struct device *dev)
3468{
3469 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
3470 if (!dev->p)
3471 return -ENOMEM;
3472 dev->p->device = dev;
3473 klist_init(&dev->p->klist_children, klist_children_get,
3474 klist_children_put);
3475 INIT_LIST_HEAD(&dev->p->deferred_probe);
3476 return 0;
3477}
3478
3479/**
3480 * device_add - add device to device hierarchy.
3481 * @dev: device.
3482 *
3483 * This is part 2 of device_register(), though may be called
3484 * separately _iff_ device_initialize() has been called separately.
3485 *
3486 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
3487 * to the global and sibling lists for the device, then
3488 * adds it to the other relevant subsystems of the driver model.
3489 *
3490 * Do not call this routine or device_register() more than once for
3491 * any device structure. The driver model core is not designed to work
3492 * with devices that get unregistered and then spring back to life.
3493 * (Among other things, it's very hard to guarantee that all references
3494 * to the previous incarnation of @dev have been dropped.) Allocate
3495 * and register a fresh new struct device instead.
3496 *
3497 * NOTE: _Never_ directly free @dev after calling this function, even
3498 * if it returned an error! Always use put_device() to give up your
3499 * reference instead.
3500 *
3501 * Rule of thumb is: if device_add() succeeds, you should call
3502 * device_del() when you want to get rid of it. If device_add() has
3503 * *not* succeeded, use *only* put_device() to drop the reference
3504 * count.
3505 */
3506int device_add(struct device *dev)
3507{
3508 struct subsys_private *sp;
3509 struct device *parent;
3510 struct kobject *kobj;
3511 struct class_interface *class_intf;
3512 int error = -EINVAL;
3513 struct kobject *glue_dir = NULL;
3514
3515 dev = get_device(dev);
3516 if (!dev)
3517 goto done;
3518
3519 if (!dev->p) {
3520 error = device_private_init(dev);
3521 if (error)
3522 goto done;
3523 }
3524
3525 /*
3526 * for statically allocated devices, which should all be converted
3527 * some day, we need to initialize the name. We prevent reading back
3528 * the name, and force the use of dev_name()
3529 */
3530 if (dev->init_name) {
3531 error = dev_set_name(dev, "%s", dev->init_name);
3532 dev->init_name = NULL;
3533 }
3534
3535 if (dev_name(dev))
3536 error = 0;
3537 /* subsystems can specify simple device enumeration */
3538 else if (dev->bus && dev->bus->dev_name)
3539 error = dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
3540 if (error)
3541 goto name_error;
3542
3543 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3544
3545 parent = get_device(dev->parent);
3546 kobj = get_device_parent(dev, parent);
3547 if (IS_ERR(kobj)) {
3548 error = PTR_ERR(kobj);
3549 goto parent_error;
3550 }
3551 if (kobj)
3552 dev->kobj.parent = kobj;
3553
3554 /* use parent numa_node */
3555 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
3556 set_dev_node(dev, dev_to_node(parent));
3557
3558 /* first, register with generic layer. */
3559 /* we require the name to be set before, and pass NULL */
3560 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
3561 if (error) {
3562 glue_dir = kobj;
3563 goto Error;
3564 }
3565
3566 /* notify platform of device entry */
3567 device_platform_notify(dev);
3568
3569 error = device_create_file(dev, &dev_attr_uevent);
3570 if (error)
3571 goto attrError;
3572
3573 error = device_add_class_symlinks(dev);
3574 if (error)
3575 goto SymlinkError;
3576 error = device_add_attrs(dev);
3577 if (error)
3578 goto AttrsError;
3579 error = bus_add_device(dev);
3580 if (error)
3581 goto BusError;
3582 error = dpm_sysfs_add(dev);
3583 if (error)
3584 goto DPMError;
3585 device_pm_add(dev);
3586
3587 if (MAJOR(dev->devt)) {
3588 error = device_create_file(dev, &dev_attr_dev);
3589 if (error)
3590 goto DevAttrError;
3591
3592 error = device_create_sys_dev_entry(dev);
3593 if (error)
3594 goto SysEntryError;
3595
3596 devtmpfs_create_node(dev);
3597 }
3598
3599 /* Notify clients of device addition. This call must come
3600 * after dpm_sysfs_add() and before kobject_uevent().
3601 */
3602 bus_notify(dev, BUS_NOTIFY_ADD_DEVICE);
3603 kobject_uevent(&dev->kobj, KOBJ_ADD);
3604
3605 /*
3606 * Check if any of the other devices (consumers) have been waiting for
3607 * this device (supplier) to be added so that they can create a device
3608 * link to it.
3609 *
3610 * This needs to happen after device_pm_add() because device_link_add()
3611 * requires the supplier be registered before it's called.
3612 *
3613 * But this also needs to happen before bus_probe_device() to make sure
3614 * waiting consumers can link to it before the driver is bound to the
3615 * device and the driver sync_state callback is called for this device.
3616 */
3617 if (dev->fwnode && !dev->fwnode->dev) {
3618 dev->fwnode->dev = dev;
3619 fw_devlink_link_device(dev);
3620 }
3621
3622 bus_probe_device(dev);
3623
3624 /*
3625 * If all driver registration is done and a newly added device doesn't
3626 * match with any driver, don't block its consumers from probing in
3627 * case the consumer device is able to operate without this supplier.
3628 */
3629 if (dev->fwnode && fw_devlink_drv_reg_done && !dev->can_match)
3630 fw_devlink_unblock_consumers(dev);
3631
3632 if (parent)
3633 klist_add_tail(&dev->p->knode_parent,
3634 &parent->p->klist_children);
3635
3636 sp = class_to_subsys(dev->class);
3637 if (sp) {
3638 mutex_lock(&sp->mutex);
3639 /* tie the class to the device */
3640 klist_add_tail(&dev->p->knode_class, &sp->klist_devices);
3641
3642 /* notify any interfaces that the device is here */
3643 list_for_each_entry(class_intf, &sp->interfaces, node)
3644 if (class_intf->add_dev)
3645 class_intf->add_dev(dev);
3646 mutex_unlock(&sp->mutex);
3647 subsys_put(sp);
3648 }
3649done:
3650 put_device(dev);
3651 return error;
3652 SysEntryError:
3653 if (MAJOR(dev->devt))
3654 device_remove_file(dev, &dev_attr_dev);
3655 DevAttrError:
3656 device_pm_remove(dev);
3657 dpm_sysfs_remove(dev);
3658 DPMError:
3659 dev->driver = NULL;
3660 bus_remove_device(dev);
3661 BusError:
3662 device_remove_attrs(dev);
3663 AttrsError:
3664 device_remove_class_symlinks(dev);
3665 SymlinkError:
3666 device_remove_file(dev, &dev_attr_uevent);
3667 attrError:
3668 device_platform_notify_remove(dev);
3669 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
3670 glue_dir = get_glue_dir(dev);
3671 kobject_del(&dev->kobj);
3672 Error:
3673 cleanup_glue_dir(dev, glue_dir);
3674parent_error:
3675 put_device(parent);
3676name_error:
3677 kfree(dev->p);
3678 dev->p = NULL;
3679 goto done;
3680}
3681EXPORT_SYMBOL_GPL(device_add);
3682
3683/**
3684 * device_register - register a device with the system.
3685 * @dev: pointer to the device structure
3686 *
3687 * This happens in two clean steps - initialize the device
3688 * and add it to the system. The two steps can be called
3689 * separately, but this is the easiest and most common.
3690 * I.e. you should only call the two helpers separately if
3691 * have a clearly defined need to use and refcount the device
3692 * before it is added to the hierarchy.
3693 *
3694 * For more information, see the kerneldoc for device_initialize()
3695 * and device_add().
3696 *
3697 * NOTE: _Never_ directly free @dev after calling this function, even
3698 * if it returned an error! Always use put_device() to give up the
3699 * reference initialized in this function instead.
3700 */
3701int device_register(struct device *dev)
3702{
3703 device_initialize(dev);
3704 return device_add(dev);
3705}
3706EXPORT_SYMBOL_GPL(device_register);
3707
3708/**
3709 * get_device - increment reference count for device.
3710 * @dev: device.
3711 *
3712 * This simply forwards the call to kobject_get(), though
3713 * we do take care to provide for the case that we get a NULL
3714 * pointer passed in.
3715 */
3716struct device *get_device(struct device *dev)
3717{
3718 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
3719}
3720EXPORT_SYMBOL_GPL(get_device);
3721
3722/**
3723 * put_device - decrement reference count.
3724 * @dev: device in question.
3725 */
3726void put_device(struct device *dev)
3727{
3728 /* might_sleep(); */
3729 if (dev)
3730 kobject_put(&dev->kobj);
3731}
3732EXPORT_SYMBOL_GPL(put_device);
3733
3734bool kill_device(struct device *dev)
3735{
3736 /*
3737 * Require the device lock and set the "dead" flag to guarantee that
3738 * the update behavior is consistent with the other bitfields near
3739 * it and that we cannot have an asynchronous probe routine trying
3740 * to run while we are tearing out the bus/class/sysfs from
3741 * underneath the device.
3742 */
3743 device_lock_assert(dev);
3744
3745 if (dev->p->dead)
3746 return false;
3747 dev->p->dead = true;
3748 return true;
3749}
3750EXPORT_SYMBOL_GPL(kill_device);
3751
3752/**
3753 * device_del - delete device from system.
3754 * @dev: device.
3755 *
3756 * This is the first part of the device unregistration
3757 * sequence. This removes the device from the lists we control
3758 * from here, has it removed from the other driver model
3759 * subsystems it was added to in device_add(), and removes it
3760 * from the kobject hierarchy.
3761 *
3762 * NOTE: this should be called manually _iff_ device_add() was
3763 * also called manually.
3764 */
3765void device_del(struct device *dev)
3766{
3767 struct subsys_private *sp;
3768 struct device *parent = dev->parent;
3769 struct kobject *glue_dir = NULL;
3770 struct class_interface *class_intf;
3771 unsigned int noio_flag;
3772
3773 device_lock(dev);
3774 kill_device(dev);
3775 device_unlock(dev);
3776
3777 if (dev->fwnode && dev->fwnode->dev == dev)
3778 dev->fwnode->dev = NULL;
3779
3780 /* Notify clients of device removal. This call must come
3781 * before dpm_sysfs_remove().
3782 */
3783 noio_flag = memalloc_noio_save();
3784 bus_notify(dev, BUS_NOTIFY_DEL_DEVICE);
3785
3786 dpm_sysfs_remove(dev);
3787 if (parent)
3788 klist_del(&dev->p->knode_parent);
3789 if (MAJOR(dev->devt)) {
3790 devtmpfs_delete_node(dev);
3791 device_remove_sys_dev_entry(dev);
3792 device_remove_file(dev, &dev_attr_dev);
3793 }
3794
3795 sp = class_to_subsys(dev->class);
3796 if (sp) {
3797 device_remove_class_symlinks(dev);
3798
3799 mutex_lock(&sp->mutex);
3800 /* notify any interfaces that the device is now gone */
3801 list_for_each_entry(class_intf, &sp->interfaces, node)
3802 if (class_intf->remove_dev)
3803 class_intf->remove_dev(dev);
3804 /* remove the device from the class list */
3805 klist_del(&dev->p->knode_class);
3806 mutex_unlock(&sp->mutex);
3807 subsys_put(sp);
3808 }
3809 device_remove_file(dev, &dev_attr_uevent);
3810 device_remove_attrs(dev);
3811 bus_remove_device(dev);
3812 device_pm_remove(dev);
3813 driver_deferred_probe_del(dev);
3814 device_platform_notify_remove(dev);
3815 device_links_purge(dev);
3816
3817 /*
3818 * If a device does not have a driver attached, we need to clean
3819 * up any managed resources. We do this in device_release(), but
3820 * it's never called (and we leak the device) if a managed
3821 * resource holds a reference to the device. So release all
3822 * managed resources here, like we do in driver_detach(). We
3823 * still need to do so again in device_release() in case someone
3824 * adds a new resource after this point, though.
3825 */
3826 devres_release_all(dev);
3827
3828 bus_notify(dev, BUS_NOTIFY_REMOVED_DEVICE);
3829 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
3830 glue_dir = get_glue_dir(dev);
3831 kobject_del(&dev->kobj);
3832 cleanup_glue_dir(dev, glue_dir);
3833 memalloc_noio_restore(noio_flag);
3834 put_device(parent);
3835}
3836EXPORT_SYMBOL_GPL(device_del);
3837
3838/**
3839 * device_unregister - unregister device from system.
3840 * @dev: device going away.
3841 *
3842 * We do this in two parts, like we do device_register(). First,
3843 * we remove it from all the subsystems with device_del(), then
3844 * we decrement the reference count via put_device(). If that
3845 * is the final reference count, the device will be cleaned up
3846 * via device_release() above. Otherwise, the structure will
3847 * stick around until the final reference to the device is dropped.
3848 */
3849void device_unregister(struct device *dev)
3850{
3851 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3852 device_del(dev);
3853 put_device(dev);
3854}
3855EXPORT_SYMBOL_GPL(device_unregister);
3856
3857static struct device *prev_device(struct klist_iter *i)
3858{
3859 struct klist_node *n = klist_prev(i);
3860 struct device *dev = NULL;
3861 struct device_private *p;
3862
3863 if (n) {
3864 p = to_device_private_parent(n);
3865 dev = p->device;
3866 }
3867 return dev;
3868}
3869
3870static struct device *next_device(struct klist_iter *i)
3871{
3872 struct klist_node *n = klist_next(i);
3873 struct device *dev = NULL;
3874 struct device_private *p;
3875
3876 if (n) {
3877 p = to_device_private_parent(n);
3878 dev = p->device;
3879 }
3880 return dev;
3881}
3882
3883/**
3884 * device_get_devnode - path of device node file
3885 * @dev: device
3886 * @mode: returned file access mode
3887 * @uid: returned file owner
3888 * @gid: returned file group
3889 * @tmp: possibly allocated string
3890 *
3891 * Return the relative path of a possible device node.
3892 * Non-default names may need to allocate a memory to compose
3893 * a name. This memory is returned in tmp and needs to be
3894 * freed by the caller.
3895 */
3896const char *device_get_devnode(const struct device *dev,
3897 umode_t *mode, kuid_t *uid, kgid_t *gid,
3898 const char **tmp)
3899{
3900 char *s;
3901
3902 *tmp = NULL;
3903
3904 /* the device type may provide a specific name */
3905 if (dev->type && dev->type->devnode)
3906 *tmp = dev->type->devnode(dev, mode, uid, gid);
3907 if (*tmp)
3908 return *tmp;
3909
3910 /* the class may provide a specific name */
3911 if (dev->class && dev->class->devnode)
3912 *tmp = dev->class->devnode(dev, mode);
3913 if (*tmp)
3914 return *tmp;
3915
3916 /* return name without allocation, tmp == NULL */
3917 if (strchr(dev_name(dev), '!') == NULL)
3918 return dev_name(dev);
3919
3920 /* replace '!' in the name with '/' */
3921 s = kstrdup_and_replace(dev_name(dev), '!', '/', GFP_KERNEL);
3922 if (!s)
3923 return NULL;
3924 return *tmp = s;
3925}
3926
3927/**
3928 * device_for_each_child - device child iterator.
3929 * @parent: parent struct device.
3930 * @fn: function to be called for each device.
3931 * @data: data for the callback.
3932 *
3933 * Iterate over @parent's child devices, and call @fn for each,
3934 * passing it @data.
3935 *
3936 * We check the return of @fn each time. If it returns anything
3937 * other than 0, we break out and return that value.
3938 */
3939int device_for_each_child(struct device *parent, void *data,
3940 int (*fn)(struct device *dev, void *data))
3941{
3942 struct klist_iter i;
3943 struct device *child;
3944 int error = 0;
3945
3946 if (!parent->p)
3947 return 0;
3948
3949 klist_iter_init(&parent->p->klist_children, &i);
3950 while (!error && (child = next_device(&i)))
3951 error = fn(child, data);
3952 klist_iter_exit(&i);
3953 return error;
3954}
3955EXPORT_SYMBOL_GPL(device_for_each_child);
3956
3957/**
3958 * device_for_each_child_reverse - device child iterator in reversed order.
3959 * @parent: parent struct device.
3960 * @fn: function to be called for each device.
3961 * @data: data for the callback.
3962 *
3963 * Iterate over @parent's child devices, and call @fn for each,
3964 * passing it @data.
3965 *
3966 * We check the return of @fn each time. If it returns anything
3967 * other than 0, we break out and return that value.
3968 */
3969int device_for_each_child_reverse(struct device *parent, void *data,
3970 int (*fn)(struct device *dev, void *data))
3971{
3972 struct klist_iter i;
3973 struct device *child;
3974 int error = 0;
3975
3976 if (!parent->p)
3977 return 0;
3978
3979 klist_iter_init(&parent->p->klist_children, &i);
3980 while ((child = prev_device(&i)) && !error)
3981 error = fn(child, data);
3982 klist_iter_exit(&i);
3983 return error;
3984}
3985EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
3986
3987/**
3988 * device_find_child - device iterator for locating a particular device.
3989 * @parent: parent struct device
3990 * @match: Callback function to check device
3991 * @data: Data to pass to match function
3992 *
3993 * This is similar to the device_for_each_child() function above, but it
3994 * returns a reference to a device that is 'found' for later use, as
3995 * determined by the @match callback.
3996 *
3997 * The callback should return 0 if the device doesn't match and non-zero
3998 * if it does. If the callback returns non-zero and a reference to the
3999 * current device can be obtained, this function will return to the caller
4000 * and not iterate over any more devices.
4001 *
4002 * NOTE: you will need to drop the reference with put_device() after use.
4003 */
4004struct device *device_find_child(struct device *parent, void *data,
4005 int (*match)(struct device *dev, void *data))
4006{
4007 struct klist_iter i;
4008 struct device *child;
4009
4010 if (!parent)
4011 return NULL;
4012
4013 klist_iter_init(&parent->p->klist_children, &i);
4014 while ((child = next_device(&i)))
4015 if (match(child, data) && get_device(child))
4016 break;
4017 klist_iter_exit(&i);
4018 return child;
4019}
4020EXPORT_SYMBOL_GPL(device_find_child);
4021
4022/**
4023 * device_find_child_by_name - device iterator for locating a child device.
4024 * @parent: parent struct device
4025 * @name: name of the child device
4026 *
4027 * This is similar to the device_find_child() function above, but it
4028 * returns a reference to a device that has the name @name.
4029 *
4030 * NOTE: you will need to drop the reference with put_device() after use.
4031 */
4032struct device *device_find_child_by_name(struct device *parent,
4033 const char *name)
4034{
4035 struct klist_iter i;
4036 struct device *child;
4037
4038 if (!parent)
4039 return NULL;
4040
4041 klist_iter_init(&parent->p->klist_children, &i);
4042 while ((child = next_device(&i)))
4043 if (sysfs_streq(dev_name(child), name) && get_device(child))
4044 break;
4045 klist_iter_exit(&i);
4046 return child;
4047}
4048EXPORT_SYMBOL_GPL(device_find_child_by_name);
4049
4050static int match_any(struct device *dev, void *unused)
4051{
4052 return 1;
4053}
4054
4055/**
4056 * device_find_any_child - device iterator for locating a child device, if any.
4057 * @parent: parent struct device
4058 *
4059 * This is similar to the device_find_child() function above, but it
4060 * returns a reference to a child device, if any.
4061 *
4062 * NOTE: you will need to drop the reference with put_device() after use.
4063 */
4064struct device *device_find_any_child(struct device *parent)
4065{
4066 return device_find_child(parent, NULL, match_any);
4067}
4068EXPORT_SYMBOL_GPL(device_find_any_child);
4069
4070int __init devices_init(void)
4071{
4072 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
4073 if (!devices_kset)
4074 return -ENOMEM;
4075 dev_kobj = kobject_create_and_add("dev", NULL);
4076 if (!dev_kobj)
4077 goto dev_kobj_err;
4078 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
4079 if (!sysfs_dev_block_kobj)
4080 goto block_kobj_err;
4081 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
4082 if (!sysfs_dev_char_kobj)
4083 goto char_kobj_err;
4084
4085 return 0;
4086
4087 char_kobj_err:
4088 kobject_put(sysfs_dev_block_kobj);
4089 block_kobj_err:
4090 kobject_put(dev_kobj);
4091 dev_kobj_err:
4092 kset_unregister(devices_kset);
4093 return -ENOMEM;
4094}
4095
4096static int device_check_offline(struct device *dev, void *not_used)
4097{
4098 int ret;
4099
4100 ret = device_for_each_child(dev, NULL, device_check_offline);
4101 if (ret)
4102 return ret;
4103
4104 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
4105}
4106
4107/**
4108 * device_offline - Prepare the device for hot-removal.
4109 * @dev: Device to be put offline.
4110 *
4111 * Execute the device bus type's .offline() callback, if present, to prepare
4112 * the device for a subsequent hot-removal. If that succeeds, the device must
4113 * not be used until either it is removed or its bus type's .online() callback
4114 * is executed.
4115 *
4116 * Call under device_hotplug_lock.
4117 */
4118int device_offline(struct device *dev)
4119{
4120 int ret;
4121
4122 if (dev->offline_disabled)
4123 return -EPERM;
4124
4125 ret = device_for_each_child(dev, NULL, device_check_offline);
4126 if (ret)
4127 return ret;
4128
4129 device_lock(dev);
4130 if (device_supports_offline(dev)) {
4131 if (dev->offline) {
4132 ret = 1;
4133 } else {
4134 ret = dev->bus->offline(dev);
4135 if (!ret) {
4136 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
4137 dev->offline = true;
4138 }
4139 }
4140 }
4141 device_unlock(dev);
4142
4143 return ret;
4144}
4145
4146/**
4147 * device_online - Put the device back online after successful device_offline().
4148 * @dev: Device to be put back online.
4149 *
4150 * If device_offline() has been successfully executed for @dev, but the device
4151 * has not been removed subsequently, execute its bus type's .online() callback
4152 * to indicate that the device can be used again.
4153 *
4154 * Call under device_hotplug_lock.
4155 */
4156int device_online(struct device *dev)
4157{
4158 int ret = 0;
4159
4160 device_lock(dev);
4161 if (device_supports_offline(dev)) {
4162 if (dev->offline) {
4163 ret = dev->bus->online(dev);
4164 if (!ret) {
4165 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
4166 dev->offline = false;
4167 }
4168 } else {
4169 ret = 1;
4170 }
4171 }
4172 device_unlock(dev);
4173
4174 return ret;
4175}
4176
4177struct root_device {
4178 struct device dev;
4179 struct module *owner;
4180};
4181
4182static inline struct root_device *to_root_device(struct device *d)
4183{
4184 return container_of(d, struct root_device, dev);
4185}
4186
4187static void root_device_release(struct device *dev)
4188{
4189 kfree(to_root_device(dev));
4190}
4191
4192/**
4193 * __root_device_register - allocate and register a root device
4194 * @name: root device name
4195 * @owner: owner module of the root device, usually THIS_MODULE
4196 *
4197 * This function allocates a root device and registers it
4198 * using device_register(). In order to free the returned
4199 * device, use root_device_unregister().
4200 *
4201 * Root devices are dummy devices which allow other devices
4202 * to be grouped under /sys/devices. Use this function to
4203 * allocate a root device and then use it as the parent of
4204 * any device which should appear under /sys/devices/{name}
4205 *
4206 * The /sys/devices/{name} directory will also contain a
4207 * 'module' symlink which points to the @owner directory
4208 * in sysfs.
4209 *
4210 * Returns &struct device pointer on success, or ERR_PTR() on error.
4211 *
4212 * Note: You probably want to use root_device_register().
4213 */
4214struct device *__root_device_register(const char *name, struct module *owner)
4215{
4216 struct root_device *root;
4217 int err = -ENOMEM;
4218
4219 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
4220 if (!root)
4221 return ERR_PTR(err);
4222
4223 err = dev_set_name(&root->dev, "%s", name);
4224 if (err) {
4225 kfree(root);
4226 return ERR_PTR(err);
4227 }
4228
4229 root->dev.release = root_device_release;
4230
4231 err = device_register(&root->dev);
4232 if (err) {
4233 put_device(&root->dev);
4234 return ERR_PTR(err);
4235 }
4236
4237#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
4238 if (owner) {
4239 struct module_kobject *mk = &owner->mkobj;
4240
4241 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
4242 if (err) {
4243 device_unregister(&root->dev);
4244 return ERR_PTR(err);
4245 }
4246 root->owner = owner;
4247 }
4248#endif
4249
4250 return &root->dev;
4251}
4252EXPORT_SYMBOL_GPL(__root_device_register);
4253
4254/**
4255 * root_device_unregister - unregister and free a root device
4256 * @dev: device going away
4257 *
4258 * This function unregisters and cleans up a device that was created by
4259 * root_device_register().
4260 */
4261void root_device_unregister(struct device *dev)
4262{
4263 struct root_device *root = to_root_device(dev);
4264
4265 if (root->owner)
4266 sysfs_remove_link(&root->dev.kobj, "module");
4267
4268 device_unregister(dev);
4269}
4270EXPORT_SYMBOL_GPL(root_device_unregister);
4271
4272
4273static void device_create_release(struct device *dev)
4274{
4275 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
4276 kfree(dev);
4277}
4278
4279static __printf(6, 0) struct device *
4280device_create_groups_vargs(const struct class *class, struct device *parent,
4281 dev_t devt, void *drvdata,
4282 const struct attribute_group **groups,
4283 const char *fmt, va_list args)
4284{
4285 struct device *dev = NULL;
4286 int retval = -ENODEV;
4287
4288 if (IS_ERR_OR_NULL(class))
4289 goto error;
4290
4291 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
4292 if (!dev) {
4293 retval = -ENOMEM;
4294 goto error;
4295 }
4296
4297 device_initialize(dev);
4298 dev->devt = devt;
4299 dev->class = class;
4300 dev->parent = parent;
4301 dev->groups = groups;
4302 dev->release = device_create_release;
4303 dev_set_drvdata(dev, drvdata);
4304
4305 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
4306 if (retval)
4307 goto error;
4308
4309 retval = device_add(dev);
4310 if (retval)
4311 goto error;
4312
4313 return dev;
4314
4315error:
4316 put_device(dev);
4317 return ERR_PTR(retval);
4318}
4319
4320/**
4321 * device_create - creates a device and registers it with sysfs
4322 * @class: pointer to the struct class that this device should be registered to
4323 * @parent: pointer to the parent struct device of this new device, if any
4324 * @devt: the dev_t for the char device to be added
4325 * @drvdata: the data to be added to the device for callbacks
4326 * @fmt: string for the device's name
4327 *
4328 * This function can be used by char device classes. A struct device
4329 * will be created in sysfs, registered to the specified class.
4330 *
4331 * A "dev" file will be created, showing the dev_t for the device, if
4332 * the dev_t is not 0,0.
4333 * If a pointer to a parent struct device is passed in, the newly created
4334 * struct device will be a child of that device in sysfs.
4335 * The pointer to the struct device will be returned from the call.
4336 * Any further sysfs files that might be required can be created using this
4337 * pointer.
4338 *
4339 * Returns &struct device pointer on success, or ERR_PTR() on error.
4340 */
4341struct device *device_create(const struct class *class, struct device *parent,
4342 dev_t devt, void *drvdata, const char *fmt, ...)
4343{
4344 va_list vargs;
4345 struct device *dev;
4346
4347 va_start(vargs, fmt);
4348 dev = device_create_groups_vargs(class, parent, devt, drvdata, NULL,
4349 fmt, vargs);
4350 va_end(vargs);
4351 return dev;
4352}
4353EXPORT_SYMBOL_GPL(device_create);
4354
4355/**
4356 * device_create_with_groups - creates a device and registers it with sysfs
4357 * @class: pointer to the struct class that this device should be registered to
4358 * @parent: pointer to the parent struct device of this new device, if any
4359 * @devt: the dev_t for the char device to be added
4360 * @drvdata: the data to be added to the device for callbacks
4361 * @groups: NULL-terminated list of attribute groups to be created
4362 * @fmt: string for the device's name
4363 *
4364 * This function can be used by char device classes. A struct device
4365 * will be created in sysfs, registered to the specified class.
4366 * Additional attributes specified in the groups parameter will also
4367 * be created automatically.
4368 *
4369 * A "dev" file will be created, showing the dev_t for the device, if
4370 * the dev_t is not 0,0.
4371 * If a pointer to a parent struct device is passed in, the newly created
4372 * struct device will be a child of that device in sysfs.
4373 * The pointer to the struct device will be returned from the call.
4374 * Any further sysfs files that might be required can be created using this
4375 * pointer.
4376 *
4377 * Returns &struct device pointer on success, or ERR_PTR() on error.
4378 */
4379struct device *device_create_with_groups(const struct class *class,
4380 struct device *parent, dev_t devt,
4381 void *drvdata,
4382 const struct attribute_group **groups,
4383 const char *fmt, ...)
4384{
4385 va_list vargs;
4386 struct device *dev;
4387
4388 va_start(vargs, fmt);
4389 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
4390 fmt, vargs);
4391 va_end(vargs);
4392 return dev;
4393}
4394EXPORT_SYMBOL_GPL(device_create_with_groups);
4395
4396/**
4397 * device_destroy - removes a device that was created with device_create()
4398 * @class: pointer to the struct class that this device was registered with
4399 * @devt: the dev_t of the device that was previously registered
4400 *
4401 * This call unregisters and cleans up a device that was created with a
4402 * call to device_create().
4403 */
4404void device_destroy(const struct class *class, dev_t devt)
4405{
4406 struct device *dev;
4407
4408 dev = class_find_device_by_devt(class, devt);
4409 if (dev) {
4410 put_device(dev);
4411 device_unregister(dev);
4412 }
4413}
4414EXPORT_SYMBOL_GPL(device_destroy);
4415
4416/**
4417 * device_rename - renames a device
4418 * @dev: the pointer to the struct device to be renamed
4419 * @new_name: the new name of the device
4420 *
4421 * It is the responsibility of the caller to provide mutual
4422 * exclusion between two different calls of device_rename
4423 * on the same device to ensure that new_name is valid and
4424 * won't conflict with other devices.
4425 *
4426 * Note: given that some subsystems (networking and infiniband) use this
4427 * function, with no immediate plans for this to change, we cannot assume or
4428 * require that this function not be called at all.
4429 *
4430 * However, if you're writing new code, do not call this function. The following
4431 * text from Kay Sievers offers some insight:
4432 *
4433 * Renaming devices is racy at many levels, symlinks and other stuff are not
4434 * replaced atomically, and you get a "move" uevent, but it's not easy to
4435 * connect the event to the old and new device. Device nodes are not renamed at
4436 * all, there isn't even support for that in the kernel now.
4437 *
4438 * In the meantime, during renaming, your target name might be taken by another
4439 * driver, creating conflicts. Or the old name is taken directly after you
4440 * renamed it -- then you get events for the same DEVPATH, before you even see
4441 * the "move" event. It's just a mess, and nothing new should ever rely on
4442 * kernel device renaming. Besides that, it's not even implemented now for
4443 * other things than (driver-core wise very simple) network devices.
4444 *
4445 * Make up a "real" name in the driver before you register anything, or add
4446 * some other attributes for userspace to find the device, or use udev to add
4447 * symlinks -- but never rename kernel devices later, it's a complete mess. We
4448 * don't even want to get into that and try to implement the missing pieces in
4449 * the core. We really have other pieces to fix in the driver core mess. :)
4450 */
4451int device_rename(struct device *dev, const char *new_name)
4452{
4453 struct kobject *kobj = &dev->kobj;
4454 char *old_device_name = NULL;
4455 int error;
4456
4457 dev = get_device(dev);
4458 if (!dev)
4459 return -EINVAL;
4460
4461 dev_dbg(dev, "renaming to %s\n", new_name);
4462
4463 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
4464 if (!old_device_name) {
4465 error = -ENOMEM;
4466 goto out;
4467 }
4468
4469 if (dev->class) {
4470 struct subsys_private *sp = class_to_subsys(dev->class);
4471
4472 if (!sp) {
4473 error = -EINVAL;
4474 goto out;
4475 }
4476
4477 error = sysfs_rename_link_ns(&sp->subsys.kobj, kobj, old_device_name,
4478 new_name, kobject_namespace(kobj));
4479 subsys_put(sp);
4480 if (error)
4481 goto out;
4482 }
4483
4484 error = kobject_rename(kobj, new_name);
4485 if (error)
4486 goto out;
4487
4488out:
4489 put_device(dev);
4490
4491 kfree(old_device_name);
4492
4493 return error;
4494}
4495EXPORT_SYMBOL_GPL(device_rename);
4496
4497static int device_move_class_links(struct device *dev,
4498 struct device *old_parent,
4499 struct device *new_parent)
4500{
4501 int error = 0;
4502
4503 if (old_parent)
4504 sysfs_remove_link(&dev->kobj, "device");
4505 if (new_parent)
4506 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
4507 "device");
4508 return error;
4509}
4510
4511/**
4512 * device_move - moves a device to a new parent
4513 * @dev: the pointer to the struct device to be moved
4514 * @new_parent: the new parent of the device (can be NULL)
4515 * @dpm_order: how to reorder the dpm_list
4516 */
4517int device_move(struct device *dev, struct device *new_parent,
4518 enum dpm_order dpm_order)
4519{
4520 int error;
4521 struct device *old_parent;
4522 struct kobject *new_parent_kobj;
4523
4524 dev = get_device(dev);
4525 if (!dev)
4526 return -EINVAL;
4527
4528 device_pm_lock();
4529 new_parent = get_device(new_parent);
4530 new_parent_kobj = get_device_parent(dev, new_parent);
4531 if (IS_ERR(new_parent_kobj)) {
4532 error = PTR_ERR(new_parent_kobj);
4533 put_device(new_parent);
4534 goto out;
4535 }
4536
4537 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
4538 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
4539 error = kobject_move(&dev->kobj, new_parent_kobj);
4540 if (error) {
4541 cleanup_glue_dir(dev, new_parent_kobj);
4542 put_device(new_parent);
4543 goto out;
4544 }
4545 old_parent = dev->parent;
4546 dev->parent = new_parent;
4547 if (old_parent)
4548 klist_remove(&dev->p->knode_parent);
4549 if (new_parent) {
4550 klist_add_tail(&dev->p->knode_parent,
4551 &new_parent->p->klist_children);
4552 set_dev_node(dev, dev_to_node(new_parent));
4553 }
4554
4555 if (dev->class) {
4556 error = device_move_class_links(dev, old_parent, new_parent);
4557 if (error) {
4558 /* We ignore errors on cleanup since we're hosed anyway... */
4559 device_move_class_links(dev, new_parent, old_parent);
4560 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
4561 if (new_parent)
4562 klist_remove(&dev->p->knode_parent);
4563 dev->parent = old_parent;
4564 if (old_parent) {
4565 klist_add_tail(&dev->p->knode_parent,
4566 &old_parent->p->klist_children);
4567 set_dev_node(dev, dev_to_node(old_parent));
4568 }
4569 }
4570 cleanup_glue_dir(dev, new_parent_kobj);
4571 put_device(new_parent);
4572 goto out;
4573 }
4574 }
4575 switch (dpm_order) {
4576 case DPM_ORDER_NONE:
4577 break;
4578 case DPM_ORDER_DEV_AFTER_PARENT:
4579 device_pm_move_after(dev, new_parent);
4580 devices_kset_move_after(dev, new_parent);
4581 break;
4582 case DPM_ORDER_PARENT_BEFORE_DEV:
4583 device_pm_move_before(new_parent, dev);
4584 devices_kset_move_before(new_parent, dev);
4585 break;
4586 case DPM_ORDER_DEV_LAST:
4587 device_pm_move_last(dev);
4588 devices_kset_move_last(dev);
4589 break;
4590 }
4591
4592 put_device(old_parent);
4593out:
4594 device_pm_unlock();
4595 put_device(dev);
4596 return error;
4597}
4598EXPORT_SYMBOL_GPL(device_move);
4599
4600static int device_attrs_change_owner(struct device *dev, kuid_t kuid,
4601 kgid_t kgid)
4602{
4603 struct kobject *kobj = &dev->kobj;
4604 const struct class *class = dev->class;
4605 const struct device_type *type = dev->type;
4606 int error;
4607
4608 if (class) {
4609 /*
4610 * Change the device groups of the device class for @dev to
4611 * @kuid/@kgid.
4612 */
4613 error = sysfs_groups_change_owner(kobj, class->dev_groups, kuid,
4614 kgid);
4615 if (error)
4616 return error;
4617 }
4618
4619 if (type) {
4620 /*
4621 * Change the device groups of the device type for @dev to
4622 * @kuid/@kgid.
4623 */
4624 error = sysfs_groups_change_owner(kobj, type->groups, kuid,
4625 kgid);
4626 if (error)
4627 return error;
4628 }
4629
4630 /* Change the device groups of @dev to @kuid/@kgid. */
4631 error = sysfs_groups_change_owner(kobj, dev->groups, kuid, kgid);
4632 if (error)
4633 return error;
4634
4635 if (device_supports_offline(dev) && !dev->offline_disabled) {
4636 /* Change online device attributes of @dev to @kuid/@kgid. */
4637 error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name,
4638 kuid, kgid);
4639 if (error)
4640 return error;
4641 }
4642
4643 return 0;
4644}
4645
4646/**
4647 * device_change_owner - change the owner of an existing device.
4648 * @dev: device.
4649 * @kuid: new owner's kuid
4650 * @kgid: new owner's kgid
4651 *
4652 * This changes the owner of @dev and its corresponding sysfs entries to
4653 * @kuid/@kgid. This function closely mirrors how @dev was added via driver
4654 * core.
4655 *
4656 * Returns 0 on success or error code on failure.
4657 */
4658int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
4659{
4660 int error;
4661 struct kobject *kobj = &dev->kobj;
4662 struct subsys_private *sp;
4663
4664 dev = get_device(dev);
4665 if (!dev)
4666 return -EINVAL;
4667
4668 /*
4669 * Change the kobject and the default attributes and groups of the
4670 * ktype associated with it to @kuid/@kgid.
4671 */
4672 error = sysfs_change_owner(kobj, kuid, kgid);
4673 if (error)
4674 goto out;
4675
4676 /*
4677 * Change the uevent file for @dev to the new owner. The uevent file
4678 * was created in a separate step when @dev got added and we mirror
4679 * that step here.
4680 */
4681 error = sysfs_file_change_owner(kobj, dev_attr_uevent.attr.name, kuid,
4682 kgid);
4683 if (error)
4684 goto out;
4685
4686 /*
4687 * Change the device groups, the device groups associated with the
4688 * device class, and the groups associated with the device type of @dev
4689 * to @kuid/@kgid.
4690 */
4691 error = device_attrs_change_owner(dev, kuid, kgid);
4692 if (error)
4693 goto out;
4694
4695 error = dpm_sysfs_change_owner(dev, kuid, kgid);
4696 if (error)
4697 goto out;
4698
4699 /*
4700 * Change the owner of the symlink located in the class directory of
4701 * the device class associated with @dev which points to the actual
4702 * directory entry for @dev to @kuid/@kgid. This ensures that the
4703 * symlink shows the same permissions as its target.
4704 */
4705 sp = class_to_subsys(dev->class);
4706 if (!sp) {
4707 error = -EINVAL;
4708 goto out;
4709 }
4710 error = sysfs_link_change_owner(&sp->subsys.kobj, &dev->kobj, dev_name(dev), kuid, kgid);
4711 subsys_put(sp);
4712
4713out:
4714 put_device(dev);
4715 return error;
4716}
4717EXPORT_SYMBOL_GPL(device_change_owner);
4718
4719/**
4720 * device_shutdown - call ->shutdown() on each device to shutdown.
4721 */
4722void device_shutdown(void)
4723{
4724 struct device *dev, *parent;
4725
4726 wait_for_device_probe();
4727 device_block_probing();
4728
4729 cpufreq_suspend();
4730
4731 spin_lock(&devices_kset->list_lock);
4732 /*
4733 * Walk the devices list backward, shutting down each in turn.
4734 * Beware that device unplug events may also start pulling
4735 * devices offline, even as the system is shutting down.
4736 */
4737 while (!list_empty(&devices_kset->list)) {
4738 dev = list_entry(devices_kset->list.prev, struct device,
4739 kobj.entry);
4740
4741 /*
4742 * hold reference count of device's parent to
4743 * prevent it from being freed because parent's
4744 * lock is to be held
4745 */
4746 parent = get_device(dev->parent);
4747 get_device(dev);
4748 /*
4749 * Make sure the device is off the kset list, in the
4750 * event that dev->*->shutdown() doesn't remove it.
4751 */
4752 list_del_init(&dev->kobj.entry);
4753 spin_unlock(&devices_kset->list_lock);
4754
4755 /* hold lock to avoid race with probe/release */
4756 if (parent)
4757 device_lock(parent);
4758 device_lock(dev);
4759
4760 /* Don't allow any more runtime suspends */
4761 pm_runtime_get_noresume(dev);
4762 pm_runtime_barrier(dev);
4763
4764 if (dev->class && dev->class->shutdown_pre) {
4765 if (initcall_debug)
4766 dev_info(dev, "shutdown_pre\n");
4767 dev->class->shutdown_pre(dev);
4768 }
4769 if (dev->bus && dev->bus->shutdown) {
4770 if (initcall_debug)
4771 dev_info(dev, "shutdown\n");
4772 dev->bus->shutdown(dev);
4773 } else if (dev->driver && dev->driver->shutdown) {
4774 if (initcall_debug)
4775 dev_info(dev, "shutdown\n");
4776 dev->driver->shutdown(dev);
4777 }
4778
4779 device_unlock(dev);
4780 if (parent)
4781 device_unlock(parent);
4782
4783 put_device(dev);
4784 put_device(parent);
4785
4786 spin_lock(&devices_kset->list_lock);
4787 }
4788 spin_unlock(&devices_kset->list_lock);
4789}
4790
4791/*
4792 * Device logging functions
4793 */
4794
4795#ifdef CONFIG_PRINTK
4796static void
4797set_dev_info(const struct device *dev, struct dev_printk_info *dev_info)
4798{
4799 const char *subsys;
4800
4801 memset(dev_info, 0, sizeof(*dev_info));
4802
4803 if (dev->class)
4804 subsys = dev->class->name;
4805 else if (dev->bus)
4806 subsys = dev->bus->name;
4807 else
4808 return;
4809
4810 strscpy(dev_info->subsystem, subsys, sizeof(dev_info->subsystem));
4811
4812 /*
4813 * Add device identifier DEVICE=:
4814 * b12:8 block dev_t
4815 * c127:3 char dev_t
4816 * n8 netdev ifindex
4817 * +sound:card0 subsystem:devname
4818 */
4819 if (MAJOR(dev->devt)) {
4820 char c;
4821
4822 if (strcmp(subsys, "block") == 0)
4823 c = 'b';
4824 else
4825 c = 'c';
4826
4827 snprintf(dev_info->device, sizeof(dev_info->device),
4828 "%c%u:%u", c, MAJOR(dev->devt), MINOR(dev->devt));
4829 } else if (strcmp(subsys, "net") == 0) {
4830 struct net_device *net = to_net_dev(dev);
4831
4832 snprintf(dev_info->device, sizeof(dev_info->device),
4833 "n%u", net->ifindex);
4834 } else {
4835 snprintf(dev_info->device, sizeof(dev_info->device),
4836 "+%s:%s", subsys, dev_name(dev));
4837 }
4838}
4839
4840int dev_vprintk_emit(int level, const struct device *dev,
4841 const char *fmt, va_list args)
4842{
4843 struct dev_printk_info dev_info;
4844
4845 set_dev_info(dev, &dev_info);
4846
4847 return vprintk_emit(0, level, &dev_info, fmt, args);
4848}
4849EXPORT_SYMBOL(dev_vprintk_emit);
4850
4851int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
4852{
4853 va_list args;
4854 int r;
4855
4856 va_start(args, fmt);
4857
4858 r = dev_vprintk_emit(level, dev, fmt, args);
4859
4860 va_end(args);
4861
4862 return r;
4863}
4864EXPORT_SYMBOL(dev_printk_emit);
4865
4866static void __dev_printk(const char *level, const struct device *dev,
4867 struct va_format *vaf)
4868{
4869 if (dev)
4870 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
4871 dev_driver_string(dev), dev_name(dev), vaf);
4872 else
4873 printk("%s(NULL device *): %pV", level, vaf);
4874}
4875
4876void _dev_printk(const char *level, const struct device *dev,
4877 const char *fmt, ...)
4878{
4879 struct va_format vaf;
4880 va_list args;
4881
4882 va_start(args, fmt);
4883
4884 vaf.fmt = fmt;
4885 vaf.va = &args;
4886
4887 __dev_printk(level, dev, &vaf);
4888
4889 va_end(args);
4890}
4891EXPORT_SYMBOL(_dev_printk);
4892
4893#define define_dev_printk_level(func, kern_level) \
4894void func(const struct device *dev, const char *fmt, ...) \
4895{ \
4896 struct va_format vaf; \
4897 va_list args; \
4898 \
4899 va_start(args, fmt); \
4900 \
4901 vaf.fmt = fmt; \
4902 vaf.va = &args; \
4903 \
4904 __dev_printk(kern_level, dev, &vaf); \
4905 \
4906 va_end(args); \
4907} \
4908EXPORT_SYMBOL(func);
4909
4910define_dev_printk_level(_dev_emerg, KERN_EMERG);
4911define_dev_printk_level(_dev_alert, KERN_ALERT);
4912define_dev_printk_level(_dev_crit, KERN_CRIT);
4913define_dev_printk_level(_dev_err, KERN_ERR);
4914define_dev_printk_level(_dev_warn, KERN_WARNING);
4915define_dev_printk_level(_dev_notice, KERN_NOTICE);
4916define_dev_printk_level(_dev_info, KERN_INFO);
4917
4918#endif
4919
4920/**
4921 * dev_err_probe - probe error check and log helper
4922 * @dev: the pointer to the struct device
4923 * @err: error value to test
4924 * @fmt: printf-style format string
4925 * @...: arguments as specified in the format string
4926 *
4927 * This helper implements common pattern present in probe functions for error
4928 * checking: print debug or error message depending if the error value is
4929 * -EPROBE_DEFER and propagate error upwards.
4930 * In case of -EPROBE_DEFER it sets also defer probe reason, which can be
4931 * checked later by reading devices_deferred debugfs attribute.
4932 * It replaces code sequence::
4933 *
4934 * if (err != -EPROBE_DEFER)
4935 * dev_err(dev, ...);
4936 * else
4937 * dev_dbg(dev, ...);
4938 * return err;
4939 *
4940 * with::
4941 *
4942 * return dev_err_probe(dev, err, ...);
4943 *
4944 * Note that it is deemed acceptable to use this function for error
4945 * prints during probe even if the @err is known to never be -EPROBE_DEFER.
4946 * The benefit compared to a normal dev_err() is the standardized format
4947 * of the error code and the fact that the error code is returned.
4948 *
4949 * Returns @err.
4950 *
4951 */
4952int dev_err_probe(const struct device *dev, int err, const char *fmt, ...)
4953{
4954 struct va_format vaf;
4955 va_list args;
4956
4957 va_start(args, fmt);
4958 vaf.fmt = fmt;
4959 vaf.va = &args;
4960
4961 if (err != -EPROBE_DEFER) {
4962 dev_err(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
4963 } else {
4964 device_set_deferred_probe_reason(dev, &vaf);
4965 dev_dbg(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
4966 }
4967
4968 va_end(args);
4969
4970 return err;
4971}
4972EXPORT_SYMBOL_GPL(dev_err_probe);
4973
4974static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
4975{
4976 return fwnode && !IS_ERR(fwnode->secondary);
4977}
4978
4979/**
4980 * set_primary_fwnode - Change the primary firmware node of a given device.
4981 * @dev: Device to handle.
4982 * @fwnode: New primary firmware node of the device.
4983 *
4984 * Set the device's firmware node pointer to @fwnode, but if a secondary
4985 * firmware node of the device is present, preserve it.
4986 *
4987 * Valid fwnode cases are:
4988 * - primary --> secondary --> -ENODEV
4989 * - primary --> NULL
4990 * - secondary --> -ENODEV
4991 * - NULL
4992 */
4993void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
4994{
4995 struct device *parent = dev->parent;
4996 struct fwnode_handle *fn = dev->fwnode;
4997
4998 if (fwnode) {
4999 if (fwnode_is_primary(fn))
5000 fn = fn->secondary;
5001
5002 if (fn) {
5003 WARN_ON(fwnode->secondary);
5004 fwnode->secondary = fn;
5005 }
5006 dev->fwnode = fwnode;
5007 } else {
5008 if (fwnode_is_primary(fn)) {
5009 dev->fwnode = fn->secondary;
5010
5011 /* Skip nullifying fn->secondary if the primary is shared */
5012 if (parent && fn == parent->fwnode)
5013 return;
5014
5015 /* Set fn->secondary = NULL, so fn remains the primary fwnode */
5016 fn->secondary = NULL;
5017 } else {
5018 dev->fwnode = NULL;
5019 }
5020 }
5021}
5022EXPORT_SYMBOL_GPL(set_primary_fwnode);
5023
5024/**
5025 * set_secondary_fwnode - Change the secondary firmware node of a given device.
5026 * @dev: Device to handle.
5027 * @fwnode: New secondary firmware node of the device.
5028 *
5029 * If a primary firmware node of the device is present, set its secondary
5030 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
5031 * @fwnode.
5032 */
5033void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
5034{
5035 if (fwnode)
5036 fwnode->secondary = ERR_PTR(-ENODEV);
5037
5038 if (fwnode_is_primary(dev->fwnode))
5039 dev->fwnode->secondary = fwnode;
5040 else
5041 dev->fwnode = fwnode;
5042}
5043EXPORT_SYMBOL_GPL(set_secondary_fwnode);
5044
5045/**
5046 * device_set_of_node_from_dev - reuse device-tree node of another device
5047 * @dev: device whose device-tree node is being set
5048 * @dev2: device whose device-tree node is being reused
5049 *
5050 * Takes another reference to the new device-tree node after first dropping
5051 * any reference held to the old node.
5052 */
5053void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
5054{
5055 of_node_put(dev->of_node);
5056 dev->of_node = of_node_get(dev2->of_node);
5057 dev->of_node_reused = true;
5058}
5059EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
5060
5061void device_set_node(struct device *dev, struct fwnode_handle *fwnode)
5062{
5063 dev->fwnode = fwnode;
5064 dev->of_node = to_of_node(fwnode);
5065}
5066EXPORT_SYMBOL_GPL(device_set_node);
5067
5068int device_match_name(struct device *dev, const void *name)
5069{
5070 return sysfs_streq(dev_name(dev), name);
5071}
5072EXPORT_SYMBOL_GPL(device_match_name);
5073
5074int device_match_of_node(struct device *dev, const void *np)
5075{
5076 return dev->of_node == np;
5077}
5078EXPORT_SYMBOL_GPL(device_match_of_node);
5079
5080int device_match_fwnode(struct device *dev, const void *fwnode)
5081{
5082 return dev_fwnode(dev) == fwnode;
5083}
5084EXPORT_SYMBOL_GPL(device_match_fwnode);
5085
5086int device_match_devt(struct device *dev, const void *pdevt)
5087{
5088 return dev->devt == *(dev_t *)pdevt;
5089}
5090EXPORT_SYMBOL_GPL(device_match_devt);
5091
5092int device_match_acpi_dev(struct device *dev, const void *adev)
5093{
5094 return ACPI_COMPANION(dev) == adev;
5095}
5096EXPORT_SYMBOL(device_match_acpi_dev);
5097
5098int device_match_acpi_handle(struct device *dev, const void *handle)
5099{
5100 return ACPI_HANDLE(dev) == handle;
5101}
5102EXPORT_SYMBOL(device_match_acpi_handle);
5103
5104int device_match_any(struct device *dev, const void *unused)
5105{
5106 return 1;
5107}
5108EXPORT_SYMBOL_GPL(device_match_any);