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