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