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