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