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