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