projects/linux·blob uaccess-cleanups · refs

5438 lines · 147.8 KiB

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