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