xref: /openbmc/linux/drivers/base/core.c (revision d0676871)
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 static bool fw_devlink_is_permissive(void)
2374 {
2375 	return fw_devlink_flags == DL_FLAG_SYNC_STATE_ONLY;
2376 }
2377 
2378 /**
2379  * device_add - add device to device hierarchy.
2380  * @dev: device.
2381  *
2382  * This is part 2 of device_register(), though may be called
2383  * separately _iff_ device_initialize() has been called separately.
2384  *
2385  * This adds @dev to the kobject hierarchy via kobject_add(), adds it
2386  * to the global and sibling lists for the device, then
2387  * adds it to the other relevant subsystems of the driver model.
2388  *
2389  * Do not call this routine or device_register() more than once for
2390  * any device structure.  The driver model core is not designed to work
2391  * with devices that get unregistered and then spring back to life.
2392  * (Among other things, it's very hard to guarantee that all references
2393  * to the previous incarnation of @dev have been dropped.)  Allocate
2394  * and register a fresh new struct device instead.
2395  *
2396  * NOTE: _Never_ directly free @dev after calling this function, even
2397  * if it returned an error! Always use put_device() to give up your
2398  * reference instead.
2399  *
2400  * Rule of thumb is: if device_add() succeeds, you should call
2401  * device_del() when you want to get rid of it. If device_add() has
2402  * *not* succeeded, use *only* put_device() to drop the reference
2403  * count.
2404  */
2405 int device_add(struct device *dev)
2406 {
2407 	struct device *parent;
2408 	struct kobject *kobj;
2409 	struct class_interface *class_intf;
2410 	int error = -EINVAL, fw_ret;
2411 	struct kobject *glue_dir = NULL;
2412 	bool is_fwnode_dev = false;
2413 
2414 	dev = get_device(dev);
2415 	if (!dev)
2416 		goto done;
2417 
2418 	if (!dev->p) {
2419 		error = device_private_init(dev);
2420 		if (error)
2421 			goto done;
2422 	}
2423 
2424 	/*
2425 	 * for statically allocated devices, which should all be converted
2426 	 * some day, we need to initialize the name. We prevent reading back
2427 	 * the name, and force the use of dev_name()
2428 	 */
2429 	if (dev->init_name) {
2430 		dev_set_name(dev, "%s", dev->init_name);
2431 		dev->init_name = NULL;
2432 	}
2433 
2434 	/* subsystems can specify simple device enumeration */
2435 	if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
2436 		dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
2437 
2438 	if (!dev_name(dev)) {
2439 		error = -EINVAL;
2440 		goto name_error;
2441 	}
2442 
2443 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2444 
2445 	parent = get_device(dev->parent);
2446 	kobj = get_device_parent(dev, parent);
2447 	if (IS_ERR(kobj)) {
2448 		error = PTR_ERR(kobj);
2449 		goto parent_error;
2450 	}
2451 	if (kobj)
2452 		dev->kobj.parent = kobj;
2453 
2454 	/* use parent numa_node */
2455 	if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
2456 		set_dev_node(dev, dev_to_node(parent));
2457 
2458 	/* first, register with generic layer. */
2459 	/* we require the name to be set before, and pass NULL */
2460 	error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
2461 	if (error) {
2462 		glue_dir = get_glue_dir(dev);
2463 		goto Error;
2464 	}
2465 
2466 	/* notify platform of device entry */
2467 	error = device_platform_notify(dev, KOBJ_ADD);
2468 	if (error)
2469 		goto platform_error;
2470 
2471 	error = device_create_file(dev, &dev_attr_uevent);
2472 	if (error)
2473 		goto attrError;
2474 
2475 	error = device_add_class_symlinks(dev);
2476 	if (error)
2477 		goto SymlinkError;
2478 	error = device_add_attrs(dev);
2479 	if (error)
2480 		goto AttrsError;
2481 	error = bus_add_device(dev);
2482 	if (error)
2483 		goto BusError;
2484 	error = dpm_sysfs_add(dev);
2485 	if (error)
2486 		goto DPMError;
2487 	device_pm_add(dev);
2488 
2489 	if (MAJOR(dev->devt)) {
2490 		error = device_create_file(dev, &dev_attr_dev);
2491 		if (error)
2492 			goto DevAttrError;
2493 
2494 		error = device_create_sys_dev_entry(dev);
2495 		if (error)
2496 			goto SysEntryError;
2497 
2498 		devtmpfs_create_node(dev);
2499 	}
2500 
2501 	/* Notify clients of device addition.  This call must come
2502 	 * after dpm_sysfs_add() and before kobject_uevent().
2503 	 */
2504 	if (dev->bus)
2505 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2506 					     BUS_NOTIFY_ADD_DEVICE, dev);
2507 
2508 	kobject_uevent(&dev->kobj, KOBJ_ADD);
2509 
2510 	if (dev->fwnode && !dev->fwnode->dev) {
2511 		dev->fwnode->dev = dev;
2512 		is_fwnode_dev = true;
2513 	}
2514 
2515 	/*
2516 	 * Check if any of the other devices (consumers) have been waiting for
2517 	 * this device (supplier) to be added so that they can create a device
2518 	 * link to it.
2519 	 *
2520 	 * This needs to happen after device_pm_add() because device_link_add()
2521 	 * requires the supplier be registered before it's called.
2522 	 *
2523 	 * But this also needs to happe before bus_probe_device() to make sure
2524 	 * waiting consumers can link to it before the driver is bound to the
2525 	 * device and the driver sync_state callback is called for this device.
2526 	 */
2527 	device_link_add_missing_supplier_links();
2528 
2529 	if (fw_devlink_flags && is_fwnode_dev &&
2530 	    fwnode_has_op(dev->fwnode, add_links)) {
2531 		fw_ret = fwnode_call_int_op(dev->fwnode, add_links, dev);
2532 		if (fw_ret == -ENODEV && !fw_devlink_is_permissive())
2533 			device_link_wait_for_mandatory_supplier(dev);
2534 		else if (fw_ret)
2535 			device_link_wait_for_optional_supplier(dev);
2536 	}
2537 
2538 	bus_probe_device(dev);
2539 	if (parent)
2540 		klist_add_tail(&dev->p->knode_parent,
2541 			       &parent->p->klist_children);
2542 
2543 	if (dev->class) {
2544 		mutex_lock(&dev->class->p->mutex);
2545 		/* tie the class to the device */
2546 		klist_add_tail(&dev->p->knode_class,
2547 			       &dev->class->p->klist_devices);
2548 
2549 		/* notify any interfaces that the device is here */
2550 		list_for_each_entry(class_intf,
2551 				    &dev->class->p->interfaces, node)
2552 			if (class_intf->add_dev)
2553 				class_intf->add_dev(dev, class_intf);
2554 		mutex_unlock(&dev->class->p->mutex);
2555 	}
2556 done:
2557 	put_device(dev);
2558 	return error;
2559  SysEntryError:
2560 	if (MAJOR(dev->devt))
2561 		device_remove_file(dev, &dev_attr_dev);
2562  DevAttrError:
2563 	device_pm_remove(dev);
2564 	dpm_sysfs_remove(dev);
2565  DPMError:
2566 	bus_remove_device(dev);
2567  BusError:
2568 	device_remove_attrs(dev);
2569  AttrsError:
2570 	device_remove_class_symlinks(dev);
2571  SymlinkError:
2572 	device_remove_file(dev, &dev_attr_uevent);
2573  attrError:
2574 	device_platform_notify(dev, KOBJ_REMOVE);
2575 platform_error:
2576 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2577 	glue_dir = get_glue_dir(dev);
2578 	kobject_del(&dev->kobj);
2579  Error:
2580 	cleanup_glue_dir(dev, glue_dir);
2581 parent_error:
2582 	put_device(parent);
2583 name_error:
2584 	kfree(dev->p);
2585 	dev->p = NULL;
2586 	goto done;
2587 }
2588 EXPORT_SYMBOL_GPL(device_add);
2589 
2590 /**
2591  * device_register - register a device with the system.
2592  * @dev: pointer to the device structure
2593  *
2594  * This happens in two clean steps - initialize the device
2595  * and add it to the system. The two steps can be called
2596  * separately, but this is the easiest and most common.
2597  * I.e. you should only call the two helpers separately if
2598  * have a clearly defined need to use and refcount the device
2599  * before it is added to the hierarchy.
2600  *
2601  * For more information, see the kerneldoc for device_initialize()
2602  * and device_add().
2603  *
2604  * NOTE: _Never_ directly free @dev after calling this function, even
2605  * if it returned an error! Always use put_device() to give up the
2606  * reference initialized in this function instead.
2607  */
2608 int device_register(struct device *dev)
2609 {
2610 	device_initialize(dev);
2611 	return device_add(dev);
2612 }
2613 EXPORT_SYMBOL_GPL(device_register);
2614 
2615 /**
2616  * get_device - increment reference count for device.
2617  * @dev: device.
2618  *
2619  * This simply forwards the call to kobject_get(), though
2620  * we do take care to provide for the case that we get a NULL
2621  * pointer passed in.
2622  */
2623 struct device *get_device(struct device *dev)
2624 {
2625 	return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
2626 }
2627 EXPORT_SYMBOL_GPL(get_device);
2628 
2629 /**
2630  * put_device - decrement reference count.
2631  * @dev: device in question.
2632  */
2633 void put_device(struct device *dev)
2634 {
2635 	/* might_sleep(); */
2636 	if (dev)
2637 		kobject_put(&dev->kobj);
2638 }
2639 EXPORT_SYMBOL_GPL(put_device);
2640 
2641 bool kill_device(struct device *dev)
2642 {
2643 	/*
2644 	 * Require the device lock and set the "dead" flag to guarantee that
2645 	 * the update behavior is consistent with the other bitfields near
2646 	 * it and that we cannot have an asynchronous probe routine trying
2647 	 * to run while we are tearing out the bus/class/sysfs from
2648 	 * underneath the device.
2649 	 */
2650 	lockdep_assert_held(&dev->mutex);
2651 
2652 	if (dev->p->dead)
2653 		return false;
2654 	dev->p->dead = true;
2655 	return true;
2656 }
2657 EXPORT_SYMBOL_GPL(kill_device);
2658 
2659 /**
2660  * device_del - delete device from system.
2661  * @dev: device.
2662  *
2663  * This is the first part of the device unregistration
2664  * sequence. This removes the device from the lists we control
2665  * from here, has it removed from the other driver model
2666  * subsystems it was added to in device_add(), and removes it
2667  * from the kobject hierarchy.
2668  *
2669  * NOTE: this should be called manually _iff_ device_add() was
2670  * also called manually.
2671  */
2672 void device_del(struct device *dev)
2673 {
2674 	struct device *parent = dev->parent;
2675 	struct kobject *glue_dir = NULL;
2676 	struct class_interface *class_intf;
2677 
2678 	device_lock(dev);
2679 	kill_device(dev);
2680 	device_unlock(dev);
2681 
2682 	if (dev->fwnode && dev->fwnode->dev == dev)
2683 		dev->fwnode->dev = NULL;
2684 
2685 	/* Notify clients of device removal.  This call must come
2686 	 * before dpm_sysfs_remove().
2687 	 */
2688 	if (dev->bus)
2689 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2690 					     BUS_NOTIFY_DEL_DEVICE, dev);
2691 
2692 	dpm_sysfs_remove(dev);
2693 	if (parent)
2694 		klist_del(&dev->p->knode_parent);
2695 	if (MAJOR(dev->devt)) {
2696 		devtmpfs_delete_node(dev);
2697 		device_remove_sys_dev_entry(dev);
2698 		device_remove_file(dev, &dev_attr_dev);
2699 	}
2700 	if (dev->class) {
2701 		device_remove_class_symlinks(dev);
2702 
2703 		mutex_lock(&dev->class->p->mutex);
2704 		/* notify any interfaces that the device is now gone */
2705 		list_for_each_entry(class_intf,
2706 				    &dev->class->p->interfaces, node)
2707 			if (class_intf->remove_dev)
2708 				class_intf->remove_dev(dev, class_intf);
2709 		/* remove the device from the class list */
2710 		klist_del(&dev->p->knode_class);
2711 		mutex_unlock(&dev->class->p->mutex);
2712 	}
2713 	device_remove_file(dev, &dev_attr_uevent);
2714 	device_remove_attrs(dev);
2715 	bus_remove_device(dev);
2716 	device_pm_remove(dev);
2717 	driver_deferred_probe_del(dev);
2718 	device_platform_notify(dev, KOBJ_REMOVE);
2719 	device_remove_properties(dev);
2720 	device_links_purge(dev);
2721 
2722 	if (dev->bus)
2723 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2724 					     BUS_NOTIFY_REMOVED_DEVICE, dev);
2725 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2726 	glue_dir = get_glue_dir(dev);
2727 	kobject_del(&dev->kobj);
2728 	cleanup_glue_dir(dev, glue_dir);
2729 	put_device(parent);
2730 }
2731 EXPORT_SYMBOL_GPL(device_del);
2732 
2733 /**
2734  * device_unregister - unregister device from system.
2735  * @dev: device going away.
2736  *
2737  * We do this in two parts, like we do device_register(). First,
2738  * we remove it from all the subsystems with device_del(), then
2739  * we decrement the reference count via put_device(). If that
2740  * is the final reference count, the device will be cleaned up
2741  * via device_release() above. Otherwise, the structure will
2742  * stick around until the final reference to the device is dropped.
2743  */
2744 void device_unregister(struct device *dev)
2745 {
2746 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2747 	device_del(dev);
2748 	put_device(dev);
2749 }
2750 EXPORT_SYMBOL_GPL(device_unregister);
2751 
2752 static struct device *prev_device(struct klist_iter *i)
2753 {
2754 	struct klist_node *n = klist_prev(i);
2755 	struct device *dev = NULL;
2756 	struct device_private *p;
2757 
2758 	if (n) {
2759 		p = to_device_private_parent(n);
2760 		dev = p->device;
2761 	}
2762 	return dev;
2763 }
2764 
2765 static struct device *next_device(struct klist_iter *i)
2766 {
2767 	struct klist_node *n = klist_next(i);
2768 	struct device *dev = NULL;
2769 	struct device_private *p;
2770 
2771 	if (n) {
2772 		p = to_device_private_parent(n);
2773 		dev = p->device;
2774 	}
2775 	return dev;
2776 }
2777 
2778 /**
2779  * device_get_devnode - path of device node file
2780  * @dev: device
2781  * @mode: returned file access mode
2782  * @uid: returned file owner
2783  * @gid: returned file group
2784  * @tmp: possibly allocated string
2785  *
2786  * Return the relative path of a possible device node.
2787  * Non-default names may need to allocate a memory to compose
2788  * a name. This memory is returned in tmp and needs to be
2789  * freed by the caller.
2790  */
2791 const char *device_get_devnode(struct device *dev,
2792 			       umode_t *mode, kuid_t *uid, kgid_t *gid,
2793 			       const char **tmp)
2794 {
2795 	char *s;
2796 
2797 	*tmp = NULL;
2798 
2799 	/* the device type may provide a specific name */
2800 	if (dev->type && dev->type->devnode)
2801 		*tmp = dev->type->devnode(dev, mode, uid, gid);
2802 	if (*tmp)
2803 		return *tmp;
2804 
2805 	/* the class may provide a specific name */
2806 	if (dev->class && dev->class->devnode)
2807 		*tmp = dev->class->devnode(dev, mode);
2808 	if (*tmp)
2809 		return *tmp;
2810 
2811 	/* return name without allocation, tmp == NULL */
2812 	if (strchr(dev_name(dev), '!') == NULL)
2813 		return dev_name(dev);
2814 
2815 	/* replace '!' in the name with '/' */
2816 	s = kstrdup(dev_name(dev), GFP_KERNEL);
2817 	if (!s)
2818 		return NULL;
2819 	strreplace(s, '!', '/');
2820 	return *tmp = s;
2821 }
2822 
2823 /**
2824  * device_for_each_child - device child iterator.
2825  * @parent: parent struct device.
2826  * @fn: function to be called for each device.
2827  * @data: data for the callback.
2828  *
2829  * Iterate over @parent's child devices, and call @fn for each,
2830  * passing it @data.
2831  *
2832  * We check the return of @fn each time. If it returns anything
2833  * other than 0, we break out and return that value.
2834  */
2835 int device_for_each_child(struct device *parent, void *data,
2836 			  int (*fn)(struct device *dev, void *data))
2837 {
2838 	struct klist_iter i;
2839 	struct device *child;
2840 	int error = 0;
2841 
2842 	if (!parent->p)
2843 		return 0;
2844 
2845 	klist_iter_init(&parent->p->klist_children, &i);
2846 	while (!error && (child = next_device(&i)))
2847 		error = fn(child, data);
2848 	klist_iter_exit(&i);
2849 	return error;
2850 }
2851 EXPORT_SYMBOL_GPL(device_for_each_child);
2852 
2853 /**
2854  * device_for_each_child_reverse - device child iterator in reversed order.
2855  * @parent: parent struct device.
2856  * @fn: function to be called for each device.
2857  * @data: data for the callback.
2858  *
2859  * Iterate over @parent's child devices, and call @fn for each,
2860  * passing it @data.
2861  *
2862  * We check the return of @fn each time. If it returns anything
2863  * other than 0, we break out and return that value.
2864  */
2865 int device_for_each_child_reverse(struct device *parent, void *data,
2866 				  int (*fn)(struct device *dev, void *data))
2867 {
2868 	struct klist_iter i;
2869 	struct device *child;
2870 	int error = 0;
2871 
2872 	if (!parent->p)
2873 		return 0;
2874 
2875 	klist_iter_init(&parent->p->klist_children, &i);
2876 	while ((child = prev_device(&i)) && !error)
2877 		error = fn(child, data);
2878 	klist_iter_exit(&i);
2879 	return error;
2880 }
2881 EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2882 
2883 /**
2884  * device_find_child - device iterator for locating a particular device.
2885  * @parent: parent struct device
2886  * @match: Callback function to check device
2887  * @data: Data to pass to match function
2888  *
2889  * This is similar to the device_for_each_child() function above, but it
2890  * returns a reference to a device that is 'found' for later use, as
2891  * determined by the @match callback.
2892  *
2893  * The callback should return 0 if the device doesn't match and non-zero
2894  * if it does.  If the callback returns non-zero and a reference to the
2895  * current device can be obtained, this function will return to the caller
2896  * and not iterate over any more devices.
2897  *
2898  * NOTE: you will need to drop the reference with put_device() after use.
2899  */
2900 struct device *device_find_child(struct device *parent, void *data,
2901 				 int (*match)(struct device *dev, void *data))
2902 {
2903 	struct klist_iter i;
2904 	struct device *child;
2905 
2906 	if (!parent)
2907 		return NULL;
2908 
2909 	klist_iter_init(&parent->p->klist_children, &i);
2910 	while ((child = next_device(&i)))
2911 		if (match(child, data) && get_device(child))
2912 			break;
2913 	klist_iter_exit(&i);
2914 	return child;
2915 }
2916 EXPORT_SYMBOL_GPL(device_find_child);
2917 
2918 /**
2919  * device_find_child_by_name - device iterator for locating a child device.
2920  * @parent: parent struct device
2921  * @name: name of the child device
2922  *
2923  * This is similar to the device_find_child() function above, but it
2924  * returns a reference to a device that has the name @name.
2925  *
2926  * NOTE: you will need to drop the reference with put_device() after use.
2927  */
2928 struct device *device_find_child_by_name(struct device *parent,
2929 					 const char *name)
2930 {
2931 	struct klist_iter i;
2932 	struct device *child;
2933 
2934 	if (!parent)
2935 		return NULL;
2936 
2937 	klist_iter_init(&parent->p->klist_children, &i);
2938 	while ((child = next_device(&i)))
2939 		if (!strcmp(dev_name(child), name) && get_device(child))
2940 			break;
2941 	klist_iter_exit(&i);
2942 	return child;
2943 }
2944 EXPORT_SYMBOL_GPL(device_find_child_by_name);
2945 
2946 int __init devices_init(void)
2947 {
2948 	devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2949 	if (!devices_kset)
2950 		return -ENOMEM;
2951 	dev_kobj = kobject_create_and_add("dev", NULL);
2952 	if (!dev_kobj)
2953 		goto dev_kobj_err;
2954 	sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2955 	if (!sysfs_dev_block_kobj)
2956 		goto block_kobj_err;
2957 	sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2958 	if (!sysfs_dev_char_kobj)
2959 		goto char_kobj_err;
2960 
2961 	return 0;
2962 
2963  char_kobj_err:
2964 	kobject_put(sysfs_dev_block_kobj);
2965  block_kobj_err:
2966 	kobject_put(dev_kobj);
2967  dev_kobj_err:
2968 	kset_unregister(devices_kset);
2969 	return -ENOMEM;
2970 }
2971 
2972 static int device_check_offline(struct device *dev, void *not_used)
2973 {
2974 	int ret;
2975 
2976 	ret = device_for_each_child(dev, NULL, device_check_offline);
2977 	if (ret)
2978 		return ret;
2979 
2980 	return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2981 }
2982 
2983 /**
2984  * device_offline - Prepare the device for hot-removal.
2985  * @dev: Device to be put offline.
2986  *
2987  * Execute the device bus type's .offline() callback, if present, to prepare
2988  * the device for a subsequent hot-removal.  If that succeeds, the device must
2989  * not be used until either it is removed or its bus type's .online() callback
2990  * is executed.
2991  *
2992  * Call under device_hotplug_lock.
2993  */
2994 int device_offline(struct device *dev)
2995 {
2996 	int ret;
2997 
2998 	if (dev->offline_disabled)
2999 		return -EPERM;
3000 
3001 	ret = device_for_each_child(dev, NULL, device_check_offline);
3002 	if (ret)
3003 		return ret;
3004 
3005 	device_lock(dev);
3006 	if (device_supports_offline(dev)) {
3007 		if (dev->offline) {
3008 			ret = 1;
3009 		} else {
3010 			ret = dev->bus->offline(dev);
3011 			if (!ret) {
3012 				kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
3013 				dev->offline = true;
3014 			}
3015 		}
3016 	}
3017 	device_unlock(dev);
3018 
3019 	return ret;
3020 }
3021 
3022 /**
3023  * device_online - Put the device back online after successful device_offline().
3024  * @dev: Device to be put back online.
3025  *
3026  * If device_offline() has been successfully executed for @dev, but the device
3027  * has not been removed subsequently, execute its bus type's .online() callback
3028  * to indicate that the device can be used again.
3029  *
3030  * Call under device_hotplug_lock.
3031  */
3032 int device_online(struct device *dev)
3033 {
3034 	int ret = 0;
3035 
3036 	device_lock(dev);
3037 	if (device_supports_offline(dev)) {
3038 		if (dev->offline) {
3039 			ret = dev->bus->online(dev);
3040 			if (!ret) {
3041 				kobject_uevent(&dev->kobj, KOBJ_ONLINE);
3042 				dev->offline = false;
3043 			}
3044 		} else {
3045 			ret = 1;
3046 		}
3047 	}
3048 	device_unlock(dev);
3049 
3050 	return ret;
3051 }
3052 
3053 struct root_device {
3054 	struct device dev;
3055 	struct module *owner;
3056 };
3057 
3058 static inline struct root_device *to_root_device(struct device *d)
3059 {
3060 	return container_of(d, struct root_device, dev);
3061 }
3062 
3063 static void root_device_release(struct device *dev)
3064 {
3065 	kfree(to_root_device(dev));
3066 }
3067 
3068 /**
3069  * __root_device_register - allocate and register a root device
3070  * @name: root device name
3071  * @owner: owner module of the root device, usually THIS_MODULE
3072  *
3073  * This function allocates a root device and registers it
3074  * using device_register(). In order to free the returned
3075  * device, use root_device_unregister().
3076  *
3077  * Root devices are dummy devices which allow other devices
3078  * to be grouped under /sys/devices. Use this function to
3079  * allocate a root device and then use it as the parent of
3080  * any device which should appear under /sys/devices/{name}
3081  *
3082  * The /sys/devices/{name} directory will also contain a
3083  * 'module' symlink which points to the @owner directory
3084  * in sysfs.
3085  *
3086  * Returns &struct device pointer on success, or ERR_PTR() on error.
3087  *
3088  * Note: You probably want to use root_device_register().
3089  */
3090 struct device *__root_device_register(const char *name, struct module *owner)
3091 {
3092 	struct root_device *root;
3093 	int err = -ENOMEM;
3094 
3095 	root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
3096 	if (!root)
3097 		return ERR_PTR(err);
3098 
3099 	err = dev_set_name(&root->dev, "%s", name);
3100 	if (err) {
3101 		kfree(root);
3102 		return ERR_PTR(err);
3103 	}
3104 
3105 	root->dev.release = root_device_release;
3106 
3107 	err = device_register(&root->dev);
3108 	if (err) {
3109 		put_device(&root->dev);
3110 		return ERR_PTR(err);
3111 	}
3112 
3113 #ifdef CONFIG_MODULES	/* gotta find a "cleaner" way to do this */
3114 	if (owner) {
3115 		struct module_kobject *mk = &owner->mkobj;
3116 
3117 		err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
3118 		if (err) {
3119 			device_unregister(&root->dev);
3120 			return ERR_PTR(err);
3121 		}
3122 		root->owner = owner;
3123 	}
3124 #endif
3125 
3126 	return &root->dev;
3127 }
3128 EXPORT_SYMBOL_GPL(__root_device_register);
3129 
3130 /**
3131  * root_device_unregister - unregister and free a root device
3132  * @dev: device going away
3133  *
3134  * This function unregisters and cleans up a device that was created by
3135  * root_device_register().
3136  */
3137 void root_device_unregister(struct device *dev)
3138 {
3139 	struct root_device *root = to_root_device(dev);
3140 
3141 	if (root->owner)
3142 		sysfs_remove_link(&root->dev.kobj, "module");
3143 
3144 	device_unregister(dev);
3145 }
3146 EXPORT_SYMBOL_GPL(root_device_unregister);
3147 
3148 
3149 static void device_create_release(struct device *dev)
3150 {
3151 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3152 	kfree(dev);
3153 }
3154 
3155 static __printf(6, 0) struct device *
3156 device_create_groups_vargs(struct class *class, struct device *parent,
3157 			   dev_t devt, void *drvdata,
3158 			   const struct attribute_group **groups,
3159 			   const char *fmt, va_list args)
3160 {
3161 	struct device *dev = NULL;
3162 	int retval = -ENODEV;
3163 
3164 	if (class == NULL || IS_ERR(class))
3165 		goto error;
3166 
3167 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3168 	if (!dev) {
3169 		retval = -ENOMEM;
3170 		goto error;
3171 	}
3172 
3173 	device_initialize(dev);
3174 	dev->devt = devt;
3175 	dev->class = class;
3176 	dev->parent = parent;
3177 	dev->groups = groups;
3178 	dev->release = device_create_release;
3179 	dev_set_drvdata(dev, drvdata);
3180 
3181 	retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
3182 	if (retval)
3183 		goto error;
3184 
3185 	retval = device_add(dev);
3186 	if (retval)
3187 		goto error;
3188 
3189 	return dev;
3190 
3191 error:
3192 	put_device(dev);
3193 	return ERR_PTR(retval);
3194 }
3195 
3196 /**
3197  * device_create_vargs - creates a device and registers it with sysfs
3198  * @class: pointer to the struct class that this device should be registered to
3199  * @parent: pointer to the parent struct device of this new device, if any
3200  * @devt: the dev_t for the char device to be added
3201  * @drvdata: the data to be added to the device for callbacks
3202  * @fmt: string for the device's name
3203  * @args: va_list for the device's name
3204  *
3205  * This function can be used by char device classes.  A struct device
3206  * will be created in sysfs, registered to the specified class.
3207  *
3208  * A "dev" file will be created, showing the dev_t for the device, if
3209  * the dev_t is not 0,0.
3210  * If a pointer to a parent struct device is passed in, the newly created
3211  * struct device will be a child of that device in sysfs.
3212  * The pointer to the struct device will be returned from the call.
3213  * Any further sysfs files that might be required can be created using this
3214  * pointer.
3215  *
3216  * Returns &struct device pointer on success, or ERR_PTR() on error.
3217  *
3218  * Note: the struct class passed to this function must have previously
3219  * been created with a call to class_create().
3220  */
3221 struct device *device_create_vargs(struct class *class, struct device *parent,
3222 				   dev_t devt, void *drvdata, const char *fmt,
3223 				   va_list args)
3224 {
3225 	return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
3226 					  fmt, args);
3227 }
3228 EXPORT_SYMBOL_GPL(device_create_vargs);
3229 
3230 /**
3231  * device_create - creates a device and registers it with sysfs
3232  * @class: pointer to the struct class that this device should be registered to
3233  * @parent: pointer to the parent struct device of this new device, if any
3234  * @devt: the dev_t for the char device to be added
3235  * @drvdata: the data to be added to the device for callbacks
3236  * @fmt: string for the device's name
3237  *
3238  * This function can be used by char device classes.  A struct device
3239  * will be created in sysfs, registered to the specified class.
3240  *
3241  * A "dev" file will be created, showing the dev_t for the device, if
3242  * the dev_t is not 0,0.
3243  * If a pointer to a parent struct device is passed in, the newly created
3244  * struct device will be a child of that device in sysfs.
3245  * The pointer to the struct device will be returned from the call.
3246  * Any further sysfs files that might be required can be created using this
3247  * pointer.
3248  *
3249  * Returns &struct device pointer on success, or ERR_PTR() on error.
3250  *
3251  * Note: the struct class passed to this function must have previously
3252  * been created with a call to class_create().
3253  */
3254 struct device *device_create(struct class *class, struct device *parent,
3255 			     dev_t devt, void *drvdata, const char *fmt, ...)
3256 {
3257 	va_list vargs;
3258 	struct device *dev;
3259 
3260 	va_start(vargs, fmt);
3261 	dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
3262 	va_end(vargs);
3263 	return dev;
3264 }
3265 EXPORT_SYMBOL_GPL(device_create);
3266 
3267 /**
3268  * device_create_with_groups - creates a device and registers it with sysfs
3269  * @class: pointer to the struct class that this device should be registered to
3270  * @parent: pointer to the parent struct device of this new device, if any
3271  * @devt: the dev_t for the char device to be added
3272  * @drvdata: the data to be added to the device for callbacks
3273  * @groups: NULL-terminated list of attribute groups to be created
3274  * @fmt: string for the device's name
3275  *
3276  * This function can be used by char device classes.  A struct device
3277  * will be created in sysfs, registered to the specified class.
3278  * Additional attributes specified in the groups parameter will also
3279  * be created automatically.
3280  *
3281  * A "dev" file will be created, showing the dev_t for the device, if
3282  * the dev_t is not 0,0.
3283  * If a pointer to a parent struct device is passed in, the newly created
3284  * struct device will be a child of that device in sysfs.
3285  * The pointer to the struct device will be returned from the call.
3286  * Any further sysfs files that might be required can be created using this
3287  * pointer.
3288  *
3289  * Returns &struct device pointer on success, or ERR_PTR() on error.
3290  *
3291  * Note: the struct class passed to this function must have previously
3292  * been created with a call to class_create().
3293  */
3294 struct device *device_create_with_groups(struct class *class,
3295 					 struct device *parent, dev_t devt,
3296 					 void *drvdata,
3297 					 const struct attribute_group **groups,
3298 					 const char *fmt, ...)
3299 {
3300 	va_list vargs;
3301 	struct device *dev;
3302 
3303 	va_start(vargs, fmt);
3304 	dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
3305 					 fmt, vargs);
3306 	va_end(vargs);
3307 	return dev;
3308 }
3309 EXPORT_SYMBOL_GPL(device_create_with_groups);
3310 
3311 /**
3312  * device_destroy - removes a device that was created with device_create()
3313  * @class: pointer to the struct class that this device was registered with
3314  * @devt: the dev_t of the device that was previously registered
3315  *
3316  * This call unregisters and cleans up a device that was created with a
3317  * call to device_create().
3318  */
3319 void device_destroy(struct class *class, dev_t devt)
3320 {
3321 	struct device *dev;
3322 
3323 	dev = class_find_device_by_devt(class, devt);
3324 	if (dev) {
3325 		put_device(dev);
3326 		device_unregister(dev);
3327 	}
3328 }
3329 EXPORT_SYMBOL_GPL(device_destroy);
3330 
3331 /**
3332  * device_rename - renames a device
3333  * @dev: the pointer to the struct device to be renamed
3334  * @new_name: the new name of the device
3335  *
3336  * It is the responsibility of the caller to provide mutual
3337  * exclusion between two different calls of device_rename
3338  * on the same device to ensure that new_name is valid and
3339  * won't conflict with other devices.
3340  *
3341  * Note: Don't call this function.  Currently, the networking layer calls this
3342  * function, but that will change.  The following text from Kay Sievers offers
3343  * some insight:
3344  *
3345  * Renaming devices is racy at many levels, symlinks and other stuff are not
3346  * replaced atomically, and you get a "move" uevent, but it's not easy to
3347  * connect the event to the old and new device. Device nodes are not renamed at
3348  * all, there isn't even support for that in the kernel now.
3349  *
3350  * In the meantime, during renaming, your target name might be taken by another
3351  * driver, creating conflicts. Or the old name is taken directly after you
3352  * renamed it -- then you get events for the same DEVPATH, before you even see
3353  * the "move" event. It's just a mess, and nothing new should ever rely on
3354  * kernel device renaming. Besides that, it's not even implemented now for
3355  * other things than (driver-core wise very simple) network devices.
3356  *
3357  * We are currently about to change network renaming in udev to completely
3358  * disallow renaming of devices in the same namespace as the kernel uses,
3359  * because we can't solve the problems properly, that arise with swapping names
3360  * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
3361  * be allowed to some other name than eth[0-9]*, for the aforementioned
3362  * reasons.
3363  *
3364  * Make up a "real" name in the driver before you register anything, or add
3365  * some other attributes for userspace to find the device, or use udev to add
3366  * symlinks -- but never rename kernel devices later, it's a complete mess. We
3367  * don't even want to get into that and try to implement the missing pieces in
3368  * the core. We really have other pieces to fix in the driver core mess. :)
3369  */
3370 int device_rename(struct device *dev, const char *new_name)
3371 {
3372 	struct kobject *kobj = &dev->kobj;
3373 	char *old_device_name = NULL;
3374 	int error;
3375 
3376 	dev = get_device(dev);
3377 	if (!dev)
3378 		return -EINVAL;
3379 
3380 	dev_dbg(dev, "renaming to %s\n", new_name);
3381 
3382 	old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
3383 	if (!old_device_name) {
3384 		error = -ENOMEM;
3385 		goto out;
3386 	}
3387 
3388 	if (dev->class) {
3389 		error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
3390 					     kobj, old_device_name,
3391 					     new_name, kobject_namespace(kobj));
3392 		if (error)
3393 			goto out;
3394 	}
3395 
3396 	error = kobject_rename(kobj, new_name);
3397 	if (error)
3398 		goto out;
3399 
3400 out:
3401 	put_device(dev);
3402 
3403 	kfree(old_device_name);
3404 
3405 	return error;
3406 }
3407 EXPORT_SYMBOL_GPL(device_rename);
3408 
3409 static int device_move_class_links(struct device *dev,
3410 				   struct device *old_parent,
3411 				   struct device *new_parent)
3412 {
3413 	int error = 0;
3414 
3415 	if (old_parent)
3416 		sysfs_remove_link(&dev->kobj, "device");
3417 	if (new_parent)
3418 		error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
3419 					  "device");
3420 	return error;
3421 }
3422 
3423 /**
3424  * device_move - moves a device to a new parent
3425  * @dev: the pointer to the struct device to be moved
3426  * @new_parent: the new parent of the device (can be NULL)
3427  * @dpm_order: how to reorder the dpm_list
3428  */
3429 int device_move(struct device *dev, struct device *new_parent,
3430 		enum dpm_order dpm_order)
3431 {
3432 	int error;
3433 	struct device *old_parent;
3434 	struct kobject *new_parent_kobj;
3435 
3436 	dev = get_device(dev);
3437 	if (!dev)
3438 		return -EINVAL;
3439 
3440 	device_pm_lock();
3441 	new_parent = get_device(new_parent);
3442 	new_parent_kobj = get_device_parent(dev, new_parent);
3443 	if (IS_ERR(new_parent_kobj)) {
3444 		error = PTR_ERR(new_parent_kobj);
3445 		put_device(new_parent);
3446 		goto out;
3447 	}
3448 
3449 	pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
3450 		 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
3451 	error = kobject_move(&dev->kobj, new_parent_kobj);
3452 	if (error) {
3453 		cleanup_glue_dir(dev, new_parent_kobj);
3454 		put_device(new_parent);
3455 		goto out;
3456 	}
3457 	old_parent = dev->parent;
3458 	dev->parent = new_parent;
3459 	if (old_parent)
3460 		klist_remove(&dev->p->knode_parent);
3461 	if (new_parent) {
3462 		klist_add_tail(&dev->p->knode_parent,
3463 			       &new_parent->p->klist_children);
3464 		set_dev_node(dev, dev_to_node(new_parent));
3465 	}
3466 
3467 	if (dev->class) {
3468 		error = device_move_class_links(dev, old_parent, new_parent);
3469 		if (error) {
3470 			/* We ignore errors on cleanup since we're hosed anyway... */
3471 			device_move_class_links(dev, new_parent, old_parent);
3472 			if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
3473 				if (new_parent)
3474 					klist_remove(&dev->p->knode_parent);
3475 				dev->parent = old_parent;
3476 				if (old_parent) {
3477 					klist_add_tail(&dev->p->knode_parent,
3478 						       &old_parent->p->klist_children);
3479 					set_dev_node(dev, dev_to_node(old_parent));
3480 				}
3481 			}
3482 			cleanup_glue_dir(dev, new_parent_kobj);
3483 			put_device(new_parent);
3484 			goto out;
3485 		}
3486 	}
3487 	switch (dpm_order) {
3488 	case DPM_ORDER_NONE:
3489 		break;
3490 	case DPM_ORDER_DEV_AFTER_PARENT:
3491 		device_pm_move_after(dev, new_parent);
3492 		devices_kset_move_after(dev, new_parent);
3493 		break;
3494 	case DPM_ORDER_PARENT_BEFORE_DEV:
3495 		device_pm_move_before(new_parent, dev);
3496 		devices_kset_move_before(new_parent, dev);
3497 		break;
3498 	case DPM_ORDER_DEV_LAST:
3499 		device_pm_move_last(dev);
3500 		devices_kset_move_last(dev);
3501 		break;
3502 	}
3503 
3504 	put_device(old_parent);
3505 out:
3506 	device_pm_unlock();
3507 	put_device(dev);
3508 	return error;
3509 }
3510 EXPORT_SYMBOL_GPL(device_move);
3511 
3512 static int device_attrs_change_owner(struct device *dev, kuid_t kuid,
3513 				     kgid_t kgid)
3514 {
3515 	struct kobject *kobj = &dev->kobj;
3516 	struct class *class = dev->class;
3517 	const struct device_type *type = dev->type;
3518 	int error;
3519 
3520 	if (class) {
3521 		/*
3522 		 * Change the device groups of the device class for @dev to
3523 		 * @kuid/@kgid.
3524 		 */
3525 		error = sysfs_groups_change_owner(kobj, class->dev_groups, kuid,
3526 						  kgid);
3527 		if (error)
3528 			return error;
3529 	}
3530 
3531 	if (type) {
3532 		/*
3533 		 * Change the device groups of the device type for @dev to
3534 		 * @kuid/@kgid.
3535 		 */
3536 		error = sysfs_groups_change_owner(kobj, type->groups, kuid,
3537 						  kgid);
3538 		if (error)
3539 			return error;
3540 	}
3541 
3542 	/* Change the device groups of @dev to @kuid/@kgid. */
3543 	error = sysfs_groups_change_owner(kobj, dev->groups, kuid, kgid);
3544 	if (error)
3545 		return error;
3546 
3547 	if (device_supports_offline(dev) && !dev->offline_disabled) {
3548 		/* Change online device attributes of @dev to @kuid/@kgid. */
3549 		error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name,
3550 						kuid, kgid);
3551 		if (error)
3552 			return error;
3553 	}
3554 
3555 	return 0;
3556 }
3557 
3558 /**
3559  * device_change_owner - change the owner of an existing device.
3560  * @dev: device.
3561  * @kuid: new owner's kuid
3562  * @kgid: new owner's kgid
3563  *
3564  * This changes the owner of @dev and its corresponding sysfs entries to
3565  * @kuid/@kgid. This function closely mirrors how @dev was added via driver
3566  * core.
3567  *
3568  * Returns 0 on success or error code on failure.
3569  */
3570 int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
3571 {
3572 	int error;
3573 	struct kobject *kobj = &dev->kobj;
3574 
3575 	dev = get_device(dev);
3576 	if (!dev)
3577 		return -EINVAL;
3578 
3579 	/*
3580 	 * Change the kobject and the default attributes and groups of the
3581 	 * ktype associated with it to @kuid/@kgid.
3582 	 */
3583 	error = sysfs_change_owner(kobj, kuid, kgid);
3584 	if (error)
3585 		goto out;
3586 
3587 	/*
3588 	 * Change the uevent file for @dev to the new owner. The uevent file
3589 	 * was created in a separate step when @dev got added and we mirror
3590 	 * that step here.
3591 	 */
3592 	error = sysfs_file_change_owner(kobj, dev_attr_uevent.attr.name, kuid,
3593 					kgid);
3594 	if (error)
3595 		goto out;
3596 
3597 	/*
3598 	 * Change the device groups, the device groups associated with the
3599 	 * device class, and the groups associated with the device type of @dev
3600 	 * to @kuid/@kgid.
3601 	 */
3602 	error = device_attrs_change_owner(dev, kuid, kgid);
3603 	if (error)
3604 		goto out;
3605 
3606 	error = dpm_sysfs_change_owner(dev, kuid, kgid);
3607 	if (error)
3608 		goto out;
3609 
3610 #ifdef CONFIG_BLOCK
3611 	if (sysfs_deprecated && dev->class == &block_class)
3612 		goto out;
3613 #endif
3614 
3615 	/*
3616 	 * Change the owner of the symlink located in the class directory of
3617 	 * the device class associated with @dev which points to the actual
3618 	 * directory entry for @dev to @kuid/@kgid. This ensures that the
3619 	 * symlink shows the same permissions as its target.
3620 	 */
3621 	error = sysfs_link_change_owner(&dev->class->p->subsys.kobj, &dev->kobj,
3622 					dev_name(dev), kuid, kgid);
3623 	if (error)
3624 		goto out;
3625 
3626 out:
3627 	put_device(dev);
3628 	return error;
3629 }
3630 EXPORT_SYMBOL_GPL(device_change_owner);
3631 
3632 /**
3633  * device_shutdown - call ->shutdown() on each device to shutdown.
3634  */
3635 void device_shutdown(void)
3636 {
3637 	struct device *dev, *parent;
3638 
3639 	wait_for_device_probe();
3640 	device_block_probing();
3641 
3642 	cpufreq_suspend();
3643 
3644 	spin_lock(&devices_kset->list_lock);
3645 	/*
3646 	 * Walk the devices list backward, shutting down each in turn.
3647 	 * Beware that device unplug events may also start pulling
3648 	 * devices offline, even as the system is shutting down.
3649 	 */
3650 	while (!list_empty(&devices_kset->list)) {
3651 		dev = list_entry(devices_kset->list.prev, struct device,
3652 				kobj.entry);
3653 
3654 		/*
3655 		 * hold reference count of device's parent to
3656 		 * prevent it from being freed because parent's
3657 		 * lock is to be held
3658 		 */
3659 		parent = get_device(dev->parent);
3660 		get_device(dev);
3661 		/*
3662 		 * Make sure the device is off the kset list, in the
3663 		 * event that dev->*->shutdown() doesn't remove it.
3664 		 */
3665 		list_del_init(&dev->kobj.entry);
3666 		spin_unlock(&devices_kset->list_lock);
3667 
3668 		/* hold lock to avoid race with probe/release */
3669 		if (parent)
3670 			device_lock(parent);
3671 		device_lock(dev);
3672 
3673 		/* Don't allow any more runtime suspends */
3674 		pm_runtime_get_noresume(dev);
3675 		pm_runtime_barrier(dev);
3676 
3677 		if (dev->class && dev->class->shutdown_pre) {
3678 			if (initcall_debug)
3679 				dev_info(dev, "shutdown_pre\n");
3680 			dev->class->shutdown_pre(dev);
3681 		}
3682 		if (dev->bus && dev->bus->shutdown) {
3683 			if (initcall_debug)
3684 				dev_info(dev, "shutdown\n");
3685 			dev->bus->shutdown(dev);
3686 		} else if (dev->driver && dev->driver->shutdown) {
3687 			if (initcall_debug)
3688 				dev_info(dev, "shutdown\n");
3689 			dev->driver->shutdown(dev);
3690 		}
3691 
3692 		device_unlock(dev);
3693 		if (parent)
3694 			device_unlock(parent);
3695 
3696 		put_device(dev);
3697 		put_device(parent);
3698 
3699 		spin_lock(&devices_kset->list_lock);
3700 	}
3701 	spin_unlock(&devices_kset->list_lock);
3702 }
3703 
3704 /*
3705  * Device logging functions
3706  */
3707 
3708 #ifdef CONFIG_PRINTK
3709 static int
3710 create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
3711 {
3712 	const char *subsys;
3713 	size_t pos = 0;
3714 
3715 	if (dev->class)
3716 		subsys = dev->class->name;
3717 	else if (dev->bus)
3718 		subsys = dev->bus->name;
3719 	else
3720 		return 0;
3721 
3722 	pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
3723 	if (pos >= hdrlen)
3724 		goto overflow;
3725 
3726 	/*
3727 	 * Add device identifier DEVICE=:
3728 	 *   b12:8         block dev_t
3729 	 *   c127:3        char dev_t
3730 	 *   n8            netdev ifindex
3731 	 *   +sound:card0  subsystem:devname
3732 	 */
3733 	if (MAJOR(dev->devt)) {
3734 		char c;
3735 
3736 		if (strcmp(subsys, "block") == 0)
3737 			c = 'b';
3738 		else
3739 			c = 'c';
3740 		pos++;
3741 		pos += snprintf(hdr + pos, hdrlen - pos,
3742 				"DEVICE=%c%u:%u",
3743 				c, MAJOR(dev->devt), MINOR(dev->devt));
3744 	} else if (strcmp(subsys, "net") == 0) {
3745 		struct net_device *net = to_net_dev(dev);
3746 
3747 		pos++;
3748 		pos += snprintf(hdr + pos, hdrlen - pos,
3749 				"DEVICE=n%u", net->ifindex);
3750 	} else {
3751 		pos++;
3752 		pos += snprintf(hdr + pos, hdrlen - pos,
3753 				"DEVICE=+%s:%s", subsys, dev_name(dev));
3754 	}
3755 
3756 	if (pos >= hdrlen)
3757 		goto overflow;
3758 
3759 	return pos;
3760 
3761 overflow:
3762 	dev_WARN(dev, "device/subsystem name too long");
3763 	return 0;
3764 }
3765 
3766 int dev_vprintk_emit(int level, const struct device *dev,
3767 		     const char *fmt, va_list args)
3768 {
3769 	char hdr[128];
3770 	size_t hdrlen;
3771 
3772 	hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
3773 
3774 	return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
3775 }
3776 EXPORT_SYMBOL(dev_vprintk_emit);
3777 
3778 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
3779 {
3780 	va_list args;
3781 	int r;
3782 
3783 	va_start(args, fmt);
3784 
3785 	r = dev_vprintk_emit(level, dev, fmt, args);
3786 
3787 	va_end(args);
3788 
3789 	return r;
3790 }
3791 EXPORT_SYMBOL(dev_printk_emit);
3792 
3793 static void __dev_printk(const char *level, const struct device *dev,
3794 			struct va_format *vaf)
3795 {
3796 	if (dev)
3797 		dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
3798 				dev_driver_string(dev), dev_name(dev), vaf);
3799 	else
3800 		printk("%s(NULL device *): %pV", level, vaf);
3801 }
3802 
3803 void dev_printk(const char *level, const struct device *dev,
3804 		const char *fmt, ...)
3805 {
3806 	struct va_format vaf;
3807 	va_list args;
3808 
3809 	va_start(args, fmt);
3810 
3811 	vaf.fmt = fmt;
3812 	vaf.va = &args;
3813 
3814 	__dev_printk(level, dev, &vaf);
3815 
3816 	va_end(args);
3817 }
3818 EXPORT_SYMBOL(dev_printk);
3819 
3820 #define define_dev_printk_level(func, kern_level)		\
3821 void func(const struct device *dev, const char *fmt, ...)	\
3822 {								\
3823 	struct va_format vaf;					\
3824 	va_list args;						\
3825 								\
3826 	va_start(args, fmt);					\
3827 								\
3828 	vaf.fmt = fmt;						\
3829 	vaf.va = &args;						\
3830 								\
3831 	__dev_printk(kern_level, dev, &vaf);			\
3832 								\
3833 	va_end(args);						\
3834 }								\
3835 EXPORT_SYMBOL(func);
3836 
3837 define_dev_printk_level(_dev_emerg, KERN_EMERG);
3838 define_dev_printk_level(_dev_alert, KERN_ALERT);
3839 define_dev_printk_level(_dev_crit, KERN_CRIT);
3840 define_dev_printk_level(_dev_err, KERN_ERR);
3841 define_dev_printk_level(_dev_warn, KERN_WARNING);
3842 define_dev_printk_level(_dev_notice, KERN_NOTICE);
3843 define_dev_printk_level(_dev_info, KERN_INFO);
3844 
3845 #endif
3846 
3847 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
3848 {
3849 	return fwnode && !IS_ERR(fwnode->secondary);
3850 }
3851 
3852 /**
3853  * set_primary_fwnode - Change the primary firmware node of a given device.
3854  * @dev: Device to handle.
3855  * @fwnode: New primary firmware node of the device.
3856  *
3857  * Set the device's firmware node pointer to @fwnode, but if a secondary
3858  * firmware node of the device is present, preserve it.
3859  */
3860 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3861 {
3862 	if (fwnode) {
3863 		struct fwnode_handle *fn = dev->fwnode;
3864 
3865 		if (fwnode_is_primary(fn))
3866 			fn = fn->secondary;
3867 
3868 		if (fn) {
3869 			WARN_ON(fwnode->secondary);
3870 			fwnode->secondary = fn;
3871 		}
3872 		dev->fwnode = fwnode;
3873 	} else {
3874 		dev->fwnode = fwnode_is_primary(dev->fwnode) ?
3875 			dev->fwnode->secondary : NULL;
3876 	}
3877 }
3878 EXPORT_SYMBOL_GPL(set_primary_fwnode);
3879 
3880 /**
3881  * set_secondary_fwnode - Change the secondary firmware node of a given device.
3882  * @dev: Device to handle.
3883  * @fwnode: New secondary firmware node of the device.
3884  *
3885  * If a primary firmware node of the device is present, set its secondary
3886  * pointer to @fwnode.  Otherwise, set the device's firmware node pointer to
3887  * @fwnode.
3888  */
3889 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3890 {
3891 	if (fwnode)
3892 		fwnode->secondary = ERR_PTR(-ENODEV);
3893 
3894 	if (fwnode_is_primary(dev->fwnode))
3895 		dev->fwnode->secondary = fwnode;
3896 	else
3897 		dev->fwnode = fwnode;
3898 }
3899 
3900 /**
3901  * device_set_of_node_from_dev - reuse device-tree node of another device
3902  * @dev: device whose device-tree node is being set
3903  * @dev2: device whose device-tree node is being reused
3904  *
3905  * Takes another reference to the new device-tree node after first dropping
3906  * any reference held to the old node.
3907  */
3908 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3909 {
3910 	of_node_put(dev->of_node);
3911 	dev->of_node = of_node_get(dev2->of_node);
3912 	dev->of_node_reused = true;
3913 }
3914 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
3915 
3916 int device_match_name(struct device *dev, const void *name)
3917 {
3918 	return sysfs_streq(dev_name(dev), name);
3919 }
3920 EXPORT_SYMBOL_GPL(device_match_name);
3921 
3922 int device_match_of_node(struct device *dev, const void *np)
3923 {
3924 	return dev->of_node == np;
3925 }
3926 EXPORT_SYMBOL_GPL(device_match_of_node);
3927 
3928 int device_match_fwnode(struct device *dev, const void *fwnode)
3929 {
3930 	return dev_fwnode(dev) == fwnode;
3931 }
3932 EXPORT_SYMBOL_GPL(device_match_fwnode);
3933 
3934 int device_match_devt(struct device *dev, const void *pdevt)
3935 {
3936 	return dev->devt == *(dev_t *)pdevt;
3937 }
3938 EXPORT_SYMBOL_GPL(device_match_devt);
3939 
3940 int device_match_acpi_dev(struct device *dev, const void *adev)
3941 {
3942 	return ACPI_COMPANION(dev) == adev;
3943 }
3944 EXPORT_SYMBOL(device_match_acpi_dev);
3945 
3946 int device_match_any(struct device *dev, const void *unused)
3947 {
3948 	return 1;
3949 }
3950 EXPORT_SYMBOL_GPL(device_match_any);
3951