xref: /openbmc/linux/drivers/base/core.c (revision 4495dfdd)
1989d42e8SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * drivers/base/core.c - core driver model code (device registration, etc)
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (c) 2002-3 Patrick Mochel
61da177e4SLinus Torvalds  * Copyright (c) 2002-3 Open Source Development Labs
764bb5d2cSGreg Kroah-Hartman  * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
864bb5d2cSGreg Kroah-Hartman  * Copyright (c) 2006 Novell, Inc.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
117847a145SHeikki Krogerus #include <linux/acpi.h>
121da177e4SLinus Torvalds #include <linux/device.h>
131da177e4SLinus Torvalds #include <linux/err.h>
1497badf87SRafael J. Wysocki #include <linux/fwnode.h>
151da177e4SLinus Torvalds #include <linux/init.h>
161da177e4SLinus Torvalds #include <linux/module.h>
171da177e4SLinus Torvalds #include <linux/slab.h>
181da177e4SLinus Torvalds #include <linux/string.h>
1923681e47SGreg Kroah-Hartman #include <linux/kdev_t.h>
20116af378SBenjamin Herrenschmidt #include <linux/notifier.h>
2107d57a32SGrant Likely #include <linux/of.h>
2207d57a32SGrant Likely #include <linux/of_device.h>
23da231fd5SKay Sievers #include <linux/genhd.h>
24f75b1c60SDave Young #include <linux/mutex.h>
25af8db150SPeter Chen #include <linux/pm_runtime.h>
26c4e00daaSKay Sievers #include <linux/netdevice.h>
27174cd4b1SIngo Molnar #include <linux/sched/signal.h>
2863967685SGreg Kroah-Hartman #include <linux/sysfs.h>
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds #include "base.h"
311da177e4SLinus Torvalds #include "power/power.h"
321da177e4SLinus Torvalds 
33e52eec13SAndi Kleen #ifdef CONFIG_SYSFS_DEPRECATED
34e52eec13SAndi Kleen #ifdef CONFIG_SYSFS_DEPRECATED_V2
35e52eec13SAndi Kleen long sysfs_deprecated = 1;
36e52eec13SAndi Kleen #else
37e52eec13SAndi Kleen long sysfs_deprecated = 0;
38e52eec13SAndi Kleen #endif
393454bf96SHanjun Guo static int __init sysfs_deprecated_setup(char *arg)
40e52eec13SAndi Kleen {
4134da5e67SJingoo Han 	return kstrtol(arg, 10, &sysfs_deprecated);
42e52eec13SAndi Kleen }
43e52eec13SAndi Kleen early_param("sysfs.deprecated", sysfs_deprecated_setup);
44e52eec13SAndi Kleen #endif
45e52eec13SAndi Kleen 
469ed98953SRafael J. Wysocki /* Device links support. */
479ed98953SRafael J. Wysocki 
489ed98953SRafael J. Wysocki #ifdef CONFIG_SRCU
499ed98953SRafael J. Wysocki static DEFINE_MUTEX(device_links_lock);
509ed98953SRafael J. Wysocki DEFINE_STATIC_SRCU(device_links_srcu);
519ed98953SRafael J. Wysocki 
529ed98953SRafael J. Wysocki static inline void device_links_write_lock(void)
539ed98953SRafael J. Wysocki {
549ed98953SRafael J. Wysocki 	mutex_lock(&device_links_lock);
559ed98953SRafael J. Wysocki }
569ed98953SRafael J. Wysocki 
579ed98953SRafael J. Wysocki static inline void device_links_write_unlock(void)
589ed98953SRafael J. Wysocki {
599ed98953SRafael J. Wysocki 	mutex_unlock(&device_links_lock);
609ed98953SRafael J. Wysocki }
619ed98953SRafael J. Wysocki 
629ed98953SRafael J. Wysocki int device_links_read_lock(void)
639ed98953SRafael J. Wysocki {
649ed98953SRafael J. Wysocki 	return srcu_read_lock(&device_links_srcu);
659ed98953SRafael J. Wysocki }
669ed98953SRafael J. Wysocki 
679ed98953SRafael J. Wysocki void device_links_read_unlock(int idx)
689ed98953SRafael J. Wysocki {
699ed98953SRafael J. Wysocki 	srcu_read_unlock(&device_links_srcu, idx);
709ed98953SRafael J. Wysocki }
719ed98953SRafael J. Wysocki #else /* !CONFIG_SRCU */
729ed98953SRafael J. Wysocki static DECLARE_RWSEM(device_links_lock);
739ed98953SRafael J. Wysocki 
749ed98953SRafael J. Wysocki static inline void device_links_write_lock(void)
759ed98953SRafael J. Wysocki {
769ed98953SRafael J. Wysocki 	down_write(&device_links_lock);
779ed98953SRafael J. Wysocki }
789ed98953SRafael J. Wysocki 
799ed98953SRafael J. Wysocki static inline void device_links_write_unlock(void)
809ed98953SRafael J. Wysocki {
819ed98953SRafael J. Wysocki 	up_write(&device_links_lock);
829ed98953SRafael J. Wysocki }
839ed98953SRafael J. Wysocki 
849ed98953SRafael J. Wysocki int device_links_read_lock(void)
859ed98953SRafael J. Wysocki {
869ed98953SRafael J. Wysocki 	down_read(&device_links_lock);
879ed98953SRafael J. Wysocki 	return 0;
889ed98953SRafael J. Wysocki }
899ed98953SRafael J. Wysocki 
909ed98953SRafael J. Wysocki void device_links_read_unlock(int not_used)
919ed98953SRafael J. Wysocki {
929ed98953SRafael J. Wysocki 	up_read(&device_links_lock);
939ed98953SRafael J. Wysocki }
949ed98953SRafael J. Wysocki #endif /* !CONFIG_SRCU */
959ed98953SRafael J. Wysocki 
969ed98953SRafael J. Wysocki /**
979ed98953SRafael J. Wysocki  * device_is_dependent - Check if one device depends on another one
989ed98953SRafael J. Wysocki  * @dev: Device to check dependencies for.
999ed98953SRafael J. Wysocki  * @target: Device to check against.
1009ed98953SRafael J. Wysocki  *
1019ed98953SRafael J. Wysocki  * Check if @target depends on @dev or any device dependent on it (its child or
1029ed98953SRafael J. Wysocki  * its consumer etc).  Return 1 if that is the case or 0 otherwise.
1039ed98953SRafael J. Wysocki  */
1049ed98953SRafael J. Wysocki static int device_is_dependent(struct device *dev, void *target)
1059ed98953SRafael J. Wysocki {
1069ed98953SRafael J. Wysocki 	struct device_link *link;
1079ed98953SRafael J. Wysocki 	int ret;
1089ed98953SRafael J. Wysocki 
109e16f4f3eSBenjamin Gaignard 	if (dev == target)
1109ed98953SRafael J. Wysocki 		return 1;
1119ed98953SRafael J. Wysocki 
1129ed98953SRafael J. Wysocki 	ret = device_for_each_child(dev, target, device_is_dependent);
1139ed98953SRafael J. Wysocki 	if (ret)
1149ed98953SRafael J. Wysocki 		return ret;
1159ed98953SRafael J. Wysocki 
1169ed98953SRafael J. Wysocki 	list_for_each_entry(link, &dev->links.consumers, s_node) {
117e16f4f3eSBenjamin Gaignard 		if (link->consumer == target)
1189ed98953SRafael J. Wysocki 			return 1;
1199ed98953SRafael J. Wysocki 
1209ed98953SRafael J. Wysocki 		ret = device_is_dependent(link->consumer, target);
1219ed98953SRafael J. Wysocki 		if (ret)
1229ed98953SRafael J. Wysocki 			break;
1239ed98953SRafael J. Wysocki 	}
1249ed98953SRafael J. Wysocki 	return ret;
1259ed98953SRafael J. Wysocki }
1269ed98953SRafael J. Wysocki 
1279ed98953SRafael J. Wysocki static int device_reorder_to_tail(struct device *dev, void *not_used)
1289ed98953SRafael J. Wysocki {
1299ed98953SRafael J. Wysocki 	struct device_link *link;
1309ed98953SRafael J. Wysocki 
1319ed98953SRafael J. Wysocki 	/*
1329ed98953SRafael J. Wysocki 	 * Devices that have not been registered yet will be put to the ends
1339ed98953SRafael J. Wysocki 	 * of the lists during the registration, so skip them here.
1349ed98953SRafael J. Wysocki 	 */
1359ed98953SRafael J. Wysocki 	if (device_is_registered(dev))
1369ed98953SRafael J. Wysocki 		devices_kset_move_last(dev);
1379ed98953SRafael J. Wysocki 
1389ed98953SRafael J. Wysocki 	if (device_pm_initialized(dev))
1399ed98953SRafael J. Wysocki 		device_pm_move_last(dev);
1409ed98953SRafael J. Wysocki 
1419ed98953SRafael J. Wysocki 	device_for_each_child(dev, NULL, device_reorder_to_tail);
1429ed98953SRafael J. Wysocki 	list_for_each_entry(link, &dev->links.consumers, s_node)
1439ed98953SRafael J. Wysocki 		device_reorder_to_tail(link->consumer, NULL);
1449ed98953SRafael J. Wysocki 
1459ed98953SRafael J. Wysocki 	return 0;
1469ed98953SRafael J. Wysocki }
1479ed98953SRafael J. Wysocki 
1489ed98953SRafael J. Wysocki /**
149494fd7b7SFeng Kan  * device_pm_move_to_tail - Move set of devices to the end of device lists
150494fd7b7SFeng Kan  * @dev: Device to move
151494fd7b7SFeng Kan  *
152494fd7b7SFeng Kan  * This is a device_reorder_to_tail() wrapper taking the requisite locks.
153494fd7b7SFeng Kan  *
154494fd7b7SFeng Kan  * It moves the @dev along with all of its children and all of its consumers
155494fd7b7SFeng Kan  * to the ends of the device_kset and dpm_list, recursively.
156494fd7b7SFeng Kan  */
157494fd7b7SFeng Kan void device_pm_move_to_tail(struct device *dev)
158494fd7b7SFeng Kan {
159494fd7b7SFeng Kan 	int idx;
160494fd7b7SFeng Kan 
161494fd7b7SFeng Kan 	idx = device_links_read_lock();
162494fd7b7SFeng Kan 	device_pm_lock();
163494fd7b7SFeng Kan 	device_reorder_to_tail(dev, NULL);
164494fd7b7SFeng Kan 	device_pm_unlock();
165494fd7b7SFeng Kan 	device_links_read_unlock(idx);
166494fd7b7SFeng Kan }
167494fd7b7SFeng Kan 
168494fd7b7SFeng Kan /**
1699ed98953SRafael J. Wysocki  * device_link_add - Create a link between two devices.
1709ed98953SRafael J. Wysocki  * @consumer: Consumer end of the link.
1719ed98953SRafael J. Wysocki  * @supplier: Supplier end of the link.
1729ed98953SRafael J. Wysocki  * @flags: Link flags.
1739ed98953SRafael J. Wysocki  *
17421d5c57bSRafael J. Wysocki  * The caller is responsible for the proper synchronization of the link creation
17521d5c57bSRafael J. Wysocki  * with runtime PM.  First, setting the DL_FLAG_PM_RUNTIME flag will cause the
17621d5c57bSRafael J. Wysocki  * runtime PM framework to take the link into account.  Second, if the
17721d5c57bSRafael J. Wysocki  * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
17821d5c57bSRafael J. Wysocki  * be forced into the active metastate and reference-counted upon the creation
17921d5c57bSRafael J. Wysocki  * of the link.  If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
18021d5c57bSRafael J. Wysocki  * ignored.
18121d5c57bSRafael J. Wysocki  *
18272175d4eSRafael J. Wysocki  * If DL_FLAG_STATELESS is set in @flags, the link is not going to be managed by
18372175d4eSRafael J. Wysocki  * the driver core and, in particular, the caller of this function is expected
18472175d4eSRafael J. Wysocki  * to drop the reference to the link acquired by it directly.
18572175d4eSRafael J. Wysocki  *
18672175d4eSRafael J. Wysocki  * If that flag is not set, however, the caller of this function is handing the
18772175d4eSRafael J. Wysocki  * management of the link over to the driver core entirely and its return value
18872175d4eSRafael J. Wysocki  * can only be used to check whether or not the link is present.  In that case,
18972175d4eSRafael J. Wysocki  * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
19072175d4eSRafael J. Wysocki  * flags can be used to indicate to the driver core when the link can be safely
19172175d4eSRafael J. Wysocki  * deleted.  Namely, setting one of them in @flags indicates to the driver core
19272175d4eSRafael J. Wysocki  * that the link is not going to be used (by the given caller of this function)
19372175d4eSRafael J. Wysocki  * after unbinding the consumer or supplier driver, respectively, from its
19472175d4eSRafael J. Wysocki  * device, so the link can be deleted at that point.  If none of them is set,
19572175d4eSRafael J. Wysocki  * the link will be maintained until one of the devices pointed to by it (either
19672175d4eSRafael J. Wysocki  * the consumer or the supplier) is unregistered.
197c8d50986SRafael J. Wysocki  *
198e7dd4010SRafael J. Wysocki  * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
199e7dd4010SRafael J. Wysocki  * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
200e7dd4010SRafael J. Wysocki  * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
201e7dd4010SRafael J. Wysocki  * be used to request the driver core to automaticall probe for a consmer
202e7dd4010SRafael J. Wysocki  * driver after successfully binding a driver to the supplier device.
203e7dd4010SRafael J. Wysocki  *
204c8d50986SRafael J. Wysocki  * The combination of DL_FLAG_STATELESS and either DL_FLAG_AUTOREMOVE_CONSUMER
205c8d50986SRafael J. Wysocki  * or DL_FLAG_AUTOREMOVE_SUPPLIER set in @flags at the same time is invalid and
206c8d50986SRafael J. Wysocki  * will cause NULL to be returned upfront.
2079ed98953SRafael J. Wysocki  *
2089ed98953SRafael J. Wysocki  * A side effect of the link creation is re-ordering of dpm_list and the
2099ed98953SRafael J. Wysocki  * devices_kset list by moving the consumer device and all devices depending
2109ed98953SRafael J. Wysocki  * on it to the ends of these lists (that does not happen to devices that have
2119ed98953SRafael J. Wysocki  * not been registered when this function is called).
2129ed98953SRafael J. Wysocki  *
2139ed98953SRafael J. Wysocki  * The supplier device is required to be registered when this function is called
2149ed98953SRafael J. Wysocki  * and NULL will be returned if that is not the case.  The consumer device need
21564df1148SLukas Wunner  * not be registered, however.
2169ed98953SRafael J. Wysocki  */
2179ed98953SRafael J. Wysocki struct device_link *device_link_add(struct device *consumer,
2189ed98953SRafael J. Wysocki 				    struct device *supplier, u32 flags)
2199ed98953SRafael J. Wysocki {
2209ed98953SRafael J. Wysocki 	struct device_link *link;
2219ed98953SRafael J. Wysocki 
2229ed98953SRafael J. Wysocki 	if (!consumer || !supplier ||
223c8d50986SRafael J. Wysocki 	    (flags & DL_FLAG_STATELESS &&
224e7dd4010SRafael J. Wysocki 	     flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
225e7dd4010SRafael J. Wysocki 		      DL_FLAG_AUTOREMOVE_SUPPLIER |
226e7dd4010SRafael J. Wysocki 		      DL_FLAG_AUTOPROBE_CONSUMER)) ||
227e7dd4010SRafael J. Wysocki 	    (flags & DL_FLAG_AUTOPROBE_CONSUMER &&
228e7dd4010SRafael J. Wysocki 	     flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
229e7dd4010SRafael J. Wysocki 		      DL_FLAG_AUTOREMOVE_SUPPLIER)))
2309ed98953SRafael J. Wysocki 		return NULL;
2319ed98953SRafael J. Wysocki 
2325db25c9eSRafael J. Wysocki 	if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
2335db25c9eSRafael J. Wysocki 		if (pm_runtime_get_sync(supplier) < 0) {
2345db25c9eSRafael J. Wysocki 			pm_runtime_put_noidle(supplier);
2355db25c9eSRafael J. Wysocki 			return NULL;
2365db25c9eSRafael J. Wysocki 		}
2375db25c9eSRafael J. Wysocki 	}
2385db25c9eSRafael J. Wysocki 
2399ed98953SRafael J. Wysocki 	device_links_write_lock();
2409ed98953SRafael J. Wysocki 	device_pm_lock();
2419ed98953SRafael J. Wysocki 
2429ed98953SRafael J. Wysocki 	/*
2439ed98953SRafael J. Wysocki 	 * If the supplier has not been fully registered yet or there is a
2449ed98953SRafael J. Wysocki 	 * reverse dependency between the consumer and the supplier already in
2459ed98953SRafael J. Wysocki 	 * the graph, return NULL.
2469ed98953SRafael J. Wysocki 	 */
2479ed98953SRafael J. Wysocki 	if (!device_pm_initialized(supplier)
2489ed98953SRafael J. Wysocki 	    || device_is_dependent(consumer, supplier)) {
2499ed98953SRafael J. Wysocki 		link = NULL;
2509ed98953SRafael J. Wysocki 		goto out;
2519ed98953SRafael J. Wysocki 	}
2529ed98953SRafael J. Wysocki 
25372175d4eSRafael J. Wysocki 	/*
25472175d4eSRafael J. Wysocki 	 * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
25572175d4eSRafael J. Wysocki 	 * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
25672175d4eSRafael J. Wysocki 	 * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
25772175d4eSRafael J. Wysocki 	 */
25872175d4eSRafael J. Wysocki 	if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
25972175d4eSRafael J. Wysocki 		flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
26072175d4eSRafael J. Wysocki 
261f265df55SRafael J. Wysocki 	list_for_each_entry(link, &supplier->links.consumers, s_node) {
262f265df55SRafael J. Wysocki 		if (link->consumer != consumer)
263f265df55SRafael J. Wysocki 			continue;
264f265df55SRafael J. Wysocki 
265f265df55SRafael J. Wysocki 		/*
266f265df55SRafael J. Wysocki 		 * Don't return a stateless link if the caller wants a stateful
267f265df55SRafael J. Wysocki 		 * one and vice versa.
268f265df55SRafael J. Wysocki 		 */
269f265df55SRafael J. Wysocki 		if (WARN_ON((flags & DL_FLAG_STATELESS) != (link->flags & DL_FLAG_STATELESS))) {
270f265df55SRafael J. Wysocki 			link = NULL;
271f265df55SRafael J. Wysocki 			goto out;
272f265df55SRafael J. Wysocki 		}
273f265df55SRafael J. Wysocki 
274e2f3cd83SRafael J. Wysocki 		if (flags & DL_FLAG_PM_RUNTIME) {
275e2f3cd83SRafael J. Wysocki 			if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
2764c06c4e6SRafael J. Wysocki 				pm_runtime_new_link(consumer);
277e2f3cd83SRafael J. Wysocki 				link->flags |= DL_FLAG_PM_RUNTIME;
278e2f3cd83SRafael J. Wysocki 			}
279e2f3cd83SRafael J. Wysocki 			if (flags & DL_FLAG_RPM_ACTIVE)
28036003d4cSRafael J. Wysocki 				refcount_inc(&link->rpm_active);
281e2f3cd83SRafael J. Wysocki 		}
282e2f3cd83SRafael J. Wysocki 
28372175d4eSRafael J. Wysocki 		if (flags & DL_FLAG_STATELESS) {
284ead18c23SLukas Wunner 			kref_get(&link->kref);
2859ed98953SRafael J. Wysocki 			goto out;
286ead18c23SLukas Wunner 		}
2879ed98953SRafael J. Wysocki 
28872175d4eSRafael J. Wysocki 		/*
28972175d4eSRafael J. Wysocki 		 * If the life time of the link following from the new flags is
29072175d4eSRafael J. Wysocki 		 * longer than indicated by the flags of the existing link,
29172175d4eSRafael J. Wysocki 		 * update the existing link to stay around longer.
29272175d4eSRafael J. Wysocki 		 */
29372175d4eSRafael J. Wysocki 		if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
29472175d4eSRafael J. Wysocki 			if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
29572175d4eSRafael J. Wysocki 				link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
29672175d4eSRafael J. Wysocki 				link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
29772175d4eSRafael J. Wysocki 			}
29872175d4eSRafael J. Wysocki 		} else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
29972175d4eSRafael J. Wysocki 			link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
30072175d4eSRafael J. Wysocki 					 DL_FLAG_AUTOREMOVE_SUPPLIER);
30172175d4eSRafael J. Wysocki 		}
30272175d4eSRafael J. Wysocki 		goto out;
30372175d4eSRafael J. Wysocki 	}
30472175d4eSRafael J. Wysocki 
30521d5c57bSRafael J. Wysocki 	link = kzalloc(sizeof(*link), GFP_KERNEL);
3069ed98953SRafael J. Wysocki 	if (!link)
3079ed98953SRafael J. Wysocki 		goto out;
3089ed98953SRafael J. Wysocki 
309e2f3cd83SRafael J. Wysocki 	refcount_set(&link->rpm_active, 1);
310e2f3cd83SRafael J. Wysocki 
311baa8809fSRafael J. Wysocki 	if (flags & DL_FLAG_PM_RUNTIME) {
312e2f3cd83SRafael J. Wysocki 		if (flags & DL_FLAG_RPM_ACTIVE)
31336003d4cSRafael J. Wysocki 			refcount_inc(&link->rpm_active);
314e2f3cd83SRafael J. Wysocki 
3154c06c4e6SRafael J. Wysocki 		pm_runtime_new_link(consumer);
31621d5c57bSRafael J. Wysocki 	}
317e2f3cd83SRafael J. Wysocki 
3189ed98953SRafael J. Wysocki 	get_device(supplier);
3199ed98953SRafael J. Wysocki 	link->supplier = supplier;
3209ed98953SRafael J. Wysocki 	INIT_LIST_HEAD(&link->s_node);
3219ed98953SRafael J. Wysocki 	get_device(consumer);
3229ed98953SRafael J. Wysocki 	link->consumer = consumer;
3239ed98953SRafael J. Wysocki 	INIT_LIST_HEAD(&link->c_node);
3249ed98953SRafael J. Wysocki 	link->flags = flags;
325ead18c23SLukas Wunner 	kref_init(&link->kref);
3269ed98953SRafael J. Wysocki 
32764df1148SLukas Wunner 	/* Determine the initial link state. */
3289ed98953SRafael J. Wysocki 	if (flags & DL_FLAG_STATELESS) {
3299ed98953SRafael J. Wysocki 		link->status = DL_STATE_NONE;
3309ed98953SRafael J. Wysocki 	} else {
3319ed98953SRafael J. Wysocki 		switch (supplier->links.status) {
33215cfb094SRafael J. Wysocki 		case DL_DEV_PROBING:
3339ed98953SRafael J. Wysocki 			switch (consumer->links.status) {
3349ed98953SRafael J. Wysocki 			case DL_DEV_PROBING:
33521d5c57bSRafael J. Wysocki 				/*
33615cfb094SRafael J. Wysocki 				 * A consumer driver can create a link to a
33715cfb094SRafael J. Wysocki 				 * supplier that has not completed its probing
33815cfb094SRafael J. Wysocki 				 * yet as long as it knows that the supplier is
33915cfb094SRafael J. Wysocki 				 * already functional (for example, it has just
34015cfb094SRafael J. Wysocki 				 * acquired some resources from the supplier).
34121d5c57bSRafael J. Wysocki 				 */
34215cfb094SRafael J. Wysocki 				link->status = DL_STATE_CONSUMER_PROBE;
34315cfb094SRafael J. Wysocki 				break;
34415cfb094SRafael J. Wysocki 			default:
34515cfb094SRafael J. Wysocki 				link->status = DL_STATE_DORMANT;
34615cfb094SRafael J. Wysocki 				break;
34715cfb094SRafael J. Wysocki 			}
34815cfb094SRafael J. Wysocki 			break;
34915cfb094SRafael J. Wysocki 		case DL_DEV_DRIVER_BOUND:
35015cfb094SRafael J. Wysocki 			switch (consumer->links.status) {
35115cfb094SRafael J. Wysocki 			case DL_DEV_PROBING:
3529ed98953SRafael J. Wysocki 				link->status = DL_STATE_CONSUMER_PROBE;
3539ed98953SRafael J. Wysocki 				break;
3549ed98953SRafael J. Wysocki 			case DL_DEV_DRIVER_BOUND:
3559ed98953SRafael J. Wysocki 				link->status = DL_STATE_ACTIVE;
3569ed98953SRafael J. Wysocki 				break;
3579ed98953SRafael J. Wysocki 			default:
3589ed98953SRafael J. Wysocki 				link->status = DL_STATE_AVAILABLE;
3599ed98953SRafael J. Wysocki 				break;
3609ed98953SRafael J. Wysocki 			}
3619ed98953SRafael J. Wysocki 			break;
3629ed98953SRafael J. Wysocki 		case DL_DEV_UNBINDING:
3639ed98953SRafael J. Wysocki 			link->status = DL_STATE_SUPPLIER_UNBIND;
3649ed98953SRafael J. Wysocki 			break;
3659ed98953SRafael J. Wysocki 		default:
3669ed98953SRafael J. Wysocki 			link->status = DL_STATE_DORMANT;
3679ed98953SRafael J. Wysocki 			break;
3689ed98953SRafael J. Wysocki 		}
3699ed98953SRafael J. Wysocki 	}
3709ed98953SRafael J. Wysocki 
3719ed98953SRafael J. Wysocki 	/*
37215cfb094SRafael J. Wysocki 	 * Some callers expect the link creation during consumer driver probe to
37315cfb094SRafael J. Wysocki 	 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
37415cfb094SRafael J. Wysocki 	 */
37515cfb094SRafael J. Wysocki 	if (link->status == DL_STATE_CONSUMER_PROBE &&
37615cfb094SRafael J. Wysocki 	    flags & DL_FLAG_PM_RUNTIME)
37715cfb094SRafael J. Wysocki 		pm_runtime_resume(supplier);
37815cfb094SRafael J. Wysocki 
37915cfb094SRafael J. Wysocki 	/*
3809ed98953SRafael J. Wysocki 	 * Move the consumer and all of the devices depending on it to the end
3819ed98953SRafael J. Wysocki 	 * of dpm_list and the devices_kset list.
3829ed98953SRafael J. Wysocki 	 *
3839ed98953SRafael J. Wysocki 	 * It is necessary to hold dpm_list locked throughout all that or else
3849ed98953SRafael J. Wysocki 	 * we may end up suspending with a wrong ordering of it.
3859ed98953SRafael J. Wysocki 	 */
3869ed98953SRafael J. Wysocki 	device_reorder_to_tail(consumer, NULL);
3879ed98953SRafael J. Wysocki 
3889ed98953SRafael J. Wysocki 	list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
3899ed98953SRafael J. Wysocki 	list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
3909ed98953SRafael J. Wysocki 
3918a4b3269SJerome Brunet 	dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
3929ed98953SRafael J. Wysocki 
3939ed98953SRafael J. Wysocki  out:
3949ed98953SRafael J. Wysocki 	device_pm_unlock();
3959ed98953SRafael J. Wysocki 	device_links_write_unlock();
3965db25c9eSRafael J. Wysocki 
397e2f3cd83SRafael J. Wysocki 	if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
3985db25c9eSRafael J. Wysocki 		pm_runtime_put(supplier);
3995db25c9eSRafael J. Wysocki 
4009ed98953SRafael J. Wysocki 	return link;
4019ed98953SRafael J. Wysocki }
4029ed98953SRafael J. Wysocki EXPORT_SYMBOL_GPL(device_link_add);
4039ed98953SRafael J. Wysocki 
4049ed98953SRafael J. Wysocki static void device_link_free(struct device_link *link)
4059ed98953SRafael J. Wysocki {
406a1fdbfbbSRafael J. Wysocki 	while (refcount_dec_not_one(&link->rpm_active))
407a1fdbfbbSRafael J. Wysocki 		pm_runtime_put(link->supplier);
408a1fdbfbbSRafael J. Wysocki 
4099ed98953SRafael J. Wysocki 	put_device(link->consumer);
4109ed98953SRafael J. Wysocki 	put_device(link->supplier);
4119ed98953SRafael J. Wysocki 	kfree(link);
4129ed98953SRafael J. Wysocki }
4139ed98953SRafael J. Wysocki 
4149ed98953SRafael J. Wysocki #ifdef CONFIG_SRCU
4159ed98953SRafael J. Wysocki static void __device_link_free_srcu(struct rcu_head *rhead)
4169ed98953SRafael J. Wysocki {
4179ed98953SRafael J. Wysocki 	device_link_free(container_of(rhead, struct device_link, rcu_head));
4189ed98953SRafael J. Wysocki }
4199ed98953SRafael J. Wysocki 
420ead18c23SLukas Wunner static void __device_link_del(struct kref *kref)
4219ed98953SRafael J. Wysocki {
422ead18c23SLukas Wunner 	struct device_link *link = container_of(kref, struct device_link, kref);
423ead18c23SLukas Wunner 
4248a4b3269SJerome Brunet 	dev_dbg(link->consumer, "Dropping the link to %s\n",
4259ed98953SRafael J. Wysocki 		dev_name(link->supplier));
4269ed98953SRafael J. Wysocki 
427baa8809fSRafael J. Wysocki 	if (link->flags & DL_FLAG_PM_RUNTIME)
428baa8809fSRafael J. Wysocki 		pm_runtime_drop_link(link->consumer);
429baa8809fSRafael J. Wysocki 
4309ed98953SRafael J. Wysocki 	list_del_rcu(&link->s_node);
4319ed98953SRafael J. Wysocki 	list_del_rcu(&link->c_node);
4329ed98953SRafael J. Wysocki 	call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
4339ed98953SRafael J. Wysocki }
4349ed98953SRafael J. Wysocki #else /* !CONFIG_SRCU */
435ead18c23SLukas Wunner static void __device_link_del(struct kref *kref)
4369ed98953SRafael J. Wysocki {
437ead18c23SLukas Wunner 	struct device_link *link = container_of(kref, struct device_link, kref);
438ead18c23SLukas Wunner 
4399ed98953SRafael J. Wysocki 	dev_info(link->consumer, "Dropping the link to %s\n",
4409ed98953SRafael J. Wysocki 		 dev_name(link->supplier));
4419ed98953SRafael J. Wysocki 
442433986c2SLukas Wunner 	if (link->flags & DL_FLAG_PM_RUNTIME)
443433986c2SLukas Wunner 		pm_runtime_drop_link(link->consumer);
444433986c2SLukas Wunner 
4459ed98953SRafael J. Wysocki 	list_del(&link->s_node);
4469ed98953SRafael J. Wysocki 	list_del(&link->c_node);
4479ed98953SRafael J. Wysocki 	device_link_free(link);
4489ed98953SRafael J. Wysocki }
4499ed98953SRafael J. Wysocki #endif /* !CONFIG_SRCU */
4509ed98953SRafael J. Wysocki 
45172175d4eSRafael J. Wysocki static void device_link_put_kref(struct device_link *link)
45272175d4eSRafael J. Wysocki {
45372175d4eSRafael J. Wysocki 	if (link->flags & DL_FLAG_STATELESS)
45472175d4eSRafael J. Wysocki 		kref_put(&link->kref, __device_link_del);
45572175d4eSRafael J. Wysocki 	else
45672175d4eSRafael J. Wysocki 		WARN(1, "Unable to drop a managed device link reference\n");
45772175d4eSRafael J. Wysocki }
45872175d4eSRafael J. Wysocki 
4599ed98953SRafael J. Wysocki /**
46072175d4eSRafael J. Wysocki  * device_link_del - Delete a stateless link between two devices.
4619ed98953SRafael J. Wysocki  * @link: Device link to delete.
4629ed98953SRafael J. Wysocki  *
4639ed98953SRafael J. Wysocki  * The caller must ensure proper synchronization of this function with runtime
464ead18c23SLukas Wunner  * PM.  If the link was added multiple times, it needs to be deleted as often.
465ead18c23SLukas Wunner  * Care is required for hotplugged devices:  Their links are purged on removal
466ead18c23SLukas Wunner  * and calling device_link_del() is then no longer allowed.
4679ed98953SRafael J. Wysocki  */
4689ed98953SRafael J. Wysocki void device_link_del(struct device_link *link)
4699ed98953SRafael J. Wysocki {
4709ed98953SRafael J. Wysocki 	device_links_write_lock();
4719ed98953SRafael J. Wysocki 	device_pm_lock();
47272175d4eSRafael J. Wysocki 	device_link_put_kref(link);
4739ed98953SRafael J. Wysocki 	device_pm_unlock();
4749ed98953SRafael J. Wysocki 	device_links_write_unlock();
4759ed98953SRafael J. Wysocki }
4769ed98953SRafael J. Wysocki EXPORT_SYMBOL_GPL(device_link_del);
4779ed98953SRafael J. Wysocki 
478d8842211Spascal paillet /**
47972175d4eSRafael J. Wysocki  * device_link_remove - Delete a stateless link between two devices.
480d8842211Spascal paillet  * @consumer: Consumer end of the link.
481d8842211Spascal paillet  * @supplier: Supplier end of the link.
482d8842211Spascal paillet  *
483d8842211Spascal paillet  * The caller must ensure proper synchronization of this function with runtime
484d8842211Spascal paillet  * PM.
485d8842211Spascal paillet  */
486d8842211Spascal paillet void device_link_remove(void *consumer, struct device *supplier)
487d8842211Spascal paillet {
488d8842211Spascal paillet 	struct device_link *link;
489d8842211Spascal paillet 
490d8842211Spascal paillet 	if (WARN_ON(consumer == supplier))
491d8842211Spascal paillet 		return;
492d8842211Spascal paillet 
493d8842211Spascal paillet 	device_links_write_lock();
494d8842211Spascal paillet 	device_pm_lock();
495d8842211Spascal paillet 
496d8842211Spascal paillet 	list_for_each_entry(link, &supplier->links.consumers, s_node) {
497d8842211Spascal paillet 		if (link->consumer == consumer) {
49872175d4eSRafael J. Wysocki 			device_link_put_kref(link);
499d8842211Spascal paillet 			break;
500d8842211Spascal paillet 		}
501d8842211Spascal paillet 	}
502d8842211Spascal paillet 
503d8842211Spascal paillet 	device_pm_unlock();
504d8842211Spascal paillet 	device_links_write_unlock();
505d8842211Spascal paillet }
506d8842211Spascal paillet EXPORT_SYMBOL_GPL(device_link_remove);
507d8842211Spascal paillet 
5089ed98953SRafael J. Wysocki static void device_links_missing_supplier(struct device *dev)
5099ed98953SRafael J. Wysocki {
5109ed98953SRafael J. Wysocki 	struct device_link *link;
5119ed98953SRafael J. Wysocki 
5129ed98953SRafael J. Wysocki 	list_for_each_entry(link, &dev->links.suppliers, c_node)
5139ed98953SRafael J. Wysocki 		if (link->status == DL_STATE_CONSUMER_PROBE)
5149ed98953SRafael J. Wysocki 			WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
5159ed98953SRafael J. Wysocki }
5169ed98953SRafael J. Wysocki 
5179ed98953SRafael J. Wysocki /**
5189ed98953SRafael J. Wysocki  * device_links_check_suppliers - Check presence of supplier drivers.
5199ed98953SRafael J. Wysocki  * @dev: Consumer device.
5209ed98953SRafael J. Wysocki  *
5219ed98953SRafael J. Wysocki  * Check links from this device to any suppliers.  Walk the list of the device's
5229ed98953SRafael J. Wysocki  * links to suppliers and see if all of them are available.  If not, simply
5239ed98953SRafael J. Wysocki  * return -EPROBE_DEFER.
5249ed98953SRafael J. Wysocki  *
5259ed98953SRafael J. Wysocki  * We need to guarantee that the supplier will not go away after the check has
5269ed98953SRafael J. Wysocki  * been positive here.  It only can go away in __device_release_driver() and
5279ed98953SRafael J. Wysocki  * that function  checks the device's links to consumers.  This means we need to
5289ed98953SRafael J. Wysocki  * mark the link as "consumer probe in progress" to make the supplier removal
5299ed98953SRafael J. Wysocki  * wait for us to complete (or bad things may happen).
5309ed98953SRafael J. Wysocki  *
5319ed98953SRafael J. Wysocki  * Links with the DL_FLAG_STATELESS flag set are ignored.
5329ed98953SRafael J. Wysocki  */
5339ed98953SRafael J. Wysocki int device_links_check_suppliers(struct device *dev)
5349ed98953SRafael J. Wysocki {
5359ed98953SRafael J. Wysocki 	struct device_link *link;
5369ed98953SRafael J. Wysocki 	int ret = 0;
5379ed98953SRafael J. Wysocki 
5389ed98953SRafael J. Wysocki 	device_links_write_lock();
5399ed98953SRafael J. Wysocki 
5409ed98953SRafael J. Wysocki 	list_for_each_entry(link, &dev->links.suppliers, c_node) {
5419ed98953SRafael J. Wysocki 		if (link->flags & DL_FLAG_STATELESS)
5429ed98953SRafael J. Wysocki 			continue;
5439ed98953SRafael J. Wysocki 
5449ed98953SRafael J. Wysocki 		if (link->status != DL_STATE_AVAILABLE) {
5459ed98953SRafael J. Wysocki 			device_links_missing_supplier(dev);
5469ed98953SRafael J. Wysocki 			ret = -EPROBE_DEFER;
5479ed98953SRafael J. Wysocki 			break;
5489ed98953SRafael J. Wysocki 		}
5499ed98953SRafael J. Wysocki 		WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
5509ed98953SRafael J. Wysocki 	}
5519ed98953SRafael J. Wysocki 	dev->links.status = DL_DEV_PROBING;
5529ed98953SRafael J. Wysocki 
5539ed98953SRafael J. Wysocki 	device_links_write_unlock();
5549ed98953SRafael J. Wysocki 	return ret;
5559ed98953SRafael J. Wysocki }
5569ed98953SRafael J. Wysocki 
5579ed98953SRafael J. Wysocki /**
5589ed98953SRafael J. Wysocki  * device_links_driver_bound - Update device links after probing its driver.
5599ed98953SRafael J. Wysocki  * @dev: Device to update the links for.
5609ed98953SRafael J. Wysocki  *
5619ed98953SRafael J. Wysocki  * The probe has been successful, so update links from this device to any
5629ed98953SRafael J. Wysocki  * consumers by changing their status to "available".
5639ed98953SRafael J. Wysocki  *
5649ed98953SRafael J. Wysocki  * Also change the status of @dev's links to suppliers to "active".
5659ed98953SRafael J. Wysocki  *
5669ed98953SRafael J. Wysocki  * Links with the DL_FLAG_STATELESS flag set are ignored.
5679ed98953SRafael J. Wysocki  */
5689ed98953SRafael J. Wysocki void device_links_driver_bound(struct device *dev)
5699ed98953SRafael J. Wysocki {
5709ed98953SRafael J. Wysocki 	struct device_link *link;
5719ed98953SRafael J. Wysocki 
5729ed98953SRafael J. Wysocki 	device_links_write_lock();
5739ed98953SRafael J. Wysocki 
5749ed98953SRafael J. Wysocki 	list_for_each_entry(link, &dev->links.consumers, s_node) {
5759ed98953SRafael J. Wysocki 		if (link->flags & DL_FLAG_STATELESS)
5769ed98953SRafael J. Wysocki 			continue;
5779ed98953SRafael J. Wysocki 
57815cfb094SRafael J. Wysocki 		/*
57915cfb094SRafael J. Wysocki 		 * Links created during consumer probe may be in the "consumer
58015cfb094SRafael J. Wysocki 		 * probe" state to start with if the supplier is still probing
58115cfb094SRafael J. Wysocki 		 * when they are created and they may become "active" if the
58215cfb094SRafael J. Wysocki 		 * consumer probe returns first.  Skip them here.
58315cfb094SRafael J. Wysocki 		 */
58415cfb094SRafael J. Wysocki 		if (link->status == DL_STATE_CONSUMER_PROBE ||
58515cfb094SRafael J. Wysocki 		    link->status == DL_STATE_ACTIVE)
58615cfb094SRafael J. Wysocki 			continue;
58715cfb094SRafael J. Wysocki 
5889ed98953SRafael J. Wysocki 		WARN_ON(link->status != DL_STATE_DORMANT);
5899ed98953SRafael J. Wysocki 		WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
590e7dd4010SRafael J. Wysocki 
591e7dd4010SRafael J. Wysocki 		if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER)
592e7dd4010SRafael J. Wysocki 			driver_deferred_probe_add(link->consumer);
5939ed98953SRafael J. Wysocki 	}
5949ed98953SRafael J. Wysocki 
5959ed98953SRafael J. Wysocki 	list_for_each_entry(link, &dev->links.suppliers, c_node) {
5969ed98953SRafael J. Wysocki 		if (link->flags & DL_FLAG_STATELESS)
5979ed98953SRafael J. Wysocki 			continue;
5989ed98953SRafael J. Wysocki 
5999ed98953SRafael J. Wysocki 		WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
6009ed98953SRafael J. Wysocki 		WRITE_ONCE(link->status, DL_STATE_ACTIVE);
6019ed98953SRafael J. Wysocki 	}
6029ed98953SRafael J. Wysocki 
6039ed98953SRafael J. Wysocki 	dev->links.status = DL_DEV_DRIVER_BOUND;
6049ed98953SRafael J. Wysocki 
6059ed98953SRafael J. Wysocki 	device_links_write_unlock();
6069ed98953SRafael J. Wysocki }
6079ed98953SRafael J. Wysocki 
6089ed98953SRafael J. Wysocki /**
6099ed98953SRafael J. Wysocki  * __device_links_no_driver - Update links of a device without a driver.
6109ed98953SRafael J. Wysocki  * @dev: Device without a drvier.
6119ed98953SRafael J. Wysocki  *
6129ed98953SRafael J. Wysocki  * Delete all non-persistent links from this device to any suppliers.
6139ed98953SRafael J. Wysocki  *
6149ed98953SRafael J. Wysocki  * Persistent links stay around, but their status is changed to "available",
6159ed98953SRafael J. Wysocki  * unless they already are in the "supplier unbind in progress" state in which
6169ed98953SRafael J. Wysocki  * case they need not be updated.
6179ed98953SRafael J. Wysocki  *
6189ed98953SRafael J. Wysocki  * Links with the DL_FLAG_STATELESS flag set are ignored.
6199ed98953SRafael J. Wysocki  */
6209ed98953SRafael J. Wysocki static void __device_links_no_driver(struct device *dev)
6219ed98953SRafael J. Wysocki {
6229ed98953SRafael J. Wysocki 	struct device_link *link, *ln;
6239ed98953SRafael J. Wysocki 
6249ed98953SRafael J. Wysocki 	list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
6259ed98953SRafael J. Wysocki 		if (link->flags & DL_FLAG_STATELESS)
6269ed98953SRafael J. Wysocki 			continue;
6279ed98953SRafael J. Wysocki 
628e88728f4SVivek Gautam 		if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
6290fe6f787SYong Wu 			__device_link_del(&link->kref);
63015cfb094SRafael J. Wysocki 		else if (link->status == DL_STATE_CONSUMER_PROBE ||
63115cfb094SRafael J. Wysocki 			 link->status == DL_STATE_ACTIVE)
6329ed98953SRafael J. Wysocki 			WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
6339ed98953SRafael J. Wysocki 	}
6349ed98953SRafael J. Wysocki 
6359ed98953SRafael J. Wysocki 	dev->links.status = DL_DEV_NO_DRIVER;
6369ed98953SRafael J. Wysocki }
6379ed98953SRafael J. Wysocki 
63815cfb094SRafael J. Wysocki /**
63915cfb094SRafael J. Wysocki  * device_links_no_driver - Update links after failing driver probe.
64015cfb094SRafael J. Wysocki  * @dev: Device whose driver has just failed to probe.
64115cfb094SRafael J. Wysocki  *
64215cfb094SRafael J. Wysocki  * Clean up leftover links to consumers for @dev and invoke
64315cfb094SRafael J. Wysocki  * %__device_links_no_driver() to update links to suppliers for it as
64415cfb094SRafael J. Wysocki  * appropriate.
64515cfb094SRafael J. Wysocki  *
64615cfb094SRafael J. Wysocki  * Links with the DL_FLAG_STATELESS flag set are ignored.
64715cfb094SRafael J. Wysocki  */
6489ed98953SRafael J. Wysocki void device_links_no_driver(struct device *dev)
6499ed98953SRafael J. Wysocki {
65015cfb094SRafael J. Wysocki 	struct device_link *link;
65115cfb094SRafael J. Wysocki 
6529ed98953SRafael J. Wysocki 	device_links_write_lock();
65315cfb094SRafael J. Wysocki 
65415cfb094SRafael J. Wysocki 	list_for_each_entry(link, &dev->links.consumers, s_node) {
65515cfb094SRafael J. Wysocki 		if (link->flags & DL_FLAG_STATELESS)
65615cfb094SRafael J. Wysocki 			continue;
65715cfb094SRafael J. Wysocki 
65815cfb094SRafael J. Wysocki 		/*
65915cfb094SRafael J. Wysocki 		 * The probe has failed, so if the status of the link is
66015cfb094SRafael J. Wysocki 		 * "consumer probe" or "active", it must have been added by
66115cfb094SRafael J. Wysocki 		 * a probing consumer while this device was still probing.
66215cfb094SRafael J. Wysocki 		 * Change its state to "dormant", as it represents a valid
66315cfb094SRafael J. Wysocki 		 * relationship, but it is not functionally meaningful.
66415cfb094SRafael J. Wysocki 		 */
66515cfb094SRafael J. Wysocki 		if (link->status == DL_STATE_CONSUMER_PROBE ||
66615cfb094SRafael J. Wysocki 		    link->status == DL_STATE_ACTIVE)
66715cfb094SRafael J. Wysocki 			WRITE_ONCE(link->status, DL_STATE_DORMANT);
66815cfb094SRafael J. Wysocki 	}
66915cfb094SRafael J. Wysocki 
6709ed98953SRafael J. Wysocki 	__device_links_no_driver(dev);
67115cfb094SRafael J. Wysocki 
6729ed98953SRafael J. Wysocki 	device_links_write_unlock();
6739ed98953SRafael J. Wysocki }
6749ed98953SRafael J. Wysocki 
6759ed98953SRafael J. Wysocki /**
6769ed98953SRafael J. Wysocki  * device_links_driver_cleanup - Update links after driver removal.
6779ed98953SRafael J. Wysocki  * @dev: Device whose driver has just gone away.
6789ed98953SRafael J. Wysocki  *
6799ed98953SRafael J. Wysocki  * Update links to consumers for @dev by changing their status to "dormant" and
6809ed98953SRafael J. Wysocki  * invoke %__device_links_no_driver() to update links to suppliers for it as
6819ed98953SRafael J. Wysocki  * appropriate.
6829ed98953SRafael J. Wysocki  *
6839ed98953SRafael J. Wysocki  * Links with the DL_FLAG_STATELESS flag set are ignored.
6849ed98953SRafael J. Wysocki  */
6859ed98953SRafael J. Wysocki void device_links_driver_cleanup(struct device *dev)
6869ed98953SRafael J. Wysocki {
687c8d50986SRafael J. Wysocki 	struct device_link *link, *ln;
6889ed98953SRafael J. Wysocki 
6899ed98953SRafael J. Wysocki 	device_links_write_lock();
6909ed98953SRafael J. Wysocki 
691c8d50986SRafael J. Wysocki 	list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
6929ed98953SRafael J. Wysocki 		if (link->flags & DL_FLAG_STATELESS)
6939ed98953SRafael J. Wysocki 			continue;
6949ed98953SRafael J. Wysocki 
695e88728f4SVivek Gautam 		WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
6969ed98953SRafael J. Wysocki 		WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
6971689cac5SVivek Gautam 
6981689cac5SVivek Gautam 		/*
6991689cac5SVivek Gautam 		 * autoremove the links between this @dev and its consumer
7001689cac5SVivek Gautam 		 * devices that are not active, i.e. where the link state
7011689cac5SVivek Gautam 		 * has moved to DL_STATE_SUPPLIER_UNBIND.
7021689cac5SVivek Gautam 		 */
7031689cac5SVivek Gautam 		if (link->status == DL_STATE_SUPPLIER_UNBIND &&
7041689cac5SVivek Gautam 		    link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
7050fe6f787SYong Wu 			__device_link_del(&link->kref);
7061689cac5SVivek Gautam 
7079ed98953SRafael J. Wysocki 		WRITE_ONCE(link->status, DL_STATE_DORMANT);
7089ed98953SRafael J. Wysocki 	}
7099ed98953SRafael J. Wysocki 
7109ed98953SRafael J. Wysocki 	__device_links_no_driver(dev);
7119ed98953SRafael J. Wysocki 
7129ed98953SRafael J. Wysocki 	device_links_write_unlock();
7139ed98953SRafael J. Wysocki }
7149ed98953SRafael J. Wysocki 
7159ed98953SRafael J. Wysocki /**
7169ed98953SRafael J. Wysocki  * device_links_busy - Check if there are any busy links to consumers.
7179ed98953SRafael J. Wysocki  * @dev: Device to check.
7189ed98953SRafael J. Wysocki  *
7199ed98953SRafael J. Wysocki  * Check each consumer of the device and return 'true' if its link's status
7209ed98953SRafael J. Wysocki  * is one of "consumer probe" or "active" (meaning that the given consumer is
7219ed98953SRafael J. Wysocki  * probing right now or its driver is present).  Otherwise, change the link
7229ed98953SRafael J. Wysocki  * state to "supplier unbind" to prevent the consumer from being probed
7239ed98953SRafael J. Wysocki  * successfully going forward.
7249ed98953SRafael J. Wysocki  *
7259ed98953SRafael J. Wysocki  * Return 'false' if there are no probing or active consumers.
7269ed98953SRafael J. Wysocki  *
7279ed98953SRafael J. Wysocki  * Links with the DL_FLAG_STATELESS flag set are ignored.
7289ed98953SRafael J. Wysocki  */
7299ed98953SRafael J. Wysocki bool device_links_busy(struct device *dev)
7309ed98953SRafael J. Wysocki {
7319ed98953SRafael J. Wysocki 	struct device_link *link;
7329ed98953SRafael J. Wysocki 	bool ret = false;
7339ed98953SRafael J. Wysocki 
7349ed98953SRafael J. Wysocki 	device_links_write_lock();
7359ed98953SRafael J. Wysocki 
7369ed98953SRafael J. Wysocki 	list_for_each_entry(link, &dev->links.consumers, s_node) {
7379ed98953SRafael J. Wysocki 		if (link->flags & DL_FLAG_STATELESS)
7389ed98953SRafael J. Wysocki 			continue;
7399ed98953SRafael J. Wysocki 
7409ed98953SRafael J. Wysocki 		if (link->status == DL_STATE_CONSUMER_PROBE
7419ed98953SRafael J. Wysocki 		    || link->status == DL_STATE_ACTIVE) {
7429ed98953SRafael J. Wysocki 			ret = true;
7439ed98953SRafael J. Wysocki 			break;
7449ed98953SRafael J. Wysocki 		}
7459ed98953SRafael J. Wysocki 		WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
7469ed98953SRafael J. Wysocki 	}
7479ed98953SRafael J. Wysocki 
7489ed98953SRafael J. Wysocki 	dev->links.status = DL_DEV_UNBINDING;
7499ed98953SRafael J. Wysocki 
7509ed98953SRafael J. Wysocki 	device_links_write_unlock();
7519ed98953SRafael J. Wysocki 	return ret;
7529ed98953SRafael J. Wysocki }
7539ed98953SRafael J. Wysocki 
7549ed98953SRafael J. Wysocki /**
7559ed98953SRafael J. Wysocki  * device_links_unbind_consumers - Force unbind consumers of the given device.
7569ed98953SRafael J. Wysocki  * @dev: Device to unbind the consumers of.
7579ed98953SRafael J. Wysocki  *
7589ed98953SRafael J. Wysocki  * Walk the list of links to consumers for @dev and if any of them is in the
7599ed98953SRafael J. Wysocki  * "consumer probe" state, wait for all device probes in progress to complete
7609ed98953SRafael J. Wysocki  * and start over.
7619ed98953SRafael J. Wysocki  *
7629ed98953SRafael J. Wysocki  * If that's not the case, change the status of the link to "supplier unbind"
7639ed98953SRafael J. Wysocki  * and check if the link was in the "active" state.  If so, force the consumer
7649ed98953SRafael J. Wysocki  * driver to unbind and start over (the consumer will not re-probe as we have
7659ed98953SRafael J. Wysocki  * changed the state of the link already).
7669ed98953SRafael J. Wysocki  *
7679ed98953SRafael J. Wysocki  * Links with the DL_FLAG_STATELESS flag set are ignored.
7689ed98953SRafael J. Wysocki  */
7699ed98953SRafael J. Wysocki void device_links_unbind_consumers(struct device *dev)
7709ed98953SRafael J. Wysocki {
7719ed98953SRafael J. Wysocki 	struct device_link *link;
7729ed98953SRafael J. Wysocki 
7739ed98953SRafael J. Wysocki  start:
7749ed98953SRafael J. Wysocki 	device_links_write_lock();
7759ed98953SRafael J. Wysocki 
7769ed98953SRafael J. Wysocki 	list_for_each_entry(link, &dev->links.consumers, s_node) {
7779ed98953SRafael J. Wysocki 		enum device_link_state status;
7789ed98953SRafael J. Wysocki 
7799ed98953SRafael J. Wysocki 		if (link->flags & DL_FLAG_STATELESS)
7809ed98953SRafael J. Wysocki 			continue;
7819ed98953SRafael J. Wysocki 
7829ed98953SRafael J. Wysocki 		status = link->status;
7839ed98953SRafael J. Wysocki 		if (status == DL_STATE_CONSUMER_PROBE) {
7849ed98953SRafael J. Wysocki 			device_links_write_unlock();
7859ed98953SRafael J. Wysocki 
7869ed98953SRafael J. Wysocki 			wait_for_device_probe();
7879ed98953SRafael J. Wysocki 			goto start;
7889ed98953SRafael J. Wysocki 		}
7899ed98953SRafael J. Wysocki 		WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
7909ed98953SRafael J. Wysocki 		if (status == DL_STATE_ACTIVE) {
7919ed98953SRafael J. Wysocki 			struct device *consumer = link->consumer;
7929ed98953SRafael J. Wysocki 
7939ed98953SRafael J. Wysocki 			get_device(consumer);
7949ed98953SRafael J. Wysocki 
7959ed98953SRafael J. Wysocki 			device_links_write_unlock();
7969ed98953SRafael J. Wysocki 
7979ed98953SRafael J. Wysocki 			device_release_driver_internal(consumer, NULL,
7989ed98953SRafael J. Wysocki 						       consumer->parent);
7999ed98953SRafael J. Wysocki 			put_device(consumer);
8009ed98953SRafael J. Wysocki 			goto start;
8019ed98953SRafael J. Wysocki 		}
8029ed98953SRafael J. Wysocki 	}
8039ed98953SRafael J. Wysocki 
8049ed98953SRafael J. Wysocki 	device_links_write_unlock();
8059ed98953SRafael J. Wysocki }
8069ed98953SRafael J. Wysocki 
8079ed98953SRafael J. Wysocki /**
8089ed98953SRafael J. Wysocki  * device_links_purge - Delete existing links to other devices.
8099ed98953SRafael J. Wysocki  * @dev: Target device.
8109ed98953SRafael J. Wysocki  */
8119ed98953SRafael J. Wysocki static void device_links_purge(struct device *dev)
8129ed98953SRafael J. Wysocki {
8139ed98953SRafael J. Wysocki 	struct device_link *link, *ln;
8149ed98953SRafael J. Wysocki 
8159ed98953SRafael J. Wysocki 	/*
8169ed98953SRafael J. Wysocki 	 * Delete all of the remaining links from this device to any other
8179ed98953SRafael J. Wysocki 	 * devices (either consumers or suppliers).
8189ed98953SRafael J. Wysocki 	 */
8199ed98953SRafael J. Wysocki 	device_links_write_lock();
8209ed98953SRafael J. Wysocki 
8219ed98953SRafael J. Wysocki 	list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
8229ed98953SRafael J. Wysocki 		WARN_ON(link->status == DL_STATE_ACTIVE);
823ead18c23SLukas Wunner 		__device_link_del(&link->kref);
8249ed98953SRafael J. Wysocki 	}
8259ed98953SRafael J. Wysocki 
8269ed98953SRafael J. Wysocki 	list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
8279ed98953SRafael J. Wysocki 		WARN_ON(link->status != DL_STATE_DORMANT &&
8289ed98953SRafael J. Wysocki 			link->status != DL_STATE_NONE);
829ead18c23SLukas Wunner 		__device_link_del(&link->kref);
8309ed98953SRafael J. Wysocki 	}
8319ed98953SRafael J. Wysocki 
8329ed98953SRafael J. Wysocki 	device_links_write_unlock();
8339ed98953SRafael J. Wysocki }
8349ed98953SRafael J. Wysocki 
8359ed98953SRafael J. Wysocki /* Device links support end. */
8369ed98953SRafael J. Wysocki 
8371da177e4SLinus Torvalds int (*platform_notify)(struct device *dev) = NULL;
8381da177e4SLinus Torvalds int (*platform_notify_remove)(struct device *dev) = NULL;
839e105b8bfSDan Williams static struct kobject *dev_kobj;
840e105b8bfSDan Williams struct kobject *sysfs_dev_char_kobj;
841e105b8bfSDan Williams struct kobject *sysfs_dev_block_kobj;
8421da177e4SLinus Torvalds 
8435e33bc41SRafael J. Wysocki static DEFINE_MUTEX(device_hotplug_lock);
8445e33bc41SRafael J. Wysocki 
8455e33bc41SRafael J. Wysocki void lock_device_hotplug(void)
8465e33bc41SRafael J. Wysocki {
8475e33bc41SRafael J. Wysocki 	mutex_lock(&device_hotplug_lock);
8485e33bc41SRafael J. Wysocki }
8495e33bc41SRafael J. Wysocki 
8505e33bc41SRafael J. Wysocki void unlock_device_hotplug(void)
8515e33bc41SRafael J. Wysocki {
8525e33bc41SRafael J. Wysocki 	mutex_unlock(&device_hotplug_lock);
8535e33bc41SRafael J. Wysocki }
8545e33bc41SRafael J. Wysocki 
8555e33bc41SRafael J. Wysocki int lock_device_hotplug_sysfs(void)
8565e33bc41SRafael J. Wysocki {
8575e33bc41SRafael J. Wysocki 	if (mutex_trylock(&device_hotplug_lock))
8585e33bc41SRafael J. Wysocki 		return 0;
8595e33bc41SRafael J. Wysocki 
8605e33bc41SRafael J. Wysocki 	/* Avoid busy looping (5 ms of sleep should do). */
8615e33bc41SRafael J. Wysocki 	msleep(5);
8625e33bc41SRafael J. Wysocki 	return restart_syscall();
8635e33bc41SRafael J. Wysocki }
8645e33bc41SRafael J. Wysocki 
8654e886c29SGreg Kroah-Hartman #ifdef CONFIG_BLOCK
8664e886c29SGreg Kroah-Hartman static inline int device_is_not_partition(struct device *dev)
8674e886c29SGreg Kroah-Hartman {
8684e886c29SGreg Kroah-Hartman 	return !(dev->type == &part_type);
8694e886c29SGreg Kroah-Hartman }
8704e886c29SGreg Kroah-Hartman #else
8714e886c29SGreg Kroah-Hartman static inline int device_is_not_partition(struct device *dev)
8724e886c29SGreg Kroah-Hartman {
8734e886c29SGreg Kroah-Hartman 	return 1;
8744e886c29SGreg Kroah-Hartman }
8754e886c29SGreg Kroah-Hartman #endif
8761da177e4SLinus Torvalds 
87707de0e86SHeikki Krogerus static int
87807de0e86SHeikki Krogerus device_platform_notify(struct device *dev, enum kobject_action action)
87907de0e86SHeikki Krogerus {
8807847a145SHeikki Krogerus 	int ret;
8817847a145SHeikki Krogerus 
8827847a145SHeikki Krogerus 	ret = acpi_platform_notify(dev, action);
8837847a145SHeikki Krogerus 	if (ret)
8847847a145SHeikki Krogerus 		return ret;
8857847a145SHeikki Krogerus 
88659abd836SHeikki Krogerus 	ret = software_node_notify(dev, action);
88759abd836SHeikki Krogerus 	if (ret)
88859abd836SHeikki Krogerus 		return ret;
88959abd836SHeikki Krogerus 
89007de0e86SHeikki Krogerus 	if (platform_notify && action == KOBJ_ADD)
89107de0e86SHeikki Krogerus 		platform_notify(dev);
89207de0e86SHeikki Krogerus 	else if (platform_notify_remove && action == KOBJ_REMOVE)
89307de0e86SHeikki Krogerus 		platform_notify_remove(dev);
89407de0e86SHeikki Krogerus 	return 0;
89507de0e86SHeikki Krogerus }
89607de0e86SHeikki Krogerus 
8973e95637aSAlan Stern /**
8983e95637aSAlan Stern  * dev_driver_string - Return a device's driver name, if at all possible
8993e95637aSAlan Stern  * @dev: struct device to get the name of
9003e95637aSAlan Stern  *
9013e95637aSAlan Stern  * Will return the device's driver's name if it is bound to a device.  If
9029169c012Syan  * the device is not bound to a driver, it will return the name of the bus
9033e95637aSAlan Stern  * it is attached to.  If it is not attached to a bus either, an empty
9043e95637aSAlan Stern  * string will be returned.
9053e95637aSAlan Stern  */
906bf9ca69fSJean Delvare const char *dev_driver_string(const struct device *dev)
9073e95637aSAlan Stern {
9083589972eSAlan Stern 	struct device_driver *drv;
9093589972eSAlan Stern 
9103589972eSAlan Stern 	/* dev->driver can change to NULL underneath us because of unbinding,
9113589972eSAlan Stern 	 * so be careful about accessing it.  dev->bus and dev->class should
9123589972eSAlan Stern 	 * never change once they are set, so they don't need special care.
9133589972eSAlan Stern 	 */
9146aa7de05SMark Rutland 	drv = READ_ONCE(dev->driver);
9153589972eSAlan Stern 	return drv ? drv->name :
916a456b702SJean Delvare 			(dev->bus ? dev->bus->name :
917a456b702SJean Delvare 			(dev->class ? dev->class->name : ""));
9183e95637aSAlan Stern }
919310a922dSMatthew Wilcox EXPORT_SYMBOL(dev_driver_string);
9203e95637aSAlan Stern 
9211da177e4SLinus Torvalds #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
9221da177e4SLinus Torvalds 
9234a3ad20cSGreg Kroah-Hartman static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
9244a3ad20cSGreg Kroah-Hartman 			     char *buf)
9251da177e4SLinus Torvalds {
9261da177e4SLinus Torvalds 	struct device_attribute *dev_attr = to_dev_attr(attr);
927b0d1f807SLars-Peter Clausen 	struct device *dev = kobj_to_dev(kobj);
9284a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds 	if (dev_attr->show)
93154b6f35cSYani Ioannou 		ret = dev_attr->show(dev, dev_attr, buf);
932815d2d50SAndrew Morton 	if (ret >= (ssize_t)PAGE_SIZE) {
933a52668c6SSergey Senozhatsky 		printk("dev_attr_show: %pS returned bad count\n",
934a52668c6SSergey Senozhatsky 				dev_attr->show);
935815d2d50SAndrew Morton 	}
9361da177e4SLinus Torvalds 	return ret;
9371da177e4SLinus Torvalds }
9381da177e4SLinus Torvalds 
9394a3ad20cSGreg Kroah-Hartman static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
9401da177e4SLinus Torvalds 			      const char *buf, size_t count)
9411da177e4SLinus Torvalds {
9421da177e4SLinus Torvalds 	struct device_attribute *dev_attr = to_dev_attr(attr);
943b0d1f807SLars-Peter Clausen 	struct device *dev = kobj_to_dev(kobj);
9444a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds 	if (dev_attr->store)
94754b6f35cSYani Ioannou 		ret = dev_attr->store(dev, dev_attr, buf, count);
9481da177e4SLinus Torvalds 	return ret;
9491da177e4SLinus Torvalds }
9501da177e4SLinus Torvalds 
95152cf25d0SEmese Revfy static const struct sysfs_ops dev_sysfs_ops = {
9521da177e4SLinus Torvalds 	.show	= dev_attr_show,
9531da177e4SLinus Torvalds 	.store	= dev_attr_store,
9541da177e4SLinus Torvalds };
9551da177e4SLinus Torvalds 
956ca22e56dSKay Sievers #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
957ca22e56dSKay Sievers 
958ca22e56dSKay Sievers ssize_t device_store_ulong(struct device *dev,
959ca22e56dSKay Sievers 			   struct device_attribute *attr,
960ca22e56dSKay Sievers 			   const char *buf, size_t size)
961ca22e56dSKay Sievers {
962ca22e56dSKay Sievers 	struct dev_ext_attribute *ea = to_ext_attr(attr);
963f88184bfSKaitao cheng 	int ret;
964f88184bfSKaitao cheng 	unsigned long new;
965f88184bfSKaitao cheng 
966f88184bfSKaitao cheng 	ret = kstrtoul(buf, 0, &new);
967f88184bfSKaitao cheng 	if (ret)
968f88184bfSKaitao cheng 		return ret;
969ca22e56dSKay Sievers 	*(unsigned long *)(ea->var) = new;
970ca22e56dSKay Sievers 	/* Always return full write size even if we didn't consume all */
971ca22e56dSKay Sievers 	return size;
972ca22e56dSKay Sievers }
973ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(device_store_ulong);
974ca22e56dSKay Sievers 
975ca22e56dSKay Sievers ssize_t device_show_ulong(struct device *dev,
976ca22e56dSKay Sievers 			  struct device_attribute *attr,
977ca22e56dSKay Sievers 			  char *buf)
978ca22e56dSKay Sievers {
979ca22e56dSKay Sievers 	struct dev_ext_attribute *ea = to_ext_attr(attr);
980ca22e56dSKay Sievers 	return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
981ca22e56dSKay Sievers }
982ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(device_show_ulong);
983ca22e56dSKay Sievers 
984ca22e56dSKay Sievers ssize_t device_store_int(struct device *dev,
985ca22e56dSKay Sievers 			 struct device_attribute *attr,
986ca22e56dSKay Sievers 			 const char *buf, size_t size)
987ca22e56dSKay Sievers {
988ca22e56dSKay Sievers 	struct dev_ext_attribute *ea = to_ext_attr(attr);
989f88184bfSKaitao cheng 	int ret;
990f88184bfSKaitao cheng 	long new;
991f88184bfSKaitao cheng 
992f88184bfSKaitao cheng 	ret = kstrtol(buf, 0, &new);
993f88184bfSKaitao cheng 	if (ret)
994f88184bfSKaitao cheng 		return ret;
995f88184bfSKaitao cheng 
996f88184bfSKaitao cheng 	if (new > INT_MAX || new < INT_MIN)
997ca22e56dSKay Sievers 		return -EINVAL;
998ca22e56dSKay Sievers 	*(int *)(ea->var) = new;
999ca22e56dSKay Sievers 	/* Always return full write size even if we didn't consume all */
1000ca22e56dSKay Sievers 	return size;
1001ca22e56dSKay Sievers }
1002ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(device_store_int);
1003ca22e56dSKay Sievers 
1004ca22e56dSKay Sievers ssize_t device_show_int(struct device *dev,
1005ca22e56dSKay Sievers 			struct device_attribute *attr,
1006ca22e56dSKay Sievers 			char *buf)
1007ca22e56dSKay Sievers {
1008ca22e56dSKay Sievers 	struct dev_ext_attribute *ea = to_ext_attr(attr);
1009ca22e56dSKay Sievers 
1010ca22e56dSKay Sievers 	return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
1011ca22e56dSKay Sievers }
1012ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(device_show_int);
10131da177e4SLinus Torvalds 
101491872392SBorislav Petkov ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
101591872392SBorislav Petkov 			  const char *buf, size_t size)
101691872392SBorislav Petkov {
101791872392SBorislav Petkov 	struct dev_ext_attribute *ea = to_ext_attr(attr);
101891872392SBorislav Petkov 
101991872392SBorislav Petkov 	if (strtobool(buf, ea->var) < 0)
102091872392SBorislav Petkov 		return -EINVAL;
102191872392SBorislav Petkov 
102291872392SBorislav Petkov 	return size;
102391872392SBorislav Petkov }
102491872392SBorislav Petkov EXPORT_SYMBOL_GPL(device_store_bool);
102591872392SBorislav Petkov 
102691872392SBorislav Petkov ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
102791872392SBorislav Petkov 			 char *buf)
102891872392SBorislav Petkov {
102991872392SBorislav Petkov 	struct dev_ext_attribute *ea = to_ext_attr(attr);
103091872392SBorislav Petkov 
103191872392SBorislav Petkov 	return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
103291872392SBorislav Petkov }
103391872392SBorislav Petkov EXPORT_SYMBOL_GPL(device_show_bool);
103491872392SBorislav Petkov 
10351da177e4SLinus Torvalds /**
10361da177e4SLinus Torvalds  * device_release - free device structure.
10371da177e4SLinus Torvalds  * @kobj: device's kobject.
10381da177e4SLinus Torvalds  *
10391da177e4SLinus Torvalds  * This is called once the reference count for the object
10401da177e4SLinus Torvalds  * reaches 0. We forward the call to the device's release
10411da177e4SLinus Torvalds  * method, which should handle actually freeing the structure.
10421da177e4SLinus Torvalds  */
10431da177e4SLinus Torvalds static void device_release(struct kobject *kobj)
10441da177e4SLinus Torvalds {
1045b0d1f807SLars-Peter Clausen 	struct device *dev = kobj_to_dev(kobj);
1046fb069a5dSGreg Kroah-Hartman 	struct device_private *p = dev->p;
10471da177e4SLinus Torvalds 
1048a525a3ddSMing Lei 	/*
1049a525a3ddSMing Lei 	 * Some platform devices are driven without driver attached
1050a525a3ddSMing Lei 	 * and managed resources may have been acquired.  Make sure
1051a525a3ddSMing Lei 	 * all resources are released.
1052a525a3ddSMing Lei 	 *
1053a525a3ddSMing Lei 	 * Drivers still can add resources into device after device
1054a525a3ddSMing Lei 	 * is deleted but alive, so release devres here to avoid
1055a525a3ddSMing Lei 	 * possible memory leak.
1056a525a3ddSMing Lei 	 */
1057a525a3ddSMing Lei 	devres_release_all(dev);
1058a525a3ddSMing Lei 
10591da177e4SLinus Torvalds 	if (dev->release)
10601da177e4SLinus Torvalds 		dev->release(dev);
1061f9f852dfSKay Sievers 	else if (dev->type && dev->type->release)
1062f9f852dfSKay Sievers 		dev->type->release(dev);
10632620efefSGreg Kroah-Hartman 	else if (dev->class && dev->class->dev_release)
10642620efefSGreg Kroah-Hartman 		dev->class->dev_release(dev);
1065f810a5cfSArjan van de Ven 	else
1066186bddb2SEzequiel Garcia 		WARN(1, KERN_ERR "Device '%s' does not have a release() function, it is broken and must be fixed. See Documentation/kobject.txt.\n",
10671e0b2cf9SKay Sievers 			dev_name(dev));
1068fb069a5dSGreg Kroah-Hartman 	kfree(p);
10691da177e4SLinus Torvalds }
10701da177e4SLinus Torvalds 
1071bc451f20SEric W. Biederman static const void *device_namespace(struct kobject *kobj)
1072bc451f20SEric W. Biederman {
1073b0d1f807SLars-Peter Clausen 	struct device *dev = kobj_to_dev(kobj);
1074bc451f20SEric W. Biederman 	const void *ns = NULL;
1075bc451f20SEric W. Biederman 
1076bc451f20SEric W. Biederman 	if (dev->class && dev->class->ns_type)
1077bc451f20SEric W. Biederman 		ns = dev->class->namespace(dev);
1078bc451f20SEric W. Biederman 
1079bc451f20SEric W. Biederman 	return ns;
1080bc451f20SEric W. Biederman }
1081bc451f20SEric W. Biederman 
10829944e894SDmitry Torokhov static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
10839944e894SDmitry Torokhov {
10849944e894SDmitry Torokhov 	struct device *dev = kobj_to_dev(kobj);
10859944e894SDmitry Torokhov 
10869944e894SDmitry Torokhov 	if (dev->class && dev->class->get_ownership)
10879944e894SDmitry Torokhov 		dev->class->get_ownership(dev, uid, gid);
10889944e894SDmitry Torokhov }
10899944e894SDmitry Torokhov 
10908f4afc41SGreg Kroah-Hartman static struct kobj_type device_ktype = {
10911da177e4SLinus Torvalds 	.release	= device_release,
10921da177e4SLinus Torvalds 	.sysfs_ops	= &dev_sysfs_ops,
1093bc451f20SEric W. Biederman 	.namespace	= device_namespace,
10949944e894SDmitry Torokhov 	.get_ownership	= device_get_ownership,
10951da177e4SLinus Torvalds };
10961da177e4SLinus Torvalds 
10971da177e4SLinus Torvalds 
1098312c004dSKay Sievers static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
10991da177e4SLinus Torvalds {
11001da177e4SLinus Torvalds 	struct kobj_type *ktype = get_ktype(kobj);
11011da177e4SLinus Torvalds 
11028f4afc41SGreg Kroah-Hartman 	if (ktype == &device_ktype) {
1103b0d1f807SLars-Peter Clausen 		struct device *dev = kobj_to_dev(kobj);
11041da177e4SLinus Torvalds 		if (dev->bus)
11051da177e4SLinus Torvalds 			return 1;
110623681e47SGreg Kroah-Hartman 		if (dev->class)
110723681e47SGreg Kroah-Hartman 			return 1;
11081da177e4SLinus Torvalds 	}
11091da177e4SLinus Torvalds 	return 0;
11101da177e4SLinus Torvalds }
11111da177e4SLinus Torvalds 
1112312c004dSKay Sievers static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
11131da177e4SLinus Torvalds {
1114b0d1f807SLars-Peter Clausen 	struct device *dev = kobj_to_dev(kobj);
11151da177e4SLinus Torvalds 
111623681e47SGreg Kroah-Hartman 	if (dev->bus)
11171da177e4SLinus Torvalds 		return dev->bus->name;
111823681e47SGreg Kroah-Hartman 	if (dev->class)
111923681e47SGreg Kroah-Hartman 		return dev->class->name;
112023681e47SGreg Kroah-Hartman 	return NULL;
11211da177e4SLinus Torvalds }
11221da177e4SLinus Torvalds 
11237eff2e7aSKay Sievers static int dev_uevent(struct kset *kset, struct kobject *kobj,
11247eff2e7aSKay Sievers 		      struct kobj_uevent_env *env)
11251da177e4SLinus Torvalds {
1126b0d1f807SLars-Peter Clausen 	struct device *dev = kobj_to_dev(kobj);
11271da177e4SLinus Torvalds 	int retval = 0;
11281da177e4SLinus Torvalds 
11296fcf53acSKay Sievers 	/* add device node properties if present */
113023681e47SGreg Kroah-Hartman 	if (MAJOR(dev->devt)) {
11316fcf53acSKay Sievers 		const char *tmp;
11326fcf53acSKay Sievers 		const char *name;
11332c9ede55SAl Viro 		umode_t mode = 0;
11344e4098a3SGreg Kroah-Hartman 		kuid_t uid = GLOBAL_ROOT_UID;
11354e4098a3SGreg Kroah-Hartman 		kgid_t gid = GLOBAL_ROOT_GID;
11366fcf53acSKay Sievers 
11377eff2e7aSKay Sievers 		add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
11387eff2e7aSKay Sievers 		add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
11393c2670e6SKay Sievers 		name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
11406fcf53acSKay Sievers 		if (name) {
11416fcf53acSKay Sievers 			add_uevent_var(env, "DEVNAME=%s", name);
1142e454cea2SKay Sievers 			if (mode)
1143e454cea2SKay Sievers 				add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
11444e4098a3SGreg Kroah-Hartman 			if (!uid_eq(uid, GLOBAL_ROOT_UID))
11454e4098a3SGreg Kroah-Hartman 				add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
11464e4098a3SGreg Kroah-Hartman 			if (!gid_eq(gid, GLOBAL_ROOT_GID))
11474e4098a3SGreg Kroah-Hartman 				add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
11483c2670e6SKay Sievers 			kfree(tmp);
11496fcf53acSKay Sievers 		}
115023681e47SGreg Kroah-Hartman 	}
115123681e47SGreg Kroah-Hartman 
1152414264f9SKay Sievers 	if (dev->type && dev->type->name)
11537eff2e7aSKay Sievers 		add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
1154414264f9SKay Sievers 
1155239378f1SKay Sievers 	if (dev->driver)
11567eff2e7aSKay Sievers 		add_uevent_var(env, "DRIVER=%s", dev->driver->name);
1157239378f1SKay Sievers 
115807d57a32SGrant Likely 	/* Add common DT information about the device */
115907d57a32SGrant Likely 	of_device_uevent(dev, env);
116007d57a32SGrant Likely 
11611da177e4SLinus Torvalds 	/* have the bus specific function add its stuff */
11627eff2e7aSKay Sievers 	if (dev->bus && dev->bus->uevent) {
11637eff2e7aSKay Sievers 		retval = dev->bus->uevent(dev, env);
1164f9f852dfSKay Sievers 		if (retval)
11657dc72b28SGreg Kroah-Hartman 			pr_debug("device: '%s': %s: bus uevent() returned %d\n",
11661e0b2cf9SKay Sievers 				 dev_name(dev), __func__, retval);
11671da177e4SLinus Torvalds 	}
11681da177e4SLinus Torvalds 
11692620efefSGreg Kroah-Hartman 	/* have the class specific function add its stuff */
11707eff2e7aSKay Sievers 	if (dev->class && dev->class->dev_uevent) {
11717eff2e7aSKay Sievers 		retval = dev->class->dev_uevent(dev, env);
1172f9f852dfSKay Sievers 		if (retval)
11737dc72b28SGreg Kroah-Hartman 			pr_debug("device: '%s': %s: class uevent() "
11741e0b2cf9SKay Sievers 				 "returned %d\n", dev_name(dev),
11752b3a302aSHarvey Harrison 				 __func__, retval);
11762620efefSGreg Kroah-Hartman 	}
1177f9f852dfSKay Sievers 
1178eef35c2dSStefan Weil 	/* have the device type specific function add its stuff */
11797eff2e7aSKay Sievers 	if (dev->type && dev->type->uevent) {
11807eff2e7aSKay Sievers 		retval = dev->type->uevent(dev, env);
1181f9f852dfSKay Sievers 		if (retval)
11827dc72b28SGreg Kroah-Hartman 			pr_debug("device: '%s': %s: dev_type uevent() "
11831e0b2cf9SKay Sievers 				 "returned %d\n", dev_name(dev),
11842b3a302aSHarvey Harrison 				 __func__, retval);
11852620efefSGreg Kroah-Hartman 	}
11862620efefSGreg Kroah-Hartman 
11871da177e4SLinus Torvalds 	return retval;
11881da177e4SLinus Torvalds }
11891da177e4SLinus Torvalds 
11909cd43611SEmese Revfy static const struct kset_uevent_ops device_uevent_ops = {
1191312c004dSKay Sievers 	.filter =	dev_uevent_filter,
1192312c004dSKay Sievers 	.name =		dev_uevent_name,
1193312c004dSKay Sievers 	.uevent =	dev_uevent,
11941da177e4SLinus Torvalds };
11951da177e4SLinus Torvalds 
1196c5e064a6SGreg Kroah-Hartman static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
119716574dccSKay Sievers 			   char *buf)
119816574dccSKay Sievers {
119916574dccSKay Sievers 	struct kobject *top_kobj;
120016574dccSKay Sievers 	struct kset *kset;
12017eff2e7aSKay Sievers 	struct kobj_uevent_env *env = NULL;
120216574dccSKay Sievers 	int i;
120316574dccSKay Sievers 	size_t count = 0;
120416574dccSKay Sievers 	int retval;
120516574dccSKay Sievers 
120616574dccSKay Sievers 	/* search the kset, the device belongs to */
120716574dccSKay Sievers 	top_kobj = &dev->kobj;
12085c5daf65SKay Sievers 	while (!top_kobj->kset && top_kobj->parent)
120916574dccSKay Sievers 		top_kobj = top_kobj->parent;
121016574dccSKay Sievers 	if (!top_kobj->kset)
121116574dccSKay Sievers 		goto out;
12125c5daf65SKay Sievers 
121316574dccSKay Sievers 	kset = top_kobj->kset;
121416574dccSKay Sievers 	if (!kset->uevent_ops || !kset->uevent_ops->uevent)
121516574dccSKay Sievers 		goto out;
121616574dccSKay Sievers 
121716574dccSKay Sievers 	/* respect filter */
121816574dccSKay Sievers 	if (kset->uevent_ops && kset->uevent_ops->filter)
121916574dccSKay Sievers 		if (!kset->uevent_ops->filter(kset, &dev->kobj))
122016574dccSKay Sievers 			goto out;
122116574dccSKay Sievers 
12227eff2e7aSKay Sievers 	env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
12237eff2e7aSKay Sievers 	if (!env)
1224c7308c81SGreg Kroah-Hartman 		return -ENOMEM;
1225c7308c81SGreg Kroah-Hartman 
122616574dccSKay Sievers 	/* let the kset specific function add its keys */
12277eff2e7aSKay Sievers 	retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
122816574dccSKay Sievers 	if (retval)
122916574dccSKay Sievers 		goto out;
123016574dccSKay Sievers 
123116574dccSKay Sievers 	/* copy keys to file */
12327eff2e7aSKay Sievers 	for (i = 0; i < env->envp_idx; i++)
12337eff2e7aSKay Sievers 		count += sprintf(&buf[count], "%s\n", env->envp[i]);
123416574dccSKay Sievers out:
12357eff2e7aSKay Sievers 	kfree(env);
123616574dccSKay Sievers 	return count;
123716574dccSKay Sievers }
123816574dccSKay Sievers 
1239c5e064a6SGreg Kroah-Hartman static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
1240a7fd6706SKay Sievers 			    const char *buf, size_t count)
1241a7fd6706SKay Sievers {
1242df44b479SPeter Rajnoha 	int rc;
1243df44b479SPeter Rajnoha 
1244df44b479SPeter Rajnoha 	rc = kobject_synth_uevent(&dev->kobj, buf, count);
1245df44b479SPeter Rajnoha 
1246df44b479SPeter Rajnoha 	if (rc) {
1247f36776faSPeter Rajnoha 		dev_err(dev, "uevent: failed to send synthetic uevent\n");
1248df44b479SPeter Rajnoha 		return rc;
1249df44b479SPeter Rajnoha 	}
125060a96a59SKay Sievers 
1251a7fd6706SKay Sievers 	return count;
1252a7fd6706SKay Sievers }
1253c5e064a6SGreg Kroah-Hartman static DEVICE_ATTR_RW(uevent);
1254a7fd6706SKay Sievers 
1255c5e064a6SGreg Kroah-Hartman static ssize_t online_show(struct device *dev, struct device_attribute *attr,
12564f3549d7SRafael J. Wysocki 			   char *buf)
12574f3549d7SRafael J. Wysocki {
12584f3549d7SRafael J. Wysocki 	bool val;
12594f3549d7SRafael J. Wysocki 
12605e33bc41SRafael J. Wysocki 	device_lock(dev);
12614f3549d7SRafael J. Wysocki 	val = !dev->offline;
12625e33bc41SRafael J. Wysocki 	device_unlock(dev);
12634f3549d7SRafael J. Wysocki 	return sprintf(buf, "%u\n", val);
12644f3549d7SRafael J. Wysocki }
12654f3549d7SRafael J. Wysocki 
1266c5e064a6SGreg Kroah-Hartman static ssize_t online_store(struct device *dev, struct device_attribute *attr,
12674f3549d7SRafael J. Wysocki 			    const char *buf, size_t count)
12684f3549d7SRafael J. Wysocki {
12694f3549d7SRafael J. Wysocki 	bool val;
12704f3549d7SRafael J. Wysocki 	int ret;
12714f3549d7SRafael J. Wysocki 
12724f3549d7SRafael J. Wysocki 	ret = strtobool(buf, &val);
12734f3549d7SRafael J. Wysocki 	if (ret < 0)
12744f3549d7SRafael J. Wysocki 		return ret;
12754f3549d7SRafael J. Wysocki 
12765e33bc41SRafael J. Wysocki 	ret = lock_device_hotplug_sysfs();
12775e33bc41SRafael J. Wysocki 	if (ret)
12785e33bc41SRafael J. Wysocki 		return ret;
12795e33bc41SRafael J. Wysocki 
12804f3549d7SRafael J. Wysocki 	ret = val ? device_online(dev) : device_offline(dev);
12814f3549d7SRafael J. Wysocki 	unlock_device_hotplug();
12824f3549d7SRafael J. Wysocki 	return ret < 0 ? ret : count;
12834f3549d7SRafael J. Wysocki }
1284c5e064a6SGreg Kroah-Hartman static DEVICE_ATTR_RW(online);
12854f3549d7SRafael J. Wysocki 
1286fa6fdb33SGreg Kroah-Hartman int device_add_groups(struct device *dev, const struct attribute_group **groups)
1287621a1672SDmitry Torokhov {
12883e9b2baeSGreg Kroah-Hartman 	return sysfs_create_groups(&dev->kobj, groups);
1289621a1672SDmitry Torokhov }
1290a7670d42SDmitry Torokhov EXPORT_SYMBOL_GPL(device_add_groups);
1291621a1672SDmitry Torokhov 
1292fa6fdb33SGreg Kroah-Hartman void device_remove_groups(struct device *dev,
1293a4dbd674SDavid Brownell 			  const struct attribute_group **groups)
1294621a1672SDmitry Torokhov {
12953e9b2baeSGreg Kroah-Hartman 	sysfs_remove_groups(&dev->kobj, groups);
1296621a1672SDmitry Torokhov }
1297a7670d42SDmitry Torokhov EXPORT_SYMBOL_GPL(device_remove_groups);
1298de0ff00dSGreg Kroah-Hartman 
129957b8ff07SDmitry Torokhov union device_attr_group_devres {
130057b8ff07SDmitry Torokhov 	const struct attribute_group *group;
130157b8ff07SDmitry Torokhov 	const struct attribute_group **groups;
130257b8ff07SDmitry Torokhov };
130357b8ff07SDmitry Torokhov 
130457b8ff07SDmitry Torokhov static int devm_attr_group_match(struct device *dev, void *res, void *data)
130557b8ff07SDmitry Torokhov {
130657b8ff07SDmitry Torokhov 	return ((union device_attr_group_devres *)res)->group == data;
130757b8ff07SDmitry Torokhov }
130857b8ff07SDmitry Torokhov 
130957b8ff07SDmitry Torokhov static void devm_attr_group_remove(struct device *dev, void *res)
131057b8ff07SDmitry Torokhov {
131157b8ff07SDmitry Torokhov 	union device_attr_group_devres *devres = res;
131257b8ff07SDmitry Torokhov 	const struct attribute_group *group = devres->group;
131357b8ff07SDmitry Torokhov 
131457b8ff07SDmitry Torokhov 	dev_dbg(dev, "%s: removing group %p\n", __func__, group);
131557b8ff07SDmitry Torokhov 	sysfs_remove_group(&dev->kobj, group);
131657b8ff07SDmitry Torokhov }
131757b8ff07SDmitry Torokhov 
131857b8ff07SDmitry Torokhov static void devm_attr_groups_remove(struct device *dev, void *res)
131957b8ff07SDmitry Torokhov {
132057b8ff07SDmitry Torokhov 	union device_attr_group_devres *devres = res;
132157b8ff07SDmitry Torokhov 	const struct attribute_group **groups = devres->groups;
132257b8ff07SDmitry Torokhov 
132357b8ff07SDmitry Torokhov 	dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
132457b8ff07SDmitry Torokhov 	sysfs_remove_groups(&dev->kobj, groups);
132557b8ff07SDmitry Torokhov }
132657b8ff07SDmitry Torokhov 
132757b8ff07SDmitry Torokhov /**
132857b8ff07SDmitry Torokhov  * devm_device_add_group - given a device, create a managed attribute group
132957b8ff07SDmitry Torokhov  * @dev:	The device to create the group for
133057b8ff07SDmitry Torokhov  * @grp:	The attribute group to create
133157b8ff07SDmitry Torokhov  *
133257b8ff07SDmitry Torokhov  * This function creates a group for the first time.  It will explicitly
133357b8ff07SDmitry Torokhov  * warn and error if any of the attribute files being created already exist.
133457b8ff07SDmitry Torokhov  *
133557b8ff07SDmitry Torokhov  * Returns 0 on success or error code on failure.
133657b8ff07SDmitry Torokhov  */
133757b8ff07SDmitry Torokhov int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
133857b8ff07SDmitry Torokhov {
133957b8ff07SDmitry Torokhov 	union device_attr_group_devres *devres;
134057b8ff07SDmitry Torokhov 	int error;
134157b8ff07SDmitry Torokhov 
134257b8ff07SDmitry Torokhov 	devres = devres_alloc(devm_attr_group_remove,
134357b8ff07SDmitry Torokhov 			      sizeof(*devres), GFP_KERNEL);
134457b8ff07SDmitry Torokhov 	if (!devres)
134557b8ff07SDmitry Torokhov 		return -ENOMEM;
134657b8ff07SDmitry Torokhov 
134757b8ff07SDmitry Torokhov 	error = sysfs_create_group(&dev->kobj, grp);
134857b8ff07SDmitry Torokhov 	if (error) {
134957b8ff07SDmitry Torokhov 		devres_free(devres);
135057b8ff07SDmitry Torokhov 		return error;
135157b8ff07SDmitry Torokhov 	}
135257b8ff07SDmitry Torokhov 
135357b8ff07SDmitry Torokhov 	devres->group = grp;
135457b8ff07SDmitry Torokhov 	devres_add(dev, devres);
135557b8ff07SDmitry Torokhov 	return 0;
135657b8ff07SDmitry Torokhov }
135757b8ff07SDmitry Torokhov EXPORT_SYMBOL_GPL(devm_device_add_group);
135857b8ff07SDmitry Torokhov 
135957b8ff07SDmitry Torokhov /**
136057b8ff07SDmitry Torokhov  * devm_device_remove_group: remove a managed group from a device
136157b8ff07SDmitry Torokhov  * @dev:	device to remove the group from
136257b8ff07SDmitry Torokhov  * @grp:	group to remove
136357b8ff07SDmitry Torokhov  *
136457b8ff07SDmitry Torokhov  * This function removes a group of attributes from a device. The attributes
136557b8ff07SDmitry Torokhov  * previously have to have been created for this group, otherwise it will fail.
136657b8ff07SDmitry Torokhov  */
136757b8ff07SDmitry Torokhov void devm_device_remove_group(struct device *dev,
136857b8ff07SDmitry Torokhov 			      const struct attribute_group *grp)
136957b8ff07SDmitry Torokhov {
137057b8ff07SDmitry Torokhov 	WARN_ON(devres_release(dev, devm_attr_group_remove,
137157b8ff07SDmitry Torokhov 			       devm_attr_group_match,
137257b8ff07SDmitry Torokhov 			       /* cast away const */ (void *)grp));
137357b8ff07SDmitry Torokhov }
137457b8ff07SDmitry Torokhov EXPORT_SYMBOL_GPL(devm_device_remove_group);
137557b8ff07SDmitry Torokhov 
137657b8ff07SDmitry Torokhov /**
137757b8ff07SDmitry Torokhov  * devm_device_add_groups - create a bunch of managed attribute groups
137857b8ff07SDmitry Torokhov  * @dev:	The device to create the group for
137957b8ff07SDmitry Torokhov  * @groups:	The attribute groups to create, NULL terminated
138057b8ff07SDmitry Torokhov  *
138157b8ff07SDmitry Torokhov  * This function creates a bunch of managed attribute groups.  If an error
138257b8ff07SDmitry Torokhov  * occurs when creating a group, all previously created groups will be
138357b8ff07SDmitry Torokhov  * removed, unwinding everything back to the original state when this
138457b8ff07SDmitry Torokhov  * function was called.  It will explicitly warn and error if any of the
138557b8ff07SDmitry Torokhov  * attribute files being created already exist.
138657b8ff07SDmitry Torokhov  *
138757b8ff07SDmitry Torokhov  * Returns 0 on success or error code from sysfs_create_group on failure.
138857b8ff07SDmitry Torokhov  */
138957b8ff07SDmitry Torokhov int devm_device_add_groups(struct device *dev,
139057b8ff07SDmitry Torokhov 			   const struct attribute_group **groups)
139157b8ff07SDmitry Torokhov {
139257b8ff07SDmitry Torokhov 	union device_attr_group_devres *devres;
139357b8ff07SDmitry Torokhov 	int error;
139457b8ff07SDmitry Torokhov 
139557b8ff07SDmitry Torokhov 	devres = devres_alloc(devm_attr_groups_remove,
139657b8ff07SDmitry Torokhov 			      sizeof(*devres), GFP_KERNEL);
139757b8ff07SDmitry Torokhov 	if (!devres)
139857b8ff07SDmitry Torokhov 		return -ENOMEM;
139957b8ff07SDmitry Torokhov 
140057b8ff07SDmitry Torokhov 	error = sysfs_create_groups(&dev->kobj, groups);
140157b8ff07SDmitry Torokhov 	if (error) {
140257b8ff07SDmitry Torokhov 		devres_free(devres);
140357b8ff07SDmitry Torokhov 		return error;
140457b8ff07SDmitry Torokhov 	}
140557b8ff07SDmitry Torokhov 
140657b8ff07SDmitry Torokhov 	devres->groups = groups;
140757b8ff07SDmitry Torokhov 	devres_add(dev, devres);
140857b8ff07SDmitry Torokhov 	return 0;
140957b8ff07SDmitry Torokhov }
141057b8ff07SDmitry Torokhov EXPORT_SYMBOL_GPL(devm_device_add_groups);
141157b8ff07SDmitry Torokhov 
141257b8ff07SDmitry Torokhov /**
141357b8ff07SDmitry Torokhov  * devm_device_remove_groups - remove a list of managed groups
141457b8ff07SDmitry Torokhov  *
141557b8ff07SDmitry Torokhov  * @dev:	The device for the groups to be removed from
141657b8ff07SDmitry Torokhov  * @groups:	NULL terminated list of groups to be removed
141757b8ff07SDmitry Torokhov  *
141857b8ff07SDmitry Torokhov  * If groups is not NULL, remove the specified groups from the device.
141957b8ff07SDmitry Torokhov  */
142057b8ff07SDmitry Torokhov void devm_device_remove_groups(struct device *dev,
142157b8ff07SDmitry Torokhov 			       const struct attribute_group **groups)
142257b8ff07SDmitry Torokhov {
142357b8ff07SDmitry Torokhov 	WARN_ON(devres_release(dev, devm_attr_groups_remove,
142457b8ff07SDmitry Torokhov 			       devm_attr_group_match,
142557b8ff07SDmitry Torokhov 			       /* cast away const */ (void *)groups));
142657b8ff07SDmitry Torokhov }
142757b8ff07SDmitry Torokhov EXPORT_SYMBOL_GPL(devm_device_remove_groups);
14281da177e4SLinus Torvalds 
14292620efefSGreg Kroah-Hartman static int device_add_attrs(struct device *dev)
14302620efefSGreg Kroah-Hartman {
14312620efefSGreg Kroah-Hartman 	struct class *class = dev->class;
1432aed65af1SStephen Hemminger 	const struct device_type *type = dev->type;
1433621a1672SDmitry Torokhov 	int error;
14342620efefSGreg Kroah-Hartman 
1435621a1672SDmitry Torokhov 	if (class) {
1436d05a6f96SGreg Kroah-Hartman 		error = device_add_groups(dev, class->dev_groups);
14372620efefSGreg Kroah-Hartman 		if (error)
1438621a1672SDmitry Torokhov 			return error;
1439f9f852dfSKay Sievers 	}
1440f9f852dfSKay Sievers 
1441621a1672SDmitry Torokhov 	if (type) {
1442621a1672SDmitry Torokhov 		error = device_add_groups(dev, type->groups);
1443f9f852dfSKay Sievers 		if (error)
1444a6b01dedSGreg Kroah-Hartman 			goto err_remove_class_groups;
1445f9f852dfSKay Sievers 	}
1446621a1672SDmitry Torokhov 
1447621a1672SDmitry Torokhov 	error = device_add_groups(dev, dev->groups);
1448f9f852dfSKay Sievers 	if (error)
1449621a1672SDmitry Torokhov 		goto err_remove_type_groups;
1450621a1672SDmitry Torokhov 
14514f3549d7SRafael J. Wysocki 	if (device_supports_offline(dev) && !dev->offline_disabled) {
1452c5e064a6SGreg Kroah-Hartman 		error = device_create_file(dev, &dev_attr_online);
14534f3549d7SRafael J. Wysocki 		if (error)
1454ecfbf6fdSRafael J. Wysocki 			goto err_remove_dev_groups;
14554f3549d7SRafael J. Wysocki 	}
14564f3549d7SRafael J. Wysocki 
1457621a1672SDmitry Torokhov 	return 0;
1458621a1672SDmitry Torokhov 
1459ecfbf6fdSRafael J. Wysocki  err_remove_dev_groups:
1460ecfbf6fdSRafael J. Wysocki 	device_remove_groups(dev, dev->groups);
1461621a1672SDmitry Torokhov  err_remove_type_groups:
1462621a1672SDmitry Torokhov 	if (type)
1463621a1672SDmitry Torokhov 		device_remove_groups(dev, type->groups);
1464d05a6f96SGreg Kroah-Hartman  err_remove_class_groups:
1465d05a6f96SGreg Kroah-Hartman 	if (class)
1466d05a6f96SGreg Kroah-Hartman 		device_remove_groups(dev, class->dev_groups);
1467f9f852dfSKay Sievers 
14682620efefSGreg Kroah-Hartman 	return error;
14692620efefSGreg Kroah-Hartman }
14702620efefSGreg Kroah-Hartman 
14712620efefSGreg Kroah-Hartman static void device_remove_attrs(struct device *dev)
14722620efefSGreg Kroah-Hartman {
14732620efefSGreg Kroah-Hartman 	struct class *class = dev->class;
1474aed65af1SStephen Hemminger 	const struct device_type *type = dev->type;
14752620efefSGreg Kroah-Hartman 
1476c5e064a6SGreg Kroah-Hartman 	device_remove_file(dev, &dev_attr_online);
1477621a1672SDmitry Torokhov 	device_remove_groups(dev, dev->groups);
1478f9f852dfSKay Sievers 
1479621a1672SDmitry Torokhov 	if (type)
1480621a1672SDmitry Torokhov 		device_remove_groups(dev, type->groups);
1481621a1672SDmitry Torokhov 
1482a6b01dedSGreg Kroah-Hartman 	if (class)
1483d05a6f96SGreg Kroah-Hartman 		device_remove_groups(dev, class->dev_groups);
1484c97415a7SStefan Achatz }
14852620efefSGreg Kroah-Hartman 
1486c5e064a6SGreg Kroah-Hartman static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
148723681e47SGreg Kroah-Hartman 			char *buf)
148823681e47SGreg Kroah-Hartman {
148923681e47SGreg Kroah-Hartman 	return print_dev_t(buf, dev->devt);
149023681e47SGreg Kroah-Hartman }
1491c5e064a6SGreg Kroah-Hartman static DEVICE_ATTR_RO(dev);
1492ad6a1e1cSTejun Heo 
1493ca22e56dSKay Sievers /* /sys/devices/ */
1494881c6cfdSGreg Kroah-Hartman struct kset *devices_kset;
14951da177e4SLinus Torvalds 
14961da177e4SLinus Torvalds /**
149752cdbdd4SGrygorii Strashko  * devices_kset_move_before - Move device in the devices_kset's list.
149852cdbdd4SGrygorii Strashko  * @deva: Device to move.
149952cdbdd4SGrygorii Strashko  * @devb: Device @deva should come before.
150052cdbdd4SGrygorii Strashko  */
150152cdbdd4SGrygorii Strashko static void devices_kset_move_before(struct device *deva, struct device *devb)
150252cdbdd4SGrygorii Strashko {
150352cdbdd4SGrygorii Strashko 	if (!devices_kset)
150452cdbdd4SGrygorii Strashko 		return;
150552cdbdd4SGrygorii Strashko 	pr_debug("devices_kset: Moving %s before %s\n",
150652cdbdd4SGrygorii Strashko 		 dev_name(deva), dev_name(devb));
150752cdbdd4SGrygorii Strashko 	spin_lock(&devices_kset->list_lock);
150852cdbdd4SGrygorii Strashko 	list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
150952cdbdd4SGrygorii Strashko 	spin_unlock(&devices_kset->list_lock);
151052cdbdd4SGrygorii Strashko }
151152cdbdd4SGrygorii Strashko 
151252cdbdd4SGrygorii Strashko /**
151352cdbdd4SGrygorii Strashko  * devices_kset_move_after - Move device in the devices_kset's list.
151452cdbdd4SGrygorii Strashko  * @deva: Device to move
151552cdbdd4SGrygorii Strashko  * @devb: Device @deva should come after.
151652cdbdd4SGrygorii Strashko  */
151752cdbdd4SGrygorii Strashko static void devices_kset_move_after(struct device *deva, struct device *devb)
151852cdbdd4SGrygorii Strashko {
151952cdbdd4SGrygorii Strashko 	if (!devices_kset)
152052cdbdd4SGrygorii Strashko 		return;
152152cdbdd4SGrygorii Strashko 	pr_debug("devices_kset: Moving %s after %s\n",
152252cdbdd4SGrygorii Strashko 		 dev_name(deva), dev_name(devb));
152352cdbdd4SGrygorii Strashko 	spin_lock(&devices_kset->list_lock);
152452cdbdd4SGrygorii Strashko 	list_move(&deva->kobj.entry, &devb->kobj.entry);
152552cdbdd4SGrygorii Strashko 	spin_unlock(&devices_kset->list_lock);
152652cdbdd4SGrygorii Strashko }
152752cdbdd4SGrygorii Strashko 
152852cdbdd4SGrygorii Strashko /**
152952cdbdd4SGrygorii Strashko  * devices_kset_move_last - move the device to the end of devices_kset's list.
153052cdbdd4SGrygorii Strashko  * @dev: device to move
153152cdbdd4SGrygorii Strashko  */
153252cdbdd4SGrygorii Strashko void devices_kset_move_last(struct device *dev)
153352cdbdd4SGrygorii Strashko {
153452cdbdd4SGrygorii Strashko 	if (!devices_kset)
153552cdbdd4SGrygorii Strashko 		return;
153652cdbdd4SGrygorii Strashko 	pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
153752cdbdd4SGrygorii Strashko 	spin_lock(&devices_kset->list_lock);
153852cdbdd4SGrygorii Strashko 	list_move_tail(&dev->kobj.entry, &devices_kset->list);
153952cdbdd4SGrygorii Strashko 	spin_unlock(&devices_kset->list_lock);
154052cdbdd4SGrygorii Strashko }
154152cdbdd4SGrygorii Strashko 
154252cdbdd4SGrygorii Strashko /**
15431da177e4SLinus Torvalds  * device_create_file - create sysfs attribute file for device.
15441da177e4SLinus Torvalds  * @dev: device.
15451da177e4SLinus Torvalds  * @attr: device attribute descriptor.
15461da177e4SLinus Torvalds  */
154726579ab7SPhil Carmody int device_create_file(struct device *dev,
154826579ab7SPhil Carmody 		       const struct device_attribute *attr)
15491da177e4SLinus Torvalds {
15501da177e4SLinus Torvalds 	int error = 0;
15518f46baaaSFelipe Balbi 
15528f46baaaSFelipe Balbi 	if (dev) {
15538f46baaaSFelipe Balbi 		WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
155497521978Sdyoung@redhat.com 			"Attribute %s: write permission without 'store'\n",
155597521978Sdyoung@redhat.com 			attr->attr.name);
15568f46baaaSFelipe Balbi 		WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
155797521978Sdyoung@redhat.com 			"Attribute %s: read permission without 'show'\n",
155897521978Sdyoung@redhat.com 			attr->attr.name);
15591da177e4SLinus Torvalds 		error = sysfs_create_file(&dev->kobj, &attr->attr);
15608f46baaaSFelipe Balbi 	}
15618f46baaaSFelipe Balbi 
15621da177e4SLinus Torvalds 	return error;
15631da177e4SLinus Torvalds }
156486df2687SDavid Graham White EXPORT_SYMBOL_GPL(device_create_file);
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds /**
15671da177e4SLinus Torvalds  * device_remove_file - remove sysfs attribute file.
15681da177e4SLinus Torvalds  * @dev: device.
15691da177e4SLinus Torvalds  * @attr: device attribute descriptor.
15701da177e4SLinus Torvalds  */
157126579ab7SPhil Carmody void device_remove_file(struct device *dev,
157226579ab7SPhil Carmody 			const struct device_attribute *attr)
15731da177e4SLinus Torvalds {
15740c98b19fSCornelia Huck 	if (dev)
15751da177e4SLinus Torvalds 		sysfs_remove_file(&dev->kobj, &attr->attr);
15761da177e4SLinus Torvalds }
157786df2687SDavid Graham White EXPORT_SYMBOL_GPL(device_remove_file);
15781da177e4SLinus Torvalds 
15792589f188SGreg Kroah-Hartman /**
15806b0afc2aSTejun Heo  * device_remove_file_self - remove sysfs attribute file from its own method.
15816b0afc2aSTejun Heo  * @dev: device.
15826b0afc2aSTejun Heo  * @attr: device attribute descriptor.
15836b0afc2aSTejun Heo  *
15846b0afc2aSTejun Heo  * See kernfs_remove_self() for details.
15856b0afc2aSTejun Heo  */
15866b0afc2aSTejun Heo bool device_remove_file_self(struct device *dev,
15876b0afc2aSTejun Heo 			     const struct device_attribute *attr)
15886b0afc2aSTejun Heo {
15896b0afc2aSTejun Heo 	if (dev)
15906b0afc2aSTejun Heo 		return sysfs_remove_file_self(&dev->kobj, &attr->attr);
15916b0afc2aSTejun Heo 	else
15926b0afc2aSTejun Heo 		return false;
15936b0afc2aSTejun Heo }
15946b0afc2aSTejun Heo EXPORT_SYMBOL_GPL(device_remove_file_self);
15956b0afc2aSTejun Heo 
15966b0afc2aSTejun Heo /**
15972589f188SGreg Kroah-Hartman  * device_create_bin_file - create sysfs binary attribute file for device.
15982589f188SGreg Kroah-Hartman  * @dev: device.
15992589f188SGreg Kroah-Hartman  * @attr: device binary attribute descriptor.
16002589f188SGreg Kroah-Hartman  */
160166ecb92bSPhil Carmody int device_create_bin_file(struct device *dev,
160266ecb92bSPhil Carmody 			   const struct bin_attribute *attr)
16032589f188SGreg Kroah-Hartman {
16042589f188SGreg Kroah-Hartman 	int error = -EINVAL;
16052589f188SGreg Kroah-Hartman 	if (dev)
16062589f188SGreg Kroah-Hartman 		error = sysfs_create_bin_file(&dev->kobj, attr);
16072589f188SGreg Kroah-Hartman 	return error;
16082589f188SGreg Kroah-Hartman }
16092589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_bin_file);
16102589f188SGreg Kroah-Hartman 
16112589f188SGreg Kroah-Hartman /**
16122589f188SGreg Kroah-Hartman  * device_remove_bin_file - remove sysfs binary attribute file
16132589f188SGreg Kroah-Hartman  * @dev: device.
16142589f188SGreg Kroah-Hartman  * @attr: device binary attribute descriptor.
16152589f188SGreg Kroah-Hartman  */
161666ecb92bSPhil Carmody void device_remove_bin_file(struct device *dev,
161766ecb92bSPhil Carmody 			    const struct bin_attribute *attr)
16182589f188SGreg Kroah-Hartman {
16192589f188SGreg Kroah-Hartman 	if (dev)
16202589f188SGreg Kroah-Hartman 		sysfs_remove_bin_file(&dev->kobj, attr);
16212589f188SGreg Kroah-Hartman }
16222589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_remove_bin_file);
16232589f188SGreg Kroah-Hartman 
162434bb61f9SJames Bottomley static void klist_children_get(struct klist_node *n)
162534bb61f9SJames Bottomley {
1626f791b8c8SGreg Kroah-Hartman 	struct device_private *p = to_device_private_parent(n);
1627f791b8c8SGreg Kroah-Hartman 	struct device *dev = p->device;
162834bb61f9SJames Bottomley 
162934bb61f9SJames Bottomley 	get_device(dev);
163034bb61f9SJames Bottomley }
163134bb61f9SJames Bottomley 
163234bb61f9SJames Bottomley static void klist_children_put(struct klist_node *n)
163334bb61f9SJames Bottomley {
1634f791b8c8SGreg Kroah-Hartman 	struct device_private *p = to_device_private_parent(n);
1635f791b8c8SGreg Kroah-Hartman 	struct device *dev = p->device;
163634bb61f9SJames Bottomley 
163734bb61f9SJames Bottomley 	put_device(dev);
163834bb61f9SJames Bottomley }
163934bb61f9SJames Bottomley 
16401da177e4SLinus Torvalds /**
16411da177e4SLinus Torvalds  * device_initialize - init device structure.
16421da177e4SLinus Torvalds  * @dev: device.
16431da177e4SLinus Torvalds  *
16445739411aSCornelia Huck  * This prepares the device for use by other layers by initializing
16455739411aSCornelia Huck  * its fields.
16461da177e4SLinus Torvalds  * It is the first half of device_register(), if called by
16475739411aSCornelia Huck  * that function, though it can also be called separately, so one
16485739411aSCornelia Huck  * may use @dev's fields. In particular, get_device()/put_device()
16495739411aSCornelia Huck  * may be used for reference counting of @dev after calling this
16505739411aSCornelia Huck  * function.
16515739411aSCornelia Huck  *
1652b10d5efdSAlan Stern  * All fields in @dev must be initialized by the caller to 0, except
1653b10d5efdSAlan Stern  * for those explicitly set to some other value.  The simplest
1654b10d5efdSAlan Stern  * approach is to use kzalloc() to allocate the structure containing
1655b10d5efdSAlan Stern  * @dev.
1656b10d5efdSAlan Stern  *
16575739411aSCornelia Huck  * NOTE: Use put_device() to give up your reference instead of freeing
16585739411aSCornelia Huck  * @dev directly once you have called this function.
16591da177e4SLinus Torvalds  */
16601da177e4SLinus Torvalds void device_initialize(struct device *dev)
16611da177e4SLinus Torvalds {
1662881c6cfdSGreg Kroah-Hartman 	dev->kobj.kset = devices_kset;
1663f9cb074bSGreg Kroah-Hartman 	kobject_init(&dev->kobj, &device_ktype);
16641da177e4SLinus Torvalds 	INIT_LIST_HEAD(&dev->dma_pools);
16653142788bSThomas Gleixner 	mutex_init(&dev->mutex);
16661704f47bSPeter Zijlstra 	lockdep_set_novalidate_class(&dev->mutex);
16679ac7849eSTejun Heo 	spin_lock_init(&dev->devres_lock);
16689ac7849eSTejun Heo 	INIT_LIST_HEAD(&dev->devres_head);
16693b98aeafSAlan Stern 	device_pm_init(dev);
167087348136SChristoph Hellwig 	set_dev_node(dev, -1);
16714a7cc831SJiang Liu #ifdef CONFIG_GENERIC_MSI_IRQ
16724a7cc831SJiang Liu 	INIT_LIST_HEAD(&dev->msi_list);
16734a7cc831SJiang Liu #endif
16749ed98953SRafael J. Wysocki 	INIT_LIST_HEAD(&dev->links.consumers);
16759ed98953SRafael J. Wysocki 	INIT_LIST_HEAD(&dev->links.suppliers);
16769ed98953SRafael J. Wysocki 	dev->links.status = DL_DEV_NO_DRIVER;
16771da177e4SLinus Torvalds }
167886df2687SDavid Graham White EXPORT_SYMBOL_GPL(device_initialize);
16791da177e4SLinus Torvalds 
1680d73ce004STejun Heo struct kobject *virtual_device_parent(struct device *dev)
1681f0ee61a6SGreg Kroah-Hartman {
1682f0ee61a6SGreg Kroah-Hartman 	static struct kobject *virtual_dir = NULL;
1683f0ee61a6SGreg Kroah-Hartman 
1684f0ee61a6SGreg Kroah-Hartman 	if (!virtual_dir)
16854ff6abffSGreg Kroah-Hartman 		virtual_dir = kobject_create_and_add("virtual",
1686881c6cfdSGreg Kroah-Hartman 						     &devices_kset->kobj);
1687f0ee61a6SGreg Kroah-Hartman 
168886406245SKay Sievers 	return virtual_dir;
1689f0ee61a6SGreg Kroah-Hartman }
1690f0ee61a6SGreg Kroah-Hartman 
1691bc451f20SEric W. Biederman struct class_dir {
1692bc451f20SEric W. Biederman 	struct kobject kobj;
1693bc451f20SEric W. Biederman 	struct class *class;
1694bc451f20SEric W. Biederman };
1695bc451f20SEric W. Biederman 
1696bc451f20SEric W. Biederman #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1697bc451f20SEric W. Biederman 
1698bc451f20SEric W. Biederman static void class_dir_release(struct kobject *kobj)
1699bc451f20SEric W. Biederman {
1700bc451f20SEric W. Biederman 	struct class_dir *dir = to_class_dir(kobj);
1701bc451f20SEric W. Biederman 	kfree(dir);
1702bc451f20SEric W. Biederman }
1703bc451f20SEric W. Biederman 
1704bc451f20SEric W. Biederman static const
1705bc451f20SEric W. Biederman struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
1706bc451f20SEric W. Biederman {
1707bc451f20SEric W. Biederman 	struct class_dir *dir = to_class_dir(kobj);
1708bc451f20SEric W. Biederman 	return dir->class->ns_type;
1709bc451f20SEric W. Biederman }
1710bc451f20SEric W. Biederman 
1711bc451f20SEric W. Biederman static struct kobj_type class_dir_ktype = {
1712bc451f20SEric W. Biederman 	.release	= class_dir_release,
1713bc451f20SEric W. Biederman 	.sysfs_ops	= &kobj_sysfs_ops,
1714bc451f20SEric W. Biederman 	.child_ns_type	= class_dir_child_ns_type
1715bc451f20SEric W. Biederman };
1716bc451f20SEric W. Biederman 
1717bc451f20SEric W. Biederman static struct kobject *
1718bc451f20SEric W. Biederman class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
1719bc451f20SEric W. Biederman {
1720bc451f20SEric W. Biederman 	struct class_dir *dir;
1721bc451f20SEric W. Biederman 	int retval;
1722bc451f20SEric W. Biederman 
1723bc451f20SEric W. Biederman 	dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1724bc451f20SEric W. Biederman 	if (!dir)
172584d0c27dSTetsuo Handa 		return ERR_PTR(-ENOMEM);
1726bc451f20SEric W. Biederman 
1727bc451f20SEric W. Biederman 	dir->class = class;
1728bc451f20SEric W. Biederman 	kobject_init(&dir->kobj, &class_dir_ktype);
1729bc451f20SEric W. Biederman 
17306b6e39a6SKay Sievers 	dir->kobj.kset = &class->p->glue_dirs;
1731bc451f20SEric W. Biederman 
1732bc451f20SEric W. Biederman 	retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
1733bc451f20SEric W. Biederman 	if (retval < 0) {
1734bc451f20SEric W. Biederman 		kobject_put(&dir->kobj);
173584d0c27dSTetsuo Handa 		return ERR_PTR(retval);
1736bc451f20SEric W. Biederman 	}
1737bc451f20SEric W. Biederman 	return &dir->kobj;
1738bc451f20SEric W. Biederman }
1739bc451f20SEric W. Biederman 
1740e4a60d13SYijing Wang static DEFINE_MUTEX(gdp_mutex);
1741bc451f20SEric W. Biederman 
1742c744aeaeSCornelia Huck static struct kobject *get_device_parent(struct device *dev,
1743c744aeaeSCornelia Huck 					 struct device *parent)
174440fa5422SGreg Kroah-Hartman {
174586406245SKay Sievers 	if (dev->class) {
174686406245SKay Sievers 		struct kobject *kobj = NULL;
174786406245SKay Sievers 		struct kobject *parent_kobj;
174886406245SKay Sievers 		struct kobject *k;
174986406245SKay Sievers 
1750ead454feSRandy Dunlap #ifdef CONFIG_BLOCK
175139aba963SKay Sievers 		/* block disks show up in /sys/block */
1752e52eec13SAndi Kleen 		if (sysfs_deprecated && dev->class == &block_class) {
175339aba963SKay Sievers 			if (parent && parent->class == &block_class)
175439aba963SKay Sievers 				return &parent->kobj;
17556b6e39a6SKay Sievers 			return &block_class.p->subsys.kobj;
175639aba963SKay Sievers 		}
1757ead454feSRandy Dunlap #endif
1758e52eec13SAndi Kleen 
175986406245SKay Sievers 		/*
176086406245SKay Sievers 		 * If we have no parent, we live in "virtual".
17610f4dafc0SKay Sievers 		 * Class-devices with a non class-device as parent, live
17620f4dafc0SKay Sievers 		 * in a "glue" directory to prevent namespace collisions.
176386406245SKay Sievers 		 */
176486406245SKay Sievers 		if (parent == NULL)
176586406245SKay Sievers 			parent_kobj = virtual_device_parent(dev);
176624b1442dSEric W. Biederman 		else if (parent->class && !dev->class->ns_type)
176786406245SKay Sievers 			return &parent->kobj;
176886406245SKay Sievers 		else
176986406245SKay Sievers 			parent_kobj = &parent->kobj;
177086406245SKay Sievers 
177177d3d7c1STejun Heo 		mutex_lock(&gdp_mutex);
177277d3d7c1STejun Heo 
177386406245SKay Sievers 		/* find our class-directory at the parent and reference it */
17746b6e39a6SKay Sievers 		spin_lock(&dev->class->p->glue_dirs.list_lock);
17756b6e39a6SKay Sievers 		list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
177686406245SKay Sievers 			if (k->parent == parent_kobj) {
177786406245SKay Sievers 				kobj = kobject_get(k);
177886406245SKay Sievers 				break;
177986406245SKay Sievers 			}
17806b6e39a6SKay Sievers 		spin_unlock(&dev->class->p->glue_dirs.list_lock);
178177d3d7c1STejun Heo 		if (kobj) {
178277d3d7c1STejun Heo 			mutex_unlock(&gdp_mutex);
178386406245SKay Sievers 			return kobj;
178477d3d7c1STejun Heo 		}
178586406245SKay Sievers 
178686406245SKay Sievers 		/* or create a new class-directory at the parent device */
1787bc451f20SEric W. Biederman 		k = class_dir_create_and_add(dev->class, parent_kobj);
17880f4dafc0SKay Sievers 		/* do not emit an uevent for this simple "glue" directory */
178977d3d7c1STejun Heo 		mutex_unlock(&gdp_mutex);
179043968d2fSGreg Kroah-Hartman 		return k;
179186406245SKay Sievers 	}
179286406245SKay Sievers 
1793ca22e56dSKay Sievers 	/* subsystems can specify a default root directory for their devices */
1794ca22e56dSKay Sievers 	if (!parent && dev->bus && dev->bus->dev_root)
1795ca22e56dSKay Sievers 		return &dev->bus->dev_root->kobj;
1796ca22e56dSKay Sievers 
179786406245SKay Sievers 	if (parent)
1798c744aeaeSCornelia Huck 		return &parent->kobj;
1799c744aeaeSCornelia Huck 	return NULL;
1800c744aeaeSCornelia Huck }
1801da231fd5SKay Sievers 
1802cebf8fd1SMing Lei static inline bool live_in_glue_dir(struct kobject *kobj,
1803cebf8fd1SMing Lei 				    struct device *dev)
1804cebf8fd1SMing Lei {
1805cebf8fd1SMing Lei 	if (!kobj || !dev->class ||
1806cebf8fd1SMing Lei 	    kobj->kset != &dev->class->p->glue_dirs)
1807cebf8fd1SMing Lei 		return false;
1808cebf8fd1SMing Lei 	return true;
1809cebf8fd1SMing Lei }
1810cebf8fd1SMing Lei 
1811cebf8fd1SMing Lei static inline struct kobject *get_glue_dir(struct device *dev)
1812cebf8fd1SMing Lei {
1813cebf8fd1SMing Lei 	return dev->kobj.parent;
1814cebf8fd1SMing Lei }
1815cebf8fd1SMing Lei 
1816cebf8fd1SMing Lei /*
1817cebf8fd1SMing Lei  * make sure cleaning up dir as the last step, we need to make
1818cebf8fd1SMing Lei  * sure .release handler of kobject is run with holding the
1819cebf8fd1SMing Lei  * global lock
1820cebf8fd1SMing Lei  */
182163b6971aSCornelia Huck static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
1822da231fd5SKay Sievers {
18230f4dafc0SKay Sievers 	/* see if we live in a "glue" directory */
1824cebf8fd1SMing Lei 	if (!live_in_glue_dir(glue_dir, dev))
1825da231fd5SKay Sievers 		return;
1826da231fd5SKay Sievers 
1827e4a60d13SYijing Wang 	mutex_lock(&gdp_mutex);
1828726e4109SBenjamin Herrenschmidt 	if (!kobject_has_children(glue_dir))
1829726e4109SBenjamin Herrenschmidt 		kobject_del(glue_dir);
18300f4dafc0SKay Sievers 	kobject_put(glue_dir);
1831e4a60d13SYijing Wang 	mutex_unlock(&gdp_mutex);
1832da231fd5SKay Sievers }
183363b6971aSCornelia Huck 
18342ee97cafSCornelia Huck static int device_add_class_symlinks(struct device *dev)
18352ee97cafSCornelia Huck {
18365590f319SBenjamin Herrenschmidt 	struct device_node *of_node = dev_of_node(dev);
18372ee97cafSCornelia Huck 	int error;
18382ee97cafSCornelia Huck 
18395590f319SBenjamin Herrenschmidt 	if (of_node) {
18400c3c234bSRob Herring 		error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
18415590f319SBenjamin Herrenschmidt 		if (error)
18425590f319SBenjamin Herrenschmidt 			dev_warn(dev, "Error %d creating of_node link\n",error);
18435590f319SBenjamin Herrenschmidt 		/* An error here doesn't warrant bringing down the device */
18445590f319SBenjamin Herrenschmidt 	}
18455590f319SBenjamin Herrenschmidt 
18462ee97cafSCornelia Huck 	if (!dev->class)
18472ee97cafSCornelia Huck 		return 0;
1848da231fd5SKay Sievers 
18491fbfee6cSGreg Kroah-Hartman 	error = sysfs_create_link(&dev->kobj,
18506b6e39a6SKay Sievers 				  &dev->class->p->subsys.kobj,
18512ee97cafSCornelia Huck 				  "subsystem");
18522ee97cafSCornelia Huck 	if (error)
18535590f319SBenjamin Herrenschmidt 		goto out_devnode;
1854da231fd5SKay Sievers 
18554e886c29SGreg Kroah-Hartman 	if (dev->parent && device_is_not_partition(dev)) {
18564f01a757SDmitry Torokhov 		error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
18574f01a757SDmitry Torokhov 					  "device");
18584f01a757SDmitry Torokhov 		if (error)
185939aba963SKay Sievers 			goto out_subsys;
18602ee97cafSCornelia Huck 	}
186139aba963SKay Sievers 
1862ead454feSRandy Dunlap #ifdef CONFIG_BLOCK
186339aba963SKay Sievers 	/* /sys/block has directories and does not need symlinks */
1864e52eec13SAndi Kleen 	if (sysfs_deprecated && dev->class == &block_class)
186539aba963SKay Sievers 		return 0;
1866ead454feSRandy Dunlap #endif
186739aba963SKay Sievers 
186839aba963SKay Sievers 	/* link in the class directory pointing to the device */
18696b6e39a6SKay Sievers 	error = sysfs_create_link(&dev->class->p->subsys.kobj,
187039aba963SKay Sievers 				  &dev->kobj, dev_name(dev));
187139aba963SKay Sievers 	if (error)
187239aba963SKay Sievers 		goto out_device;
187339aba963SKay Sievers 
18742ee97cafSCornelia Huck 	return 0;
18752ee97cafSCornelia Huck 
187639aba963SKay Sievers out_device:
187739aba963SKay Sievers 	sysfs_remove_link(&dev->kobj, "device");
1878da231fd5SKay Sievers 
18792ee97cafSCornelia Huck out_subsys:
18802ee97cafSCornelia Huck 	sysfs_remove_link(&dev->kobj, "subsystem");
18815590f319SBenjamin Herrenschmidt out_devnode:
18825590f319SBenjamin Herrenschmidt 	sysfs_remove_link(&dev->kobj, "of_node");
18832ee97cafSCornelia Huck 	return error;
18842ee97cafSCornelia Huck }
18852ee97cafSCornelia Huck 
18862ee97cafSCornelia Huck static void device_remove_class_symlinks(struct device *dev)
18872ee97cafSCornelia Huck {
18885590f319SBenjamin Herrenschmidt 	if (dev_of_node(dev))
18895590f319SBenjamin Herrenschmidt 		sysfs_remove_link(&dev->kobj, "of_node");
18905590f319SBenjamin Herrenschmidt 
18912ee97cafSCornelia Huck 	if (!dev->class)
18922ee97cafSCornelia Huck 		return;
1893da231fd5SKay Sievers 
18944e886c29SGreg Kroah-Hartman 	if (dev->parent && device_is_not_partition(dev))
1895da231fd5SKay Sievers 		sysfs_remove_link(&dev->kobj, "device");
18962ee97cafSCornelia Huck 	sysfs_remove_link(&dev->kobj, "subsystem");
1897ead454feSRandy Dunlap #ifdef CONFIG_BLOCK
1898e52eec13SAndi Kleen 	if (sysfs_deprecated && dev->class == &block_class)
189939aba963SKay Sievers 		return;
1900ead454feSRandy Dunlap #endif
19016b6e39a6SKay Sievers 	sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
19022ee97cafSCornelia Huck }
19032ee97cafSCornelia Huck 
19041da177e4SLinus Torvalds /**
1905413c239fSStephen Rothwell  * dev_set_name - set a device name
1906413c239fSStephen Rothwell  * @dev: device
190746232366SRandy Dunlap  * @fmt: format string for the device's name
1908413c239fSStephen Rothwell  */
1909413c239fSStephen Rothwell int dev_set_name(struct device *dev, const char *fmt, ...)
1910413c239fSStephen Rothwell {
1911413c239fSStephen Rothwell 	va_list vargs;
19121fa5ae85SKay Sievers 	int err;
1913413c239fSStephen Rothwell 
1914413c239fSStephen Rothwell 	va_start(vargs, fmt);
19151fa5ae85SKay Sievers 	err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
1916413c239fSStephen Rothwell 	va_end(vargs);
19171fa5ae85SKay Sievers 	return err;
1918413c239fSStephen Rothwell }
1919413c239fSStephen Rothwell EXPORT_SYMBOL_GPL(dev_set_name);
1920413c239fSStephen Rothwell 
1921413c239fSStephen Rothwell /**
1922e105b8bfSDan Williams  * device_to_dev_kobj - select a /sys/dev/ directory for the device
1923e105b8bfSDan Williams  * @dev: device
1924e105b8bfSDan Williams  *
1925e105b8bfSDan Williams  * By default we select char/ for new entries.  Setting class->dev_obj
1926e105b8bfSDan Williams  * to NULL prevents an entry from being created.  class->dev_kobj must
1927e105b8bfSDan Williams  * be set (or cleared) before any devices are registered to the class
1928e105b8bfSDan Williams  * otherwise device_create_sys_dev_entry() and
19290d4e293cSPeter Korsgaard  * device_remove_sys_dev_entry() will disagree about the presence of
19300d4e293cSPeter Korsgaard  * the link.
1931e105b8bfSDan Williams  */
1932e105b8bfSDan Williams static struct kobject *device_to_dev_kobj(struct device *dev)
1933e105b8bfSDan Williams {
1934e105b8bfSDan Williams 	struct kobject *kobj;
1935e105b8bfSDan Williams 
1936e105b8bfSDan Williams 	if (dev->class)
1937e105b8bfSDan Williams 		kobj = dev->class->dev_kobj;
1938e105b8bfSDan Williams 	else
1939e105b8bfSDan Williams 		kobj = sysfs_dev_char_kobj;
1940e105b8bfSDan Williams 
1941e105b8bfSDan Williams 	return kobj;
1942e105b8bfSDan Williams }
1943e105b8bfSDan Williams 
1944e105b8bfSDan Williams static int device_create_sys_dev_entry(struct device *dev)
1945e105b8bfSDan Williams {
1946e105b8bfSDan Williams 	struct kobject *kobj = device_to_dev_kobj(dev);
1947e105b8bfSDan Williams 	int error = 0;
1948e105b8bfSDan Williams 	char devt_str[15];
1949e105b8bfSDan Williams 
1950e105b8bfSDan Williams 	if (kobj) {
1951e105b8bfSDan Williams 		format_dev_t(devt_str, dev->devt);
1952e105b8bfSDan Williams 		error = sysfs_create_link(kobj, &dev->kobj, devt_str);
1953e105b8bfSDan Williams 	}
1954e105b8bfSDan Williams 
1955e105b8bfSDan Williams 	return error;
1956e105b8bfSDan Williams }
1957e105b8bfSDan Williams 
1958e105b8bfSDan Williams static void device_remove_sys_dev_entry(struct device *dev)
1959e105b8bfSDan Williams {
1960e105b8bfSDan Williams 	struct kobject *kobj = device_to_dev_kobj(dev);
1961e105b8bfSDan Williams 	char devt_str[15];
1962e105b8bfSDan Williams 
1963e105b8bfSDan Williams 	if (kobj) {
1964e105b8bfSDan Williams 		format_dev_t(devt_str, dev->devt);
1965e105b8bfSDan Williams 		sysfs_remove_link(kobj, devt_str);
1966e105b8bfSDan Williams 	}
1967e105b8bfSDan Williams }
1968e105b8bfSDan Williams 
196946d3a037SShaokun Zhang static int device_private_init(struct device *dev)
1970b4028437SGreg Kroah-Hartman {
1971b4028437SGreg Kroah-Hartman 	dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
1972b4028437SGreg Kroah-Hartman 	if (!dev->p)
1973b4028437SGreg Kroah-Hartman 		return -ENOMEM;
1974b4028437SGreg Kroah-Hartman 	dev->p->device = dev;
1975b4028437SGreg Kroah-Hartman 	klist_init(&dev->p->klist_children, klist_children_get,
1976b4028437SGreg Kroah-Hartman 		   klist_children_put);
1977ef8a3fd6SGreg Kroah-Hartman 	INIT_LIST_HEAD(&dev->p->deferred_probe);
1978b4028437SGreg Kroah-Hartman 	return 0;
1979b4028437SGreg Kroah-Hartman }
1980b4028437SGreg Kroah-Hartman 
1981e105b8bfSDan Williams /**
19821da177e4SLinus Torvalds  * device_add - add device to device hierarchy.
19831da177e4SLinus Torvalds  * @dev: device.
19841da177e4SLinus Torvalds  *
19851da177e4SLinus Torvalds  * This is part 2 of device_register(), though may be called
19861da177e4SLinus Torvalds  * separately _iff_ device_initialize() has been called separately.
19871da177e4SLinus Torvalds  *
19885739411aSCornelia Huck  * This adds @dev to the kobject hierarchy via kobject_add(), adds it
19891da177e4SLinus Torvalds  * to the global and sibling lists for the device, then
19901da177e4SLinus Torvalds  * adds it to the other relevant subsystems of the driver model.
19915739411aSCornelia Huck  *
1992b10d5efdSAlan Stern  * Do not call this routine or device_register() more than once for
1993b10d5efdSAlan Stern  * any device structure.  The driver model core is not designed to work
1994b10d5efdSAlan Stern  * with devices that get unregistered and then spring back to life.
1995b10d5efdSAlan Stern  * (Among other things, it's very hard to guarantee that all references
1996b10d5efdSAlan Stern  * to the previous incarnation of @dev have been dropped.)  Allocate
1997b10d5efdSAlan Stern  * and register a fresh new struct device instead.
1998b10d5efdSAlan Stern  *
19995739411aSCornelia Huck  * NOTE: _Never_ directly free @dev after calling this function, even
20005739411aSCornelia Huck  * if it returned an error! Always use put_device() to give up your
20015739411aSCornelia Huck  * reference instead.
2002affada72SBorislav Petkov  *
2003affada72SBorislav Petkov  * Rule of thumb is: if device_add() succeeds, you should call
2004affada72SBorislav Petkov  * device_del() when you want to get rid of it. If device_add() has
2005affada72SBorislav Petkov  * *not* succeeded, use *only* put_device() to drop the reference
2006affada72SBorislav Petkov  * count.
20071da177e4SLinus Torvalds  */
20081da177e4SLinus Torvalds int device_add(struct device *dev)
20091da177e4SLinus Torvalds {
201035dbf4efSViresh Kumar 	struct device *parent;
2011ca22e56dSKay Sievers 	struct kobject *kobj;
2012c47ed219SGreg Kroah-Hartman 	struct class_interface *class_intf;
2013c906a48aSGreg Kroah-Hartman 	int error = -EINVAL;
2014cebf8fd1SMing Lei 	struct kobject *glue_dir = NULL;
2015775b64d2SRafael J. Wysocki 
20161da177e4SLinus Torvalds 	dev = get_device(dev);
2017c906a48aSGreg Kroah-Hartman 	if (!dev)
2018c906a48aSGreg Kroah-Hartman 		goto done;
2019c906a48aSGreg Kroah-Hartman 
2020fb069a5dSGreg Kroah-Hartman 	if (!dev->p) {
2021b4028437SGreg Kroah-Hartman 		error = device_private_init(dev);
2022b4028437SGreg Kroah-Hartman 		if (error)
2023fb069a5dSGreg Kroah-Hartman 			goto done;
2024fb069a5dSGreg Kroah-Hartman 	}
2025fb069a5dSGreg Kroah-Hartman 
20261fa5ae85SKay Sievers 	/*
20271fa5ae85SKay Sievers 	 * for statically allocated devices, which should all be converted
20281fa5ae85SKay Sievers 	 * some day, we need to initialize the name. We prevent reading back
20291fa5ae85SKay Sievers 	 * the name, and force the use of dev_name()
20301fa5ae85SKay Sievers 	 */
20311fa5ae85SKay Sievers 	if (dev->init_name) {
2032acc0e90fSGreg Kroah-Hartman 		dev_set_name(dev, "%s", dev->init_name);
20331fa5ae85SKay Sievers 		dev->init_name = NULL;
20341fa5ae85SKay Sievers 	}
2035c906a48aSGreg Kroah-Hartman 
2036ca22e56dSKay Sievers 	/* subsystems can specify simple device enumeration */
2037ca22e56dSKay Sievers 	if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
2038ca22e56dSKay Sievers 		dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
2039ca22e56dSKay Sievers 
2040e6309e75SThomas Gleixner 	if (!dev_name(dev)) {
2041e6309e75SThomas Gleixner 		error = -EINVAL;
20425c8563d7SKay Sievers 		goto name_error;
2043e6309e75SThomas Gleixner 	}
20441da177e4SLinus Torvalds 
20451e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2046c205ef48SGreg Kroah-Hartman 
20471da177e4SLinus Torvalds 	parent = get_device(dev->parent);
2048ca22e56dSKay Sievers 	kobj = get_device_parent(dev, parent);
204984d0c27dSTetsuo Handa 	if (IS_ERR(kobj)) {
205084d0c27dSTetsuo Handa 		error = PTR_ERR(kobj);
205184d0c27dSTetsuo Handa 		goto parent_error;
205284d0c27dSTetsuo Handa 	}
2053ca22e56dSKay Sievers 	if (kobj)
2054ca22e56dSKay Sievers 		dev->kobj.parent = kobj;
20551da177e4SLinus Torvalds 
20560d358f22SYinghai Lu 	/* use parent numa_node */
205756f2de81SZhen Lei 	if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
20580d358f22SYinghai Lu 		set_dev_node(dev, dev_to_node(parent));
20590d358f22SYinghai Lu 
20601da177e4SLinus Torvalds 	/* first, register with generic layer. */
20618a577ffcSKay Sievers 	/* we require the name to be set before, and pass NULL */
20628a577ffcSKay Sievers 	error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
2063cebf8fd1SMing Lei 	if (error) {
2064cebf8fd1SMing Lei 		glue_dir = get_glue_dir(dev);
20651da177e4SLinus Torvalds 		goto Error;
2066cebf8fd1SMing Lei 	}
2067a7fd6706SKay Sievers 
206837022644SBrian Walsh 	/* notify platform of device entry */
206907de0e86SHeikki Krogerus 	error = device_platform_notify(dev, KOBJ_ADD);
207007de0e86SHeikki Krogerus 	if (error)
207107de0e86SHeikki Krogerus 		goto platform_error;
207237022644SBrian Walsh 
2073c5e064a6SGreg Kroah-Hartman 	error = device_create_file(dev, &dev_attr_uevent);
2074a306eea4SCornelia Huck 	if (error)
2075a306eea4SCornelia Huck 		goto attrError;
2076a7fd6706SKay Sievers 
20772ee97cafSCornelia Huck 	error = device_add_class_symlinks(dev);
20782ee97cafSCornelia Huck 	if (error)
20792ee97cafSCornelia Huck 		goto SymlinkError;
2080dc0afa83SCornelia Huck 	error = device_add_attrs(dev);
2081dc0afa83SCornelia Huck 	if (error)
20822620efefSGreg Kroah-Hartman 		goto AttrsError;
2083dc0afa83SCornelia Huck 	error = bus_add_device(dev);
2084dc0afa83SCornelia Huck 	if (error)
20851da177e4SLinus Torvalds 		goto BusError;
20863b98aeafSAlan Stern 	error = dpm_sysfs_add(dev);
208757eee3d2SRafael J. Wysocki 	if (error)
20883b98aeafSAlan Stern 		goto DPMError;
20893b98aeafSAlan Stern 	device_pm_add(dev);
2090ec0676eeSAlan Stern 
20910cd75047SSergey Klyaus 	if (MAJOR(dev->devt)) {
20920cd75047SSergey Klyaus 		error = device_create_file(dev, &dev_attr_dev);
20930cd75047SSergey Klyaus 		if (error)
20940cd75047SSergey Klyaus 			goto DevAttrError;
20950cd75047SSergey Klyaus 
20960cd75047SSergey Klyaus 		error = device_create_sys_dev_entry(dev);
20970cd75047SSergey Klyaus 		if (error)
20980cd75047SSergey Klyaus 			goto SysEntryError;
20990cd75047SSergey Klyaus 
21000cd75047SSergey Klyaus 		devtmpfs_create_node(dev);
21010cd75047SSergey Klyaus 	}
21020cd75047SSergey Klyaus 
2103ec0676eeSAlan Stern 	/* Notify clients of device addition.  This call must come
2104268863f4Smajianpeng 	 * after dpm_sysfs_add() and before kobject_uevent().
2105ec0676eeSAlan Stern 	 */
2106ec0676eeSAlan Stern 	if (dev->bus)
2107ec0676eeSAlan Stern 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2108ec0676eeSAlan Stern 					     BUS_NOTIFY_ADD_DEVICE, dev);
2109ec0676eeSAlan Stern 
211053877d06SKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_ADD);
21112023c610SAlan Stern 	bus_probe_device(dev);
21121da177e4SLinus Torvalds 	if (parent)
2113f791b8c8SGreg Kroah-Hartman 		klist_add_tail(&dev->p->knode_parent,
2114f791b8c8SGreg Kroah-Hartman 			       &parent->p->klist_children);
21151da177e4SLinus Torvalds 
21165d9fd169SGreg Kroah-Hartman 	if (dev->class) {
2117ca22e56dSKay Sievers 		mutex_lock(&dev->class->p->mutex);
2118c47ed219SGreg Kroah-Hartman 		/* tie the class to the device */
2119570d0200SWei Yang 		klist_add_tail(&dev->p->knode_class,
21206b6e39a6SKay Sievers 			       &dev->class->p->klist_devices);
2121c47ed219SGreg Kroah-Hartman 
2122c47ed219SGreg Kroah-Hartman 		/* notify any interfaces that the device is here */
2123184f1f77SGreg Kroah-Hartman 		list_for_each_entry(class_intf,
2124ca22e56dSKay Sievers 				    &dev->class->p->interfaces, node)
2125c47ed219SGreg Kroah-Hartman 			if (class_intf->add_dev)
2126c47ed219SGreg Kroah-Hartman 				class_intf->add_dev(dev, class_intf);
2127ca22e56dSKay Sievers 		mutex_unlock(&dev->class->p->mutex);
21285d9fd169SGreg Kroah-Hartman 	}
2129c906a48aSGreg Kroah-Hartman done:
21301da177e4SLinus Torvalds 	put_device(dev);
21311da177e4SLinus Torvalds 	return error;
21320cd75047SSergey Klyaus  SysEntryError:
21330cd75047SSergey Klyaus 	if (MAJOR(dev->devt))
21340cd75047SSergey Klyaus 		device_remove_file(dev, &dev_attr_dev);
21350cd75047SSergey Klyaus  DevAttrError:
21360cd75047SSergey Klyaus 	device_pm_remove(dev);
21370cd75047SSergey Klyaus 	dpm_sysfs_remove(dev);
21383b98aeafSAlan Stern  DPMError:
213957eee3d2SRafael J. Wysocki 	bus_remove_device(dev);
214057eee3d2SRafael J. Wysocki  BusError:
21412620efefSGreg Kroah-Hartman 	device_remove_attrs(dev);
21422620efefSGreg Kroah-Hartman  AttrsError:
21432ee97cafSCornelia Huck 	device_remove_class_symlinks(dev);
21442ee97cafSCornelia Huck  SymlinkError:
2145c5e064a6SGreg Kroah-Hartman 	device_remove_file(dev, &dev_attr_uevent);
214623681e47SGreg Kroah-Hartman  attrError:
214707de0e86SHeikki Krogerus 	device_platform_notify(dev, KOBJ_REMOVE);
214807de0e86SHeikki Krogerus platform_error:
2149312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2150cebf8fd1SMing Lei 	glue_dir = get_glue_dir(dev);
21511da177e4SLinus Torvalds 	kobject_del(&dev->kobj);
21521da177e4SLinus Torvalds  Error:
2153cebf8fd1SMing Lei 	cleanup_glue_dir(dev, glue_dir);
215484d0c27dSTetsuo Handa parent_error:
21551da177e4SLinus Torvalds 	put_device(parent);
21565c8563d7SKay Sievers name_error:
21575c8563d7SKay Sievers 	kfree(dev->p);
21585c8563d7SKay Sievers 	dev->p = NULL;
2159c906a48aSGreg Kroah-Hartman 	goto done;
21601da177e4SLinus Torvalds }
216186df2687SDavid Graham White EXPORT_SYMBOL_GPL(device_add);
21621da177e4SLinus Torvalds 
21631da177e4SLinus Torvalds /**
21641da177e4SLinus Torvalds  * device_register - register a device with the system.
21651da177e4SLinus Torvalds  * @dev: pointer to the device structure
21661da177e4SLinus Torvalds  *
21671da177e4SLinus Torvalds  * This happens in two clean steps - initialize the device
21681da177e4SLinus Torvalds  * and add it to the system. The two steps can be called
21691da177e4SLinus Torvalds  * separately, but this is the easiest and most common.
21701da177e4SLinus Torvalds  * I.e. you should only call the two helpers separately if
21711da177e4SLinus Torvalds  * have a clearly defined need to use and refcount the device
21721da177e4SLinus Torvalds  * before it is added to the hierarchy.
21735739411aSCornelia Huck  *
2174b10d5efdSAlan Stern  * For more information, see the kerneldoc for device_initialize()
2175b10d5efdSAlan Stern  * and device_add().
2176b10d5efdSAlan Stern  *
21775739411aSCornelia Huck  * NOTE: _Never_ directly free @dev after calling this function, even
21785739411aSCornelia Huck  * if it returned an error! Always use put_device() to give up the
21795739411aSCornelia Huck  * reference initialized in this function instead.
21801da177e4SLinus Torvalds  */
21811da177e4SLinus Torvalds int device_register(struct device *dev)
21821da177e4SLinus Torvalds {
21831da177e4SLinus Torvalds 	device_initialize(dev);
21841da177e4SLinus Torvalds 	return device_add(dev);
21851da177e4SLinus Torvalds }
218686df2687SDavid Graham White EXPORT_SYMBOL_GPL(device_register);
21871da177e4SLinus Torvalds 
21881da177e4SLinus Torvalds /**
21891da177e4SLinus Torvalds  * get_device - increment reference count for device.
21901da177e4SLinus Torvalds  * @dev: device.
21911da177e4SLinus Torvalds  *
21921da177e4SLinus Torvalds  * This simply forwards the call to kobject_get(), though
21931da177e4SLinus Torvalds  * we do take care to provide for the case that we get a NULL
21941da177e4SLinus Torvalds  * pointer passed in.
21951da177e4SLinus Torvalds  */
21961da177e4SLinus Torvalds struct device *get_device(struct device *dev)
21971da177e4SLinus Torvalds {
2198b0d1f807SLars-Peter Clausen 	return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
21991da177e4SLinus Torvalds }
220086df2687SDavid Graham White EXPORT_SYMBOL_GPL(get_device);
22011da177e4SLinus Torvalds 
22021da177e4SLinus Torvalds /**
22031da177e4SLinus Torvalds  * put_device - decrement reference count.
22041da177e4SLinus Torvalds  * @dev: device in question.
22051da177e4SLinus Torvalds  */
22061da177e4SLinus Torvalds void put_device(struct device *dev)
22071da177e4SLinus Torvalds {
2208edfaa7c3SKay Sievers 	/* might_sleep(); */
22091da177e4SLinus Torvalds 	if (dev)
22101da177e4SLinus Torvalds 		kobject_put(&dev->kobj);
22111da177e4SLinus Torvalds }
221286df2687SDavid Graham White EXPORT_SYMBOL_GPL(put_device);
22131da177e4SLinus Torvalds 
22141da177e4SLinus Torvalds /**
22151da177e4SLinus Torvalds  * device_del - delete device from system.
22161da177e4SLinus Torvalds  * @dev: device.
22171da177e4SLinus Torvalds  *
22181da177e4SLinus Torvalds  * This is the first part of the device unregistration
22191da177e4SLinus Torvalds  * sequence. This removes the device from the lists we control
22201da177e4SLinus Torvalds  * from here, has it removed from the other driver model
22211da177e4SLinus Torvalds  * subsystems it was added to in device_add(), and removes it
22221da177e4SLinus Torvalds  * from the kobject hierarchy.
22231da177e4SLinus Torvalds  *
22241da177e4SLinus Torvalds  * NOTE: this should be called manually _iff_ device_add() was
22251da177e4SLinus Torvalds  * also called manually.
22261da177e4SLinus Torvalds  */
22271da177e4SLinus Torvalds void device_del(struct device *dev)
22281da177e4SLinus Torvalds {
22291da177e4SLinus Torvalds 	struct device *parent = dev->parent;
2230cebf8fd1SMing Lei 	struct kobject *glue_dir = NULL;
2231c47ed219SGreg Kroah-Hartman 	struct class_interface *class_intf;
22321da177e4SLinus Torvalds 
22333451a495SAlexander Duyck 	/*
22343451a495SAlexander Duyck 	 * Hold the device lock and set the "dead" flag to guarantee that
22353451a495SAlexander Duyck 	 * the update behavior is consistent with the other bitfields near
22363451a495SAlexander Duyck 	 * it and that we cannot have an asynchronous probe routine trying
22373451a495SAlexander Duyck 	 * to run while we are tearing out the bus/class/sysfs from
22383451a495SAlexander Duyck 	 * underneath the device.
22393451a495SAlexander Duyck 	 */
22403451a495SAlexander Duyck 	device_lock(dev);
22413451a495SAlexander Duyck 	dev->p->dead = true;
22423451a495SAlexander Duyck 	device_unlock(dev);
22433451a495SAlexander Duyck 
2244ec0676eeSAlan Stern 	/* Notify clients of device removal.  This call must come
2245ec0676eeSAlan Stern 	 * before dpm_sysfs_remove().
2246ec0676eeSAlan Stern 	 */
2247ec0676eeSAlan Stern 	if (dev->bus)
2248ec0676eeSAlan Stern 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2249ec0676eeSAlan Stern 					     BUS_NOTIFY_DEL_DEVICE, dev);
22509ed98953SRafael J. Wysocki 
22513b98aeafSAlan Stern 	dpm_sysfs_remove(dev);
22521da177e4SLinus Torvalds 	if (parent)
2253f791b8c8SGreg Kroah-Hartman 		klist_del(&dev->p->knode_parent);
2254e105b8bfSDan Williams 	if (MAJOR(dev->devt)) {
22552b2af54aSKay Sievers 		devtmpfs_delete_node(dev);
2256e105b8bfSDan Williams 		device_remove_sys_dev_entry(dev);
2257c5e064a6SGreg Kroah-Hartman 		device_remove_file(dev, &dev_attr_dev);
2258e105b8bfSDan Williams 	}
2259b9d9c82bSKay Sievers 	if (dev->class) {
2260da231fd5SKay Sievers 		device_remove_class_symlinks(dev);
226199ef3ef8SKay Sievers 
2262ca22e56dSKay Sievers 		mutex_lock(&dev->class->p->mutex);
2263c47ed219SGreg Kroah-Hartman 		/* notify any interfaces that the device is now gone */
2264184f1f77SGreg Kroah-Hartman 		list_for_each_entry(class_intf,
2265ca22e56dSKay Sievers 				    &dev->class->p->interfaces, node)
2266c47ed219SGreg Kroah-Hartman 			if (class_intf->remove_dev)
2267c47ed219SGreg Kroah-Hartman 				class_intf->remove_dev(dev, class_intf);
2268c47ed219SGreg Kroah-Hartman 		/* remove the device from the class list */
2269570d0200SWei Yang 		klist_del(&dev->p->knode_class);
2270ca22e56dSKay Sievers 		mutex_unlock(&dev->class->p->mutex);
2271b9d9c82bSKay Sievers 	}
2272c5e064a6SGreg Kroah-Hartman 	device_remove_file(dev, &dev_attr_uevent);
22732620efefSGreg Kroah-Hartman 	device_remove_attrs(dev);
227428953533SBenjamin Herrenschmidt 	bus_remove_device(dev);
22754b6d1f12SLongX Zhang 	device_pm_remove(dev);
2276d1c3414cSGrant Likely 	driver_deferred_probe_del(dev);
227707de0e86SHeikki Krogerus 	device_platform_notify(dev, KOBJ_REMOVE);
2278478573c9SLukas Wunner 	device_remove_properties(dev);
22792ec16150SJeffy Chen 	device_links_purge(dev);
22801da177e4SLinus Torvalds 
2281599bad38SJoerg Roedel 	if (dev->bus)
2282599bad38SJoerg Roedel 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2283599bad38SJoerg Roedel 					     BUS_NOTIFY_REMOVED_DEVICE, dev);
2284312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2285cebf8fd1SMing Lei 	glue_dir = get_glue_dir(dev);
22861da177e4SLinus Torvalds 	kobject_del(&dev->kobj);
2287cebf8fd1SMing Lei 	cleanup_glue_dir(dev, glue_dir);
22881da177e4SLinus Torvalds 	put_device(parent);
22891da177e4SLinus Torvalds }
229086df2687SDavid Graham White EXPORT_SYMBOL_GPL(device_del);
22911da177e4SLinus Torvalds 
22921da177e4SLinus Torvalds /**
22931da177e4SLinus Torvalds  * device_unregister - unregister device from system.
22941da177e4SLinus Torvalds  * @dev: device going away.
22951da177e4SLinus Torvalds  *
22961da177e4SLinus Torvalds  * We do this in two parts, like we do device_register(). First,
22971da177e4SLinus Torvalds  * we remove it from all the subsystems with device_del(), then
22981da177e4SLinus Torvalds  * we decrement the reference count via put_device(). If that
22991da177e4SLinus Torvalds  * is the final reference count, the device will be cleaned up
23001da177e4SLinus Torvalds  * via device_release() above. Otherwise, the structure will
23011da177e4SLinus Torvalds  * stick around until the final reference to the device is dropped.
23021da177e4SLinus Torvalds  */
23031da177e4SLinus Torvalds void device_unregister(struct device *dev)
23041da177e4SLinus Torvalds {
23051e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
23061da177e4SLinus Torvalds 	device_del(dev);
23071da177e4SLinus Torvalds 	put_device(dev);
23081da177e4SLinus Torvalds }
230986df2687SDavid Graham White EXPORT_SYMBOL_GPL(device_unregister);
23101da177e4SLinus Torvalds 
23113d060aebSAndy Shevchenko static struct device *prev_device(struct klist_iter *i)
23123d060aebSAndy Shevchenko {
23133d060aebSAndy Shevchenko 	struct klist_node *n = klist_prev(i);
23143d060aebSAndy Shevchenko 	struct device *dev = NULL;
23153d060aebSAndy Shevchenko 	struct device_private *p;
23163d060aebSAndy Shevchenko 
23173d060aebSAndy Shevchenko 	if (n) {
23183d060aebSAndy Shevchenko 		p = to_device_private_parent(n);
23193d060aebSAndy Shevchenko 		dev = p->device;
23203d060aebSAndy Shevchenko 	}
23213d060aebSAndy Shevchenko 	return dev;
23223d060aebSAndy Shevchenko }
23233d060aebSAndy Shevchenko 
232436239577Smochel@digitalimplant.org static struct device *next_device(struct klist_iter *i)
232536239577Smochel@digitalimplant.org {
232636239577Smochel@digitalimplant.org 	struct klist_node *n = klist_next(i);
2327f791b8c8SGreg Kroah-Hartman 	struct device *dev = NULL;
2328f791b8c8SGreg Kroah-Hartman 	struct device_private *p;
2329f791b8c8SGreg Kroah-Hartman 
2330f791b8c8SGreg Kroah-Hartman 	if (n) {
2331f791b8c8SGreg Kroah-Hartman 		p = to_device_private_parent(n);
2332f791b8c8SGreg Kroah-Hartman 		dev = p->device;
2333f791b8c8SGreg Kroah-Hartman 	}
2334f791b8c8SGreg Kroah-Hartman 	return dev;
233536239577Smochel@digitalimplant.org }
233636239577Smochel@digitalimplant.org 
23371da177e4SLinus Torvalds /**
2338e454cea2SKay Sievers  * device_get_devnode - path of device node file
23396fcf53acSKay Sievers  * @dev: device
2340e454cea2SKay Sievers  * @mode: returned file access mode
23413c2670e6SKay Sievers  * @uid: returned file owner
23423c2670e6SKay Sievers  * @gid: returned file group
23436fcf53acSKay Sievers  * @tmp: possibly allocated string
23446fcf53acSKay Sievers  *
23456fcf53acSKay Sievers  * Return the relative path of a possible device node.
23466fcf53acSKay Sievers  * Non-default names may need to allocate a memory to compose
23476fcf53acSKay Sievers  * a name. This memory is returned in tmp and needs to be
23486fcf53acSKay Sievers  * freed by the caller.
23496fcf53acSKay Sievers  */
2350e454cea2SKay Sievers const char *device_get_devnode(struct device *dev,
23514e4098a3SGreg Kroah-Hartman 			       umode_t *mode, kuid_t *uid, kgid_t *gid,
23523c2670e6SKay Sievers 			       const char **tmp)
23536fcf53acSKay Sievers {
23546fcf53acSKay Sievers 	char *s;
23556fcf53acSKay Sievers 
23566fcf53acSKay Sievers 	*tmp = NULL;
23576fcf53acSKay Sievers 
23586fcf53acSKay Sievers 	/* the device type may provide a specific name */
2359e454cea2SKay Sievers 	if (dev->type && dev->type->devnode)
23603c2670e6SKay Sievers 		*tmp = dev->type->devnode(dev, mode, uid, gid);
23616fcf53acSKay Sievers 	if (*tmp)
23626fcf53acSKay Sievers 		return *tmp;
23636fcf53acSKay Sievers 
23646fcf53acSKay Sievers 	/* the class may provide a specific name */
2365e454cea2SKay Sievers 	if (dev->class && dev->class->devnode)
2366e454cea2SKay Sievers 		*tmp = dev->class->devnode(dev, mode);
23676fcf53acSKay Sievers 	if (*tmp)
23686fcf53acSKay Sievers 		return *tmp;
23696fcf53acSKay Sievers 
23706fcf53acSKay Sievers 	/* return name without allocation, tmp == NULL */
23716fcf53acSKay Sievers 	if (strchr(dev_name(dev), '!') == NULL)
23726fcf53acSKay Sievers 		return dev_name(dev);
23736fcf53acSKay Sievers 
23746fcf53acSKay Sievers 	/* replace '!' in the name with '/' */
2375a29fd614SRasmus Villemoes 	s = kstrdup(dev_name(dev), GFP_KERNEL);
2376a29fd614SRasmus Villemoes 	if (!s)
23776fcf53acSKay Sievers 		return NULL;
2378a29fd614SRasmus Villemoes 	strreplace(s, '!', '/');
2379a29fd614SRasmus Villemoes 	return *tmp = s;
23806fcf53acSKay Sievers }
23816fcf53acSKay Sievers 
23826fcf53acSKay Sievers /**
23831da177e4SLinus Torvalds  * device_for_each_child - device child iterator.
2384c41455fbSRandy Dunlap  * @parent: parent struct device.
23851da177e4SLinus Torvalds  * @fn: function to be called for each device.
2386f8878dcbSRobert P. J. Day  * @data: data for the callback.
23871da177e4SLinus Torvalds  *
2388c41455fbSRandy Dunlap  * Iterate over @parent's child devices, and call @fn for each,
23891da177e4SLinus Torvalds  * passing it @data.
23901da177e4SLinus Torvalds  *
23911da177e4SLinus Torvalds  * We check the return of @fn each time. If it returns anything
23921da177e4SLinus Torvalds  * other than 0, we break out and return that value.
23931da177e4SLinus Torvalds  */
239436239577Smochel@digitalimplant.org int device_for_each_child(struct device *parent, void *data,
23954a3ad20cSGreg Kroah-Hartman 			  int (*fn)(struct device *dev, void *data))
23961da177e4SLinus Torvalds {
239736239577Smochel@digitalimplant.org 	struct klist_iter i;
23981da177e4SLinus Torvalds 	struct device *child;
23991da177e4SLinus Torvalds 	int error = 0;
24001da177e4SLinus Torvalds 
2401014c90dbSGreg Kroah-Hartman 	if (!parent->p)
2402014c90dbSGreg Kroah-Hartman 		return 0;
2403014c90dbSGreg Kroah-Hartman 
2404f791b8c8SGreg Kroah-Hartman 	klist_iter_init(&parent->p->klist_children, &i);
240593ead7c9SGimcuan Hui 	while (!error && (child = next_device(&i)))
240636239577Smochel@digitalimplant.org 		error = fn(child, data);
240736239577Smochel@digitalimplant.org 	klist_iter_exit(&i);
24081da177e4SLinus Torvalds 	return error;
24091da177e4SLinus Torvalds }
241086df2687SDavid Graham White EXPORT_SYMBOL_GPL(device_for_each_child);
24111da177e4SLinus Torvalds 
24125ab69981SCornelia Huck /**
24133d060aebSAndy Shevchenko  * device_for_each_child_reverse - device child iterator in reversed order.
24143d060aebSAndy Shevchenko  * @parent: parent struct device.
24153d060aebSAndy Shevchenko  * @fn: function to be called for each device.
24163d060aebSAndy Shevchenko  * @data: data for the callback.
24173d060aebSAndy Shevchenko  *
24183d060aebSAndy Shevchenko  * Iterate over @parent's child devices, and call @fn for each,
24193d060aebSAndy Shevchenko  * passing it @data.
24203d060aebSAndy Shevchenko  *
24213d060aebSAndy Shevchenko  * We check the return of @fn each time. If it returns anything
24223d060aebSAndy Shevchenko  * other than 0, we break out and return that value.
24233d060aebSAndy Shevchenko  */
24243d060aebSAndy Shevchenko int device_for_each_child_reverse(struct device *parent, void *data,
24253d060aebSAndy Shevchenko 				  int (*fn)(struct device *dev, void *data))
24263d060aebSAndy Shevchenko {
24273d060aebSAndy Shevchenko 	struct klist_iter i;
24283d060aebSAndy Shevchenko 	struct device *child;
24293d060aebSAndy Shevchenko 	int error = 0;
24303d060aebSAndy Shevchenko 
24313d060aebSAndy Shevchenko 	if (!parent->p)
24323d060aebSAndy Shevchenko 		return 0;
24333d060aebSAndy Shevchenko 
24343d060aebSAndy Shevchenko 	klist_iter_init(&parent->p->klist_children, &i);
24353d060aebSAndy Shevchenko 	while ((child = prev_device(&i)) && !error)
24363d060aebSAndy Shevchenko 		error = fn(child, data);
24373d060aebSAndy Shevchenko 	klist_iter_exit(&i);
24383d060aebSAndy Shevchenko 	return error;
24393d060aebSAndy Shevchenko }
24403d060aebSAndy Shevchenko EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
24413d060aebSAndy Shevchenko 
24423d060aebSAndy Shevchenko /**
24435ab69981SCornelia Huck  * device_find_child - device iterator for locating a particular device.
24445ab69981SCornelia Huck  * @parent: parent struct device
24455ab69981SCornelia Huck  * @match: Callback function to check device
2446f8878dcbSRobert P. J. Day  * @data: Data to pass to match function
24475ab69981SCornelia Huck  *
24485ab69981SCornelia Huck  * This is similar to the device_for_each_child() function above, but it
24495ab69981SCornelia Huck  * returns a reference to a device that is 'found' for later use, as
24505ab69981SCornelia Huck  * determined by the @match callback.
24515ab69981SCornelia Huck  *
24525ab69981SCornelia Huck  * The callback should return 0 if the device doesn't match and non-zero
24535ab69981SCornelia Huck  * if it does.  If the callback returns non-zero and a reference to the
24545ab69981SCornelia Huck  * current device can be obtained, this function will return to the caller
24555ab69981SCornelia Huck  * and not iterate over any more devices.
2456a4e2400aSFederico Vaga  *
2457a4e2400aSFederico Vaga  * NOTE: you will need to drop the reference with put_device() after use.
24585ab69981SCornelia Huck  */
24595ab69981SCornelia Huck struct device *device_find_child(struct device *parent, void *data,
24604a3ad20cSGreg Kroah-Hartman 				 int (*match)(struct device *dev, void *data))
24615ab69981SCornelia Huck {
24625ab69981SCornelia Huck 	struct klist_iter i;
24635ab69981SCornelia Huck 	struct device *child;
24645ab69981SCornelia Huck 
24655ab69981SCornelia Huck 	if (!parent)
24665ab69981SCornelia Huck 		return NULL;
24675ab69981SCornelia Huck 
2468f791b8c8SGreg Kroah-Hartman 	klist_iter_init(&parent->p->klist_children, &i);
24695ab69981SCornelia Huck 	while ((child = next_device(&i)))
24705ab69981SCornelia Huck 		if (match(child, data) && get_device(child))
24715ab69981SCornelia Huck 			break;
24725ab69981SCornelia Huck 	klist_iter_exit(&i);
24735ab69981SCornelia Huck 	return child;
24745ab69981SCornelia Huck }
247586df2687SDavid Graham White EXPORT_SYMBOL_GPL(device_find_child);
24765ab69981SCornelia Huck 
2477dad9bb01SHeikki Krogerus /**
2478dad9bb01SHeikki Krogerus  * device_find_child_by_name - device iterator for locating a child device.
2479dad9bb01SHeikki Krogerus  * @parent: parent struct device
2480dad9bb01SHeikki Krogerus  * @name: name of the child device
2481dad9bb01SHeikki Krogerus  *
2482dad9bb01SHeikki Krogerus  * This is similar to the device_find_child() function above, but it
2483dad9bb01SHeikki Krogerus  * returns a reference to a device that has the name @name.
2484dad9bb01SHeikki Krogerus  *
2485dad9bb01SHeikki Krogerus  * NOTE: you will need to drop the reference with put_device() after use.
2486dad9bb01SHeikki Krogerus  */
2487dad9bb01SHeikki Krogerus struct device *device_find_child_by_name(struct device *parent,
2488dad9bb01SHeikki Krogerus 					 const char *name)
2489dad9bb01SHeikki Krogerus {
2490dad9bb01SHeikki Krogerus 	struct klist_iter i;
2491dad9bb01SHeikki Krogerus 	struct device *child;
2492dad9bb01SHeikki Krogerus 
2493dad9bb01SHeikki Krogerus 	if (!parent)
2494dad9bb01SHeikki Krogerus 		return NULL;
2495dad9bb01SHeikki Krogerus 
2496dad9bb01SHeikki Krogerus 	klist_iter_init(&parent->p->klist_children, &i);
2497dad9bb01SHeikki Krogerus 	while ((child = next_device(&i)))
2498dad9bb01SHeikki Krogerus 		if (!strcmp(dev_name(child), name) && get_device(child))
2499dad9bb01SHeikki Krogerus 			break;
2500dad9bb01SHeikki Krogerus 	klist_iter_exit(&i);
2501dad9bb01SHeikki Krogerus 	return child;
2502dad9bb01SHeikki Krogerus }
2503dad9bb01SHeikki Krogerus EXPORT_SYMBOL_GPL(device_find_child_by_name);
2504dad9bb01SHeikki Krogerus 
25051da177e4SLinus Torvalds int __init devices_init(void)
25061da177e4SLinus Torvalds {
2507881c6cfdSGreg Kroah-Hartman 	devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2508881c6cfdSGreg Kroah-Hartman 	if (!devices_kset)
2509881c6cfdSGreg Kroah-Hartman 		return -ENOMEM;
2510e105b8bfSDan Williams 	dev_kobj = kobject_create_and_add("dev", NULL);
2511e105b8bfSDan Williams 	if (!dev_kobj)
2512e105b8bfSDan Williams 		goto dev_kobj_err;
2513e105b8bfSDan Williams 	sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2514e105b8bfSDan Williams 	if (!sysfs_dev_block_kobj)
2515e105b8bfSDan Williams 		goto block_kobj_err;
2516e105b8bfSDan Williams 	sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2517e105b8bfSDan Williams 	if (!sysfs_dev_char_kobj)
2518e105b8bfSDan Williams 		goto char_kobj_err;
2519e105b8bfSDan Williams 
2520881c6cfdSGreg Kroah-Hartman 	return 0;
2521e105b8bfSDan Williams 
2522e105b8bfSDan Williams  char_kobj_err:
2523e105b8bfSDan Williams 	kobject_put(sysfs_dev_block_kobj);
2524e105b8bfSDan Williams  block_kobj_err:
2525e105b8bfSDan Williams 	kobject_put(dev_kobj);
2526e105b8bfSDan Williams  dev_kobj_err:
2527e105b8bfSDan Williams 	kset_unregister(devices_kset);
2528e105b8bfSDan Williams 	return -ENOMEM;
25291da177e4SLinus Torvalds }
25301da177e4SLinus Torvalds 
25314f3549d7SRafael J. Wysocki static int device_check_offline(struct device *dev, void *not_used)
25324f3549d7SRafael J. Wysocki {
25334f3549d7SRafael J. Wysocki 	int ret;
25344f3549d7SRafael J. Wysocki 
25354f3549d7SRafael J. Wysocki 	ret = device_for_each_child(dev, NULL, device_check_offline);
25364f3549d7SRafael J. Wysocki 	if (ret)
25374f3549d7SRafael J. Wysocki 		return ret;
25384f3549d7SRafael J. Wysocki 
25394f3549d7SRafael J. Wysocki 	return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
25404f3549d7SRafael J. Wysocki }
25414f3549d7SRafael J. Wysocki 
25424f3549d7SRafael J. Wysocki /**
25434f3549d7SRafael J. Wysocki  * device_offline - Prepare the device for hot-removal.
25444f3549d7SRafael J. Wysocki  * @dev: Device to be put offline.
25454f3549d7SRafael J. Wysocki  *
25464f3549d7SRafael J. Wysocki  * Execute the device bus type's .offline() callback, if present, to prepare
25474f3549d7SRafael J. Wysocki  * the device for a subsequent hot-removal.  If that succeeds, the device must
25484f3549d7SRafael J. Wysocki  * not be used until either it is removed or its bus type's .online() callback
25494f3549d7SRafael J. Wysocki  * is executed.
25504f3549d7SRafael J. Wysocki  *
25514f3549d7SRafael J. Wysocki  * Call under device_hotplug_lock.
25524f3549d7SRafael J. Wysocki  */
25534f3549d7SRafael J. Wysocki int device_offline(struct device *dev)
25544f3549d7SRafael J. Wysocki {
25554f3549d7SRafael J. Wysocki 	int ret;
25564f3549d7SRafael J. Wysocki 
25574f3549d7SRafael J. Wysocki 	if (dev->offline_disabled)
25584f3549d7SRafael J. Wysocki 		return -EPERM;
25594f3549d7SRafael J. Wysocki 
25604f3549d7SRafael J. Wysocki 	ret = device_for_each_child(dev, NULL, device_check_offline);
25614f3549d7SRafael J. Wysocki 	if (ret)
25624f3549d7SRafael J. Wysocki 		return ret;
25634f3549d7SRafael J. Wysocki 
25644f3549d7SRafael J. Wysocki 	device_lock(dev);
25654f3549d7SRafael J. Wysocki 	if (device_supports_offline(dev)) {
25664f3549d7SRafael J. Wysocki 		if (dev->offline) {
25674f3549d7SRafael J. Wysocki 			ret = 1;
25684f3549d7SRafael J. Wysocki 		} else {
25694f3549d7SRafael J. Wysocki 			ret = dev->bus->offline(dev);
25704f3549d7SRafael J. Wysocki 			if (!ret) {
25714f3549d7SRafael J. Wysocki 				kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
25724f3549d7SRafael J. Wysocki 				dev->offline = true;
25734f3549d7SRafael J. Wysocki 			}
25744f3549d7SRafael J. Wysocki 		}
25754f3549d7SRafael J. Wysocki 	}
25764f3549d7SRafael J. Wysocki 	device_unlock(dev);
25774f3549d7SRafael J. Wysocki 
25784f3549d7SRafael J. Wysocki 	return ret;
25794f3549d7SRafael J. Wysocki }
25804f3549d7SRafael J. Wysocki 
25814f3549d7SRafael J. Wysocki /**
25824f3549d7SRafael J. Wysocki  * device_online - Put the device back online after successful device_offline().
25834f3549d7SRafael J. Wysocki  * @dev: Device to be put back online.
25844f3549d7SRafael J. Wysocki  *
25854f3549d7SRafael J. Wysocki  * If device_offline() has been successfully executed for @dev, but the device
25864f3549d7SRafael J. Wysocki  * has not been removed subsequently, execute its bus type's .online() callback
25874f3549d7SRafael J. Wysocki  * to indicate that the device can be used again.
25884f3549d7SRafael J. Wysocki  *
25894f3549d7SRafael J. Wysocki  * Call under device_hotplug_lock.
25904f3549d7SRafael J. Wysocki  */
25914f3549d7SRafael J. Wysocki int device_online(struct device *dev)
25924f3549d7SRafael J. Wysocki {
25934f3549d7SRafael J. Wysocki 	int ret = 0;
25944f3549d7SRafael J. Wysocki 
25954f3549d7SRafael J. Wysocki 	device_lock(dev);
25964f3549d7SRafael J. Wysocki 	if (device_supports_offline(dev)) {
25974f3549d7SRafael J. Wysocki 		if (dev->offline) {
25984f3549d7SRafael J. Wysocki 			ret = dev->bus->online(dev);
25994f3549d7SRafael J. Wysocki 			if (!ret) {
26004f3549d7SRafael J. Wysocki 				kobject_uevent(&dev->kobj, KOBJ_ONLINE);
26014f3549d7SRafael J. Wysocki 				dev->offline = false;
26024f3549d7SRafael J. Wysocki 			}
26034f3549d7SRafael J. Wysocki 		} else {
26044f3549d7SRafael J. Wysocki 			ret = 1;
26054f3549d7SRafael J. Wysocki 		}
26064f3549d7SRafael J. Wysocki 	}
26074f3549d7SRafael J. Wysocki 	device_unlock(dev);
26084f3549d7SRafael J. Wysocki 
26094f3549d7SRafael J. Wysocki 	return ret;
26104f3549d7SRafael J. Wysocki }
26114f3549d7SRafael J. Wysocki 
26127f100d15SKarthigan Srinivasan struct root_device {
26130aa0dc41SMark McLoughlin 	struct device dev;
26140aa0dc41SMark McLoughlin 	struct module *owner;
26150aa0dc41SMark McLoughlin };
26160aa0dc41SMark McLoughlin 
261793058424SJosh Triplett static inline struct root_device *to_root_device(struct device *d)
2618481e2079SFerenc Wagner {
2619481e2079SFerenc Wagner 	return container_of(d, struct root_device, dev);
2620481e2079SFerenc Wagner }
26210aa0dc41SMark McLoughlin 
26220aa0dc41SMark McLoughlin static void root_device_release(struct device *dev)
26230aa0dc41SMark McLoughlin {
26240aa0dc41SMark McLoughlin 	kfree(to_root_device(dev));
26250aa0dc41SMark McLoughlin }
26260aa0dc41SMark McLoughlin 
26270aa0dc41SMark McLoughlin /**
26280aa0dc41SMark McLoughlin  * __root_device_register - allocate and register a root device
26290aa0dc41SMark McLoughlin  * @name: root device name
26300aa0dc41SMark McLoughlin  * @owner: owner module of the root device, usually THIS_MODULE
26310aa0dc41SMark McLoughlin  *
26320aa0dc41SMark McLoughlin  * This function allocates a root device and registers it
26330aa0dc41SMark McLoughlin  * using device_register(). In order to free the returned
26340aa0dc41SMark McLoughlin  * device, use root_device_unregister().
26350aa0dc41SMark McLoughlin  *
26360aa0dc41SMark McLoughlin  * Root devices are dummy devices which allow other devices
26370aa0dc41SMark McLoughlin  * to be grouped under /sys/devices. Use this function to
26380aa0dc41SMark McLoughlin  * allocate a root device and then use it as the parent of
26390aa0dc41SMark McLoughlin  * any device which should appear under /sys/devices/{name}
26400aa0dc41SMark McLoughlin  *
26410aa0dc41SMark McLoughlin  * The /sys/devices/{name} directory will also contain a
26420aa0dc41SMark McLoughlin  * 'module' symlink which points to the @owner directory
26430aa0dc41SMark McLoughlin  * in sysfs.
26440aa0dc41SMark McLoughlin  *
2645f0eae0edSJani Nikula  * Returns &struct device pointer on success, or ERR_PTR() on error.
2646f0eae0edSJani Nikula  *
26470aa0dc41SMark McLoughlin  * Note: You probably want to use root_device_register().
26480aa0dc41SMark McLoughlin  */
26490aa0dc41SMark McLoughlin struct device *__root_device_register(const char *name, struct module *owner)
26500aa0dc41SMark McLoughlin {
26510aa0dc41SMark McLoughlin 	struct root_device *root;
26520aa0dc41SMark McLoughlin 	int err = -ENOMEM;
26530aa0dc41SMark McLoughlin 
26540aa0dc41SMark McLoughlin 	root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
26550aa0dc41SMark McLoughlin 	if (!root)
26560aa0dc41SMark McLoughlin 		return ERR_PTR(err);
26570aa0dc41SMark McLoughlin 
2658acc0e90fSGreg Kroah-Hartman 	err = dev_set_name(&root->dev, "%s", name);
26590aa0dc41SMark McLoughlin 	if (err) {
26600aa0dc41SMark McLoughlin 		kfree(root);
26610aa0dc41SMark McLoughlin 		return ERR_PTR(err);
26620aa0dc41SMark McLoughlin 	}
26630aa0dc41SMark McLoughlin 
26640aa0dc41SMark McLoughlin 	root->dev.release = root_device_release;
26650aa0dc41SMark McLoughlin 
26660aa0dc41SMark McLoughlin 	err = device_register(&root->dev);
26670aa0dc41SMark McLoughlin 	if (err) {
26680aa0dc41SMark McLoughlin 		put_device(&root->dev);
26690aa0dc41SMark McLoughlin 		return ERR_PTR(err);
26700aa0dc41SMark McLoughlin 	}
26710aa0dc41SMark McLoughlin 
26721d9e882bSChristoph Egger #ifdef CONFIG_MODULES	/* gotta find a "cleaner" way to do this */
26730aa0dc41SMark McLoughlin 	if (owner) {
26740aa0dc41SMark McLoughlin 		struct module_kobject *mk = &owner->mkobj;
26750aa0dc41SMark McLoughlin 
26760aa0dc41SMark McLoughlin 		err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
26770aa0dc41SMark McLoughlin 		if (err) {
26780aa0dc41SMark McLoughlin 			device_unregister(&root->dev);
26790aa0dc41SMark McLoughlin 			return ERR_PTR(err);
26800aa0dc41SMark McLoughlin 		}
26810aa0dc41SMark McLoughlin 		root->owner = owner;
26820aa0dc41SMark McLoughlin 	}
26830aa0dc41SMark McLoughlin #endif
26840aa0dc41SMark McLoughlin 
26850aa0dc41SMark McLoughlin 	return &root->dev;
26860aa0dc41SMark McLoughlin }
26870aa0dc41SMark McLoughlin EXPORT_SYMBOL_GPL(__root_device_register);
26880aa0dc41SMark McLoughlin 
26890aa0dc41SMark McLoughlin /**
26900aa0dc41SMark McLoughlin  * root_device_unregister - unregister and free a root device
26917cbcf225SRandy Dunlap  * @dev: device going away
26920aa0dc41SMark McLoughlin  *
26930aa0dc41SMark McLoughlin  * This function unregisters and cleans up a device that was created by
26940aa0dc41SMark McLoughlin  * root_device_register().
26950aa0dc41SMark McLoughlin  */
26960aa0dc41SMark McLoughlin void root_device_unregister(struct device *dev)
26970aa0dc41SMark McLoughlin {
26980aa0dc41SMark McLoughlin 	struct root_device *root = to_root_device(dev);
26990aa0dc41SMark McLoughlin 
27000aa0dc41SMark McLoughlin 	if (root->owner)
27010aa0dc41SMark McLoughlin 		sysfs_remove_link(&root->dev.kobj, "module");
27020aa0dc41SMark McLoughlin 
27030aa0dc41SMark McLoughlin 	device_unregister(dev);
27040aa0dc41SMark McLoughlin }
27050aa0dc41SMark McLoughlin EXPORT_SYMBOL_GPL(root_device_unregister);
27060aa0dc41SMark McLoughlin 
270723681e47SGreg Kroah-Hartman 
270823681e47SGreg Kroah-Hartman static void device_create_release(struct device *dev)
270923681e47SGreg Kroah-Hartman {
27101e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
271123681e47SGreg Kroah-Hartman 	kfree(dev);
271223681e47SGreg Kroah-Hartman }
271323681e47SGreg Kroah-Hartman 
27146a8b55d7SMathieu Malaterre static __printf(6, 0) struct device *
271539ef3112SGuenter Roeck device_create_groups_vargs(struct class *class, struct device *parent,
271639ef3112SGuenter Roeck 			   dev_t devt, void *drvdata,
271739ef3112SGuenter Roeck 			   const struct attribute_group **groups,
271839ef3112SGuenter Roeck 			   const char *fmt, va_list args)
271939ef3112SGuenter Roeck {
272039ef3112SGuenter Roeck 	struct device *dev = NULL;
272139ef3112SGuenter Roeck 	int retval = -ENODEV;
272239ef3112SGuenter Roeck 
272339ef3112SGuenter Roeck 	if (class == NULL || IS_ERR(class))
272439ef3112SGuenter Roeck 		goto error;
272539ef3112SGuenter Roeck 
272639ef3112SGuenter Roeck 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
272739ef3112SGuenter Roeck 	if (!dev) {
272839ef3112SGuenter Roeck 		retval = -ENOMEM;
272939ef3112SGuenter Roeck 		goto error;
273039ef3112SGuenter Roeck 	}
273139ef3112SGuenter Roeck 
2732bbc780f8SDavid Herrmann 	device_initialize(dev);
273339ef3112SGuenter Roeck 	dev->devt = devt;
273439ef3112SGuenter Roeck 	dev->class = class;
273539ef3112SGuenter Roeck 	dev->parent = parent;
273639ef3112SGuenter Roeck 	dev->groups = groups;
273739ef3112SGuenter Roeck 	dev->release = device_create_release;
273839ef3112SGuenter Roeck 	dev_set_drvdata(dev, drvdata);
273939ef3112SGuenter Roeck 
274039ef3112SGuenter Roeck 	retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
274139ef3112SGuenter Roeck 	if (retval)
274239ef3112SGuenter Roeck 		goto error;
274339ef3112SGuenter Roeck 
2744bbc780f8SDavid Herrmann 	retval = device_add(dev);
274539ef3112SGuenter Roeck 	if (retval)
274639ef3112SGuenter Roeck 		goto error;
274739ef3112SGuenter Roeck 
274839ef3112SGuenter Roeck 	return dev;
274939ef3112SGuenter Roeck 
275039ef3112SGuenter Roeck error:
275139ef3112SGuenter Roeck 	put_device(dev);
275239ef3112SGuenter Roeck 	return ERR_PTR(retval);
275339ef3112SGuenter Roeck }
275439ef3112SGuenter Roeck 
275523681e47SGreg Kroah-Hartman /**
27568882b394SGreg Kroah-Hartman  * device_create_vargs - creates a device and registers it with sysfs
27578882b394SGreg Kroah-Hartman  * @class: pointer to the struct class that this device should be registered to
27588882b394SGreg Kroah-Hartman  * @parent: pointer to the parent struct device of this new device, if any
27598882b394SGreg Kroah-Hartman  * @devt: the dev_t for the char device to be added
27608882b394SGreg Kroah-Hartman  * @drvdata: the data to be added to the device for callbacks
27618882b394SGreg Kroah-Hartman  * @fmt: string for the device's name
27628882b394SGreg Kroah-Hartman  * @args: va_list for the device's name
27638882b394SGreg Kroah-Hartman  *
27648882b394SGreg Kroah-Hartman  * This function can be used by char device classes.  A struct device
27658882b394SGreg Kroah-Hartman  * will be created in sysfs, registered to the specified class.
27668882b394SGreg Kroah-Hartman  *
27678882b394SGreg Kroah-Hartman  * A "dev" file will be created, showing the dev_t for the device, if
27688882b394SGreg Kroah-Hartman  * the dev_t is not 0,0.
27698882b394SGreg Kroah-Hartman  * If a pointer to a parent struct device is passed in, the newly created
27708882b394SGreg Kroah-Hartman  * struct device will be a child of that device in sysfs.
27718882b394SGreg Kroah-Hartman  * The pointer to the struct device will be returned from the call.
27728882b394SGreg Kroah-Hartman  * Any further sysfs files that might be required can be created using this
27738882b394SGreg Kroah-Hartman  * pointer.
27748882b394SGreg Kroah-Hartman  *
2775f0eae0edSJani Nikula  * Returns &struct device pointer on success, or ERR_PTR() on error.
2776f0eae0edSJani Nikula  *
27778882b394SGreg Kroah-Hartman  * Note: the struct class passed to this function must have previously
27788882b394SGreg Kroah-Hartman  * been created with a call to class_create().
27798882b394SGreg Kroah-Hartman  */
27808882b394SGreg Kroah-Hartman struct device *device_create_vargs(struct class *class, struct device *parent,
27818882b394SGreg Kroah-Hartman 				   dev_t devt, void *drvdata, const char *fmt,
27828882b394SGreg Kroah-Hartman 				   va_list args)
27838882b394SGreg Kroah-Hartman {
278439ef3112SGuenter Roeck 	return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
278539ef3112SGuenter Roeck 					  fmt, args);
27868882b394SGreg Kroah-Hartman }
27878882b394SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_vargs);
27888882b394SGreg Kroah-Hartman 
27898882b394SGreg Kroah-Hartman /**
27904e106739SGreg Kroah-Hartman  * device_create - creates a device and registers it with sysfs
27918882b394SGreg Kroah-Hartman  * @class: pointer to the struct class that this device should be registered to
27928882b394SGreg Kroah-Hartman  * @parent: pointer to the parent struct device of this new device, if any
27938882b394SGreg Kroah-Hartman  * @devt: the dev_t for the char device to be added
27948882b394SGreg Kroah-Hartman  * @drvdata: the data to be added to the device for callbacks
27958882b394SGreg Kroah-Hartman  * @fmt: string for the device's name
27968882b394SGreg Kroah-Hartman  *
27978882b394SGreg Kroah-Hartman  * This function can be used by char device classes.  A struct device
27988882b394SGreg Kroah-Hartman  * will be created in sysfs, registered to the specified class.
27998882b394SGreg Kroah-Hartman  *
28008882b394SGreg Kroah-Hartman  * A "dev" file will be created, showing the dev_t for the device, if
28018882b394SGreg Kroah-Hartman  * the dev_t is not 0,0.
28028882b394SGreg Kroah-Hartman  * If a pointer to a parent struct device is passed in, the newly created
28038882b394SGreg Kroah-Hartman  * struct device will be a child of that device in sysfs.
28048882b394SGreg Kroah-Hartman  * The pointer to the struct device will be returned from the call.
28058882b394SGreg Kroah-Hartman  * Any further sysfs files that might be required can be created using this
28068882b394SGreg Kroah-Hartman  * pointer.
28078882b394SGreg Kroah-Hartman  *
2808f0eae0edSJani Nikula  * Returns &struct device pointer on success, or ERR_PTR() on error.
2809f0eae0edSJani Nikula  *
28108882b394SGreg Kroah-Hartman  * Note: the struct class passed to this function must have previously
28118882b394SGreg Kroah-Hartman  * been created with a call to class_create().
28128882b394SGreg Kroah-Hartman  */
28134e106739SGreg Kroah-Hartman struct device *device_create(struct class *class, struct device *parent,
28144e106739SGreg Kroah-Hartman 			     dev_t devt, void *drvdata, const char *fmt, ...)
28158882b394SGreg Kroah-Hartman {
28168882b394SGreg Kroah-Hartman 	va_list vargs;
28178882b394SGreg Kroah-Hartman 	struct device *dev;
28188882b394SGreg Kroah-Hartman 
28198882b394SGreg Kroah-Hartman 	va_start(vargs, fmt);
28208882b394SGreg Kroah-Hartman 	dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
28218882b394SGreg Kroah-Hartman 	va_end(vargs);
28228882b394SGreg Kroah-Hartman 	return dev;
28238882b394SGreg Kroah-Hartman }
28244e106739SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create);
28258882b394SGreg Kroah-Hartman 
282639ef3112SGuenter Roeck /**
282739ef3112SGuenter Roeck  * device_create_with_groups - creates a device and registers it with sysfs
282839ef3112SGuenter Roeck  * @class: pointer to the struct class that this device should be registered to
282939ef3112SGuenter Roeck  * @parent: pointer to the parent struct device of this new device, if any
283039ef3112SGuenter Roeck  * @devt: the dev_t for the char device to be added
283139ef3112SGuenter Roeck  * @drvdata: the data to be added to the device for callbacks
283239ef3112SGuenter Roeck  * @groups: NULL-terminated list of attribute groups to be created
283339ef3112SGuenter Roeck  * @fmt: string for the device's name
283439ef3112SGuenter Roeck  *
283539ef3112SGuenter Roeck  * This function can be used by char device classes.  A struct device
283639ef3112SGuenter Roeck  * will be created in sysfs, registered to the specified class.
283739ef3112SGuenter Roeck  * Additional attributes specified in the groups parameter will also
283839ef3112SGuenter Roeck  * be created automatically.
283939ef3112SGuenter Roeck  *
284039ef3112SGuenter Roeck  * A "dev" file will be created, showing the dev_t for the device, if
284139ef3112SGuenter Roeck  * the dev_t is not 0,0.
284239ef3112SGuenter Roeck  * If a pointer to a parent struct device is passed in, the newly created
284339ef3112SGuenter Roeck  * struct device will be a child of that device in sysfs.
284439ef3112SGuenter Roeck  * The pointer to the struct device will be returned from the call.
284539ef3112SGuenter Roeck  * Any further sysfs files that might be required can be created using this
284639ef3112SGuenter Roeck  * pointer.
284739ef3112SGuenter Roeck  *
284839ef3112SGuenter Roeck  * Returns &struct device pointer on success, or ERR_PTR() on error.
284939ef3112SGuenter Roeck  *
285039ef3112SGuenter Roeck  * Note: the struct class passed to this function must have previously
285139ef3112SGuenter Roeck  * been created with a call to class_create().
285239ef3112SGuenter Roeck  */
285339ef3112SGuenter Roeck struct device *device_create_with_groups(struct class *class,
285439ef3112SGuenter Roeck 					 struct device *parent, dev_t devt,
285539ef3112SGuenter Roeck 					 void *drvdata,
285639ef3112SGuenter Roeck 					 const struct attribute_group **groups,
285739ef3112SGuenter Roeck 					 const char *fmt, ...)
285839ef3112SGuenter Roeck {
285939ef3112SGuenter Roeck 	va_list vargs;
286039ef3112SGuenter Roeck 	struct device *dev;
286139ef3112SGuenter Roeck 
286239ef3112SGuenter Roeck 	va_start(vargs, fmt);
286339ef3112SGuenter Roeck 	dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
286439ef3112SGuenter Roeck 					 fmt, vargs);
286539ef3112SGuenter Roeck 	va_end(vargs);
286639ef3112SGuenter Roeck 	return dev;
286739ef3112SGuenter Roeck }
286839ef3112SGuenter Roeck EXPORT_SYMBOL_GPL(device_create_with_groups);
286939ef3112SGuenter Roeck 
2870775b64d2SRafael J. Wysocki /**
2871775b64d2SRafael J. Wysocki  * device_destroy - removes a device that was created with device_create()
2872775b64d2SRafael J. Wysocki  * @class: pointer to the struct class that this device was registered with
2873775b64d2SRafael J. Wysocki  * @devt: the dev_t of the device that was previously registered
2874775b64d2SRafael J. Wysocki  *
2875775b64d2SRafael J. Wysocki  * This call unregisters and cleans up a device that was created with a
2876775b64d2SRafael J. Wysocki  * call to device_create().
2877775b64d2SRafael J. Wysocki  */
2878775b64d2SRafael J. Wysocki void device_destroy(struct class *class, dev_t devt)
2879775b64d2SRafael J. Wysocki {
2880775b64d2SRafael J. Wysocki 	struct device *dev;
2881775b64d2SRafael J. Wysocki 
28824495dfddSSuzuki K Poulose 	dev = class_find_device_by_devt(class, devt);
2883cd35449bSDave Young 	if (dev) {
2884cd35449bSDave Young 		put_device(dev);
288523681e47SGreg Kroah-Hartman 		device_unregister(dev);
288623681e47SGreg Kroah-Hartman 	}
2887cd35449bSDave Young }
288823681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_destroy);
2889a2de48caSGreg Kroah-Hartman 
2890a2de48caSGreg Kroah-Hartman /**
2891a2de48caSGreg Kroah-Hartman  * device_rename - renames a device
2892a2de48caSGreg Kroah-Hartman  * @dev: the pointer to the struct device to be renamed
2893a2de48caSGreg Kroah-Hartman  * @new_name: the new name of the device
2894030c1d2bSEric W. Biederman  *
2895030c1d2bSEric W. Biederman  * It is the responsibility of the caller to provide mutual
2896030c1d2bSEric W. Biederman  * exclusion between two different calls of device_rename
2897030c1d2bSEric W. Biederman  * on the same device to ensure that new_name is valid and
2898030c1d2bSEric W. Biederman  * won't conflict with other devices.
2899c6c0ac66SMichael Ellerman  *
2900a5462516STimur Tabi  * Note: Don't call this function.  Currently, the networking layer calls this
2901a5462516STimur Tabi  * function, but that will change.  The following text from Kay Sievers offers
2902a5462516STimur Tabi  * some insight:
2903a5462516STimur Tabi  *
2904a5462516STimur Tabi  * Renaming devices is racy at many levels, symlinks and other stuff are not
2905a5462516STimur Tabi  * replaced atomically, and you get a "move" uevent, but it's not easy to
2906a5462516STimur Tabi  * connect the event to the old and new device. Device nodes are not renamed at
2907a5462516STimur Tabi  * all, there isn't even support for that in the kernel now.
2908a5462516STimur Tabi  *
2909a5462516STimur Tabi  * In the meantime, during renaming, your target name might be taken by another
2910a5462516STimur Tabi  * driver, creating conflicts. Or the old name is taken directly after you
2911a5462516STimur Tabi  * renamed it -- then you get events for the same DEVPATH, before you even see
2912a5462516STimur Tabi  * the "move" event. It's just a mess, and nothing new should ever rely on
2913a5462516STimur Tabi  * kernel device renaming. Besides that, it's not even implemented now for
2914a5462516STimur Tabi  * other things than (driver-core wise very simple) network devices.
2915a5462516STimur Tabi  *
2916a5462516STimur Tabi  * We are currently about to change network renaming in udev to completely
2917a5462516STimur Tabi  * disallow renaming of devices in the same namespace as the kernel uses,
2918a5462516STimur Tabi  * because we can't solve the problems properly, that arise with swapping names
2919a5462516STimur Tabi  * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
2920a5462516STimur Tabi  * be allowed to some other name than eth[0-9]*, for the aforementioned
2921a5462516STimur Tabi  * reasons.
2922a5462516STimur Tabi  *
2923a5462516STimur Tabi  * Make up a "real" name in the driver before you register anything, or add
2924a5462516STimur Tabi  * some other attributes for userspace to find the device, or use udev to add
2925a5462516STimur Tabi  * symlinks -- but never rename kernel devices later, it's a complete mess. We
2926a5462516STimur Tabi  * don't even want to get into that and try to implement the missing pieces in
2927a5462516STimur Tabi  * the core. We really have other pieces to fix in the driver core mess. :)
2928a2de48caSGreg Kroah-Hartman  */
29296937e8f8SJohannes Berg int device_rename(struct device *dev, const char *new_name)
2930a2de48caSGreg Kroah-Hartman {
29314b30ee58STejun Heo 	struct kobject *kobj = &dev->kobj;
29322ee97cafSCornelia Huck 	char *old_device_name = NULL;
2933a2de48caSGreg Kroah-Hartman 	int error;
2934a2de48caSGreg Kroah-Hartman 
2935a2de48caSGreg Kroah-Hartman 	dev = get_device(dev);
2936a2de48caSGreg Kroah-Hartman 	if (!dev)
2937a2de48caSGreg Kroah-Hartman 		return -EINVAL;
2938a2de48caSGreg Kroah-Hartman 
293969df7533Sethan.zhao 	dev_dbg(dev, "renaming to %s\n", new_name);
2940a2de48caSGreg Kroah-Hartman 
29411fa5ae85SKay Sievers 	old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
29422ee97cafSCornelia Huck 	if (!old_device_name) {
2943952ab431SJesper Juhl 		error = -ENOMEM;
29442ee97cafSCornelia Huck 		goto out;
2945952ab431SJesper Juhl 	}
2946a2de48caSGreg Kroah-Hartman 
2947f349cf34SEric W. Biederman 	if (dev->class) {
29484b30ee58STejun Heo 		error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
29494b30ee58STejun Heo 					     kobj, old_device_name,
29504b30ee58STejun Heo 					     new_name, kobject_namespace(kobj));
2951f349cf34SEric W. Biederman 		if (error)
2952f349cf34SEric W. Biederman 			goto out;
2953f349cf34SEric W. Biederman 	}
295439aba963SKay Sievers 
29554b30ee58STejun Heo 	error = kobject_rename(kobj, new_name);
29561fa5ae85SKay Sievers 	if (error)
29572ee97cafSCornelia Huck 		goto out;
2958a2de48caSGreg Kroah-Hartman 
29592ee97cafSCornelia Huck out:
2960a2de48caSGreg Kroah-Hartman 	put_device(dev);
2961a2de48caSGreg Kroah-Hartman 
29622ee97cafSCornelia Huck 	kfree(old_device_name);
2963a2de48caSGreg Kroah-Hartman 
2964a2de48caSGreg Kroah-Hartman 	return error;
2965a2de48caSGreg Kroah-Hartman }
2966a2807dbcSJohannes Berg EXPORT_SYMBOL_GPL(device_rename);
29678a82472fSCornelia Huck 
29688a82472fSCornelia Huck static int device_move_class_links(struct device *dev,
29698a82472fSCornelia Huck 				   struct device *old_parent,
29708a82472fSCornelia Huck 				   struct device *new_parent)
29718a82472fSCornelia Huck {
2972f7f3461dSGreg Kroah-Hartman 	int error = 0;
29738a82472fSCornelia Huck 
2974f7f3461dSGreg Kroah-Hartman 	if (old_parent)
2975f7f3461dSGreg Kroah-Hartman 		sysfs_remove_link(&dev->kobj, "device");
2976f7f3461dSGreg Kroah-Hartman 	if (new_parent)
2977f7f3461dSGreg Kroah-Hartman 		error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
2978f7f3461dSGreg Kroah-Hartman 					  "device");
2979f7f3461dSGreg Kroah-Hartman 	return error;
29808a82472fSCornelia Huck }
29818a82472fSCornelia Huck 
29828a82472fSCornelia Huck /**
29838a82472fSCornelia Huck  * device_move - moves a device to a new parent
29848a82472fSCornelia Huck  * @dev: the pointer to the struct device to be moved
298513509860SWolfram Sang  * @new_parent: the new parent of the device (can be NULL)
2986ffa6a705SCornelia Huck  * @dpm_order: how to reorder the dpm_list
29878a82472fSCornelia Huck  */
2988ffa6a705SCornelia Huck int device_move(struct device *dev, struct device *new_parent,
2989ffa6a705SCornelia Huck 		enum dpm_order dpm_order)
29908a82472fSCornelia Huck {
29918a82472fSCornelia Huck 	int error;
29928a82472fSCornelia Huck 	struct device *old_parent;
2993c744aeaeSCornelia Huck 	struct kobject *new_parent_kobj;
29948a82472fSCornelia Huck 
29958a82472fSCornelia Huck 	dev = get_device(dev);
29968a82472fSCornelia Huck 	if (!dev)
29978a82472fSCornelia Huck 		return -EINVAL;
29988a82472fSCornelia Huck 
2999ffa6a705SCornelia Huck 	device_pm_lock();
30008a82472fSCornelia Huck 	new_parent = get_device(new_parent);
3001c744aeaeSCornelia Huck 	new_parent_kobj = get_device_parent(dev, new_parent);
300284d0c27dSTetsuo Handa 	if (IS_ERR(new_parent_kobj)) {
300384d0c27dSTetsuo Handa 		error = PTR_ERR(new_parent_kobj);
300484d0c27dSTetsuo Handa 		put_device(new_parent);
300584d0c27dSTetsuo Handa 		goto out;
300684d0c27dSTetsuo Handa 	}
300763b6971aSCornelia Huck 
30081e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
30091e0b2cf9SKay Sievers 		 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
3010c744aeaeSCornelia Huck 	error = kobject_move(&dev->kobj, new_parent_kobj);
30118a82472fSCornelia Huck 	if (error) {
301263b6971aSCornelia Huck 		cleanup_glue_dir(dev, new_parent_kobj);
30138a82472fSCornelia Huck 		put_device(new_parent);
30148a82472fSCornelia Huck 		goto out;
30158a82472fSCornelia Huck 	}
30168a82472fSCornelia Huck 	old_parent = dev->parent;
30178a82472fSCornelia Huck 	dev->parent = new_parent;
30188a82472fSCornelia Huck 	if (old_parent)
3019f791b8c8SGreg Kroah-Hartman 		klist_remove(&dev->p->knode_parent);
30200d358f22SYinghai Lu 	if (new_parent) {
3021f791b8c8SGreg Kroah-Hartman 		klist_add_tail(&dev->p->knode_parent,
3022f791b8c8SGreg Kroah-Hartman 			       &new_parent->p->klist_children);
30230d358f22SYinghai Lu 		set_dev_node(dev, dev_to_node(new_parent));
30240d358f22SYinghai Lu 	}
30250d358f22SYinghai Lu 
3026bdd4034dSRabin Vincent 	if (dev->class) {
30278a82472fSCornelia Huck 		error = device_move_class_links(dev, old_parent, new_parent);
30288a82472fSCornelia Huck 		if (error) {
30298a82472fSCornelia Huck 			/* We ignore errors on cleanup since we're hosed anyway... */
30308a82472fSCornelia Huck 			device_move_class_links(dev, new_parent, old_parent);
30318a82472fSCornelia Huck 			if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
3032c744aeaeSCornelia Huck 				if (new_parent)
3033f791b8c8SGreg Kroah-Hartman 					klist_remove(&dev->p->knode_parent);
30340d358f22SYinghai Lu 				dev->parent = old_parent;
30350d358f22SYinghai Lu 				if (old_parent) {
3036f791b8c8SGreg Kroah-Hartman 					klist_add_tail(&dev->p->knode_parent,
3037f791b8c8SGreg Kroah-Hartman 						       &old_parent->p->klist_children);
30380d358f22SYinghai Lu 					set_dev_node(dev, dev_to_node(old_parent));
30390d358f22SYinghai Lu 				}
30408a82472fSCornelia Huck 			}
304163b6971aSCornelia Huck 			cleanup_glue_dir(dev, new_parent_kobj);
30428a82472fSCornelia Huck 			put_device(new_parent);
30438a82472fSCornelia Huck 			goto out;
30448a82472fSCornelia Huck 		}
3045bdd4034dSRabin Vincent 	}
3046ffa6a705SCornelia Huck 	switch (dpm_order) {
3047ffa6a705SCornelia Huck 	case DPM_ORDER_NONE:
3048ffa6a705SCornelia Huck 		break;
3049ffa6a705SCornelia Huck 	case DPM_ORDER_DEV_AFTER_PARENT:
3050ffa6a705SCornelia Huck 		device_pm_move_after(dev, new_parent);
305152cdbdd4SGrygorii Strashko 		devices_kset_move_after(dev, new_parent);
3052ffa6a705SCornelia Huck 		break;
3053ffa6a705SCornelia Huck 	case DPM_ORDER_PARENT_BEFORE_DEV:
3054ffa6a705SCornelia Huck 		device_pm_move_before(new_parent, dev);
305552cdbdd4SGrygorii Strashko 		devices_kset_move_before(new_parent, dev);
3056ffa6a705SCornelia Huck 		break;
3057ffa6a705SCornelia Huck 	case DPM_ORDER_DEV_LAST:
3058ffa6a705SCornelia Huck 		device_pm_move_last(dev);
305952cdbdd4SGrygorii Strashko 		devices_kset_move_last(dev);
3060ffa6a705SCornelia Huck 		break;
3061ffa6a705SCornelia Huck 	}
3062bdd4034dSRabin Vincent 
30638a82472fSCornelia Huck 	put_device(old_parent);
30648a82472fSCornelia Huck out:
3065ffa6a705SCornelia Huck 	device_pm_unlock();
30668a82472fSCornelia Huck 	put_device(dev);
30678a82472fSCornelia Huck 	return error;
30688a82472fSCornelia Huck }
30698a82472fSCornelia Huck EXPORT_SYMBOL_GPL(device_move);
307037b0c020SGreg Kroah-Hartman 
307137b0c020SGreg Kroah-Hartman /**
307237b0c020SGreg Kroah-Hartman  * device_shutdown - call ->shutdown() on each device to shutdown.
307337b0c020SGreg Kroah-Hartman  */
307437b0c020SGreg Kroah-Hartman void device_shutdown(void)
307537b0c020SGreg Kroah-Hartman {
3076f123db8eSBenson Leung 	struct device *dev, *parent;
307737b0c020SGreg Kroah-Hartman 
30783297c8fcSPingfan Liu 	wait_for_device_probe();
30793297c8fcSPingfan Liu 	device_block_probing();
30803297c8fcSPingfan Liu 
30816245838fSHugh Daschbach 	spin_lock(&devices_kset->list_lock);
30826245838fSHugh Daschbach 	/*
30836245838fSHugh Daschbach 	 * Walk the devices list backward, shutting down each in turn.
30846245838fSHugh Daschbach 	 * Beware that device unplug events may also start pulling
30856245838fSHugh Daschbach 	 * devices offline, even as the system is shutting down.
30866245838fSHugh Daschbach 	 */
30876245838fSHugh Daschbach 	while (!list_empty(&devices_kset->list)) {
30886245838fSHugh Daschbach 		dev = list_entry(devices_kset->list.prev, struct device,
30896245838fSHugh Daschbach 				kobj.entry);
3090d1c6c030SMing Lei 
3091d1c6c030SMing Lei 		/*
3092d1c6c030SMing Lei 		 * hold reference count of device's parent to
3093d1c6c030SMing Lei 		 * prevent it from being freed because parent's
3094d1c6c030SMing Lei 		 * lock is to be held
3095d1c6c030SMing Lei 		 */
3096f123db8eSBenson Leung 		parent = get_device(dev->parent);
30976245838fSHugh Daschbach 		get_device(dev);
30986245838fSHugh Daschbach 		/*
30996245838fSHugh Daschbach 		 * Make sure the device is off the kset list, in the
31006245838fSHugh Daschbach 		 * event that dev->*->shutdown() doesn't remove it.
31016245838fSHugh Daschbach 		 */
31026245838fSHugh Daschbach 		list_del_init(&dev->kobj.entry);
31036245838fSHugh Daschbach 		spin_unlock(&devices_kset->list_lock);
3104fe6b91f4SAlan Stern 
3105d1c6c030SMing Lei 		/* hold lock to avoid race with probe/release */
3106f123db8eSBenson Leung 		if (parent)
3107f123db8eSBenson Leung 			device_lock(parent);
3108d1c6c030SMing Lei 		device_lock(dev);
3109d1c6c030SMing Lei 
3110fe6b91f4SAlan Stern 		/* Don't allow any more runtime suspends */
3111fe6b91f4SAlan Stern 		pm_runtime_get_noresume(dev);
3112fe6b91f4SAlan Stern 		pm_runtime_barrier(dev);
31136245838fSHugh Daschbach 
31147521621eSMichal Suchanek 		if (dev->class && dev->class->shutdown_pre) {
3115f77af151SJosh Zimmerman 			if (initcall_debug)
31167521621eSMichal Suchanek 				dev_info(dev, "shutdown_pre\n");
31177521621eSMichal Suchanek 			dev->class->shutdown_pre(dev);
31187521621eSMichal Suchanek 		}
31197521621eSMichal Suchanek 		if (dev->bus && dev->bus->shutdown) {
31200246c4faSShuoX Liu 			if (initcall_debug)
31210246c4faSShuoX Liu 				dev_info(dev, "shutdown\n");
312237b0c020SGreg Kroah-Hartman 			dev->bus->shutdown(dev);
312337b0c020SGreg Kroah-Hartman 		} else if (dev->driver && dev->driver->shutdown) {
31240246c4faSShuoX Liu 			if (initcall_debug)
31250246c4faSShuoX Liu 				dev_info(dev, "shutdown\n");
312637b0c020SGreg Kroah-Hartman 			dev->driver->shutdown(dev);
312737b0c020SGreg Kroah-Hartman 		}
3128d1c6c030SMing Lei 
3129d1c6c030SMing Lei 		device_unlock(dev);
3130f123db8eSBenson Leung 		if (parent)
3131f123db8eSBenson Leung 			device_unlock(parent);
3132d1c6c030SMing Lei 
31336245838fSHugh Daschbach 		put_device(dev);
3134f123db8eSBenson Leung 		put_device(parent);
31356245838fSHugh Daschbach 
31366245838fSHugh Daschbach 		spin_lock(&devices_kset->list_lock);
313737b0c020SGreg Kroah-Hartman 	}
31386245838fSHugh Daschbach 	spin_unlock(&devices_kset->list_lock);
313937b0c020SGreg Kroah-Hartman }
314099bcf217SJoe Perches 
314199bcf217SJoe Perches /*
314299bcf217SJoe Perches  * Device logging functions
314399bcf217SJoe Perches  */
314499bcf217SJoe Perches 
314599bcf217SJoe Perches #ifdef CONFIG_PRINTK
3146666f355fSJoe Perches static int
3147666f355fSJoe Perches create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
314899bcf217SJoe Perches {
3149c4e00daaSKay Sievers 	const char *subsys;
3150798efc60SJoe Perches 	size_t pos = 0;
315199bcf217SJoe Perches 
3152c4e00daaSKay Sievers 	if (dev->class)
3153c4e00daaSKay Sievers 		subsys = dev->class->name;
3154c4e00daaSKay Sievers 	else if (dev->bus)
3155c4e00daaSKay Sievers 		subsys = dev->bus->name;
3156c4e00daaSKay Sievers 	else
3157798efc60SJoe Perches 		return 0;
3158c4e00daaSKay Sievers 
3159798efc60SJoe Perches 	pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
3160655e5b7cSBen Hutchings 	if (pos >= hdrlen)
3161655e5b7cSBen Hutchings 		goto overflow;
3162c4e00daaSKay Sievers 
3163c4e00daaSKay Sievers 	/*
3164c4e00daaSKay Sievers 	 * Add device identifier DEVICE=:
3165c4e00daaSKay Sievers 	 *   b12:8         block dev_t
3166c4e00daaSKay Sievers 	 *   c127:3        char dev_t
3167c4e00daaSKay Sievers 	 *   n8            netdev ifindex
3168c4e00daaSKay Sievers 	 *   +sound:card0  subsystem:devname
3169c4e00daaSKay Sievers 	 */
3170c4e00daaSKay Sievers 	if (MAJOR(dev->devt)) {
3171c4e00daaSKay Sievers 		char c;
3172c4e00daaSKay Sievers 
3173c4e00daaSKay Sievers 		if (strcmp(subsys, "block") == 0)
3174c4e00daaSKay Sievers 			c = 'b';
3175c4e00daaSKay Sievers 		else
3176c4e00daaSKay Sievers 			c = 'c';
3177798efc60SJoe Perches 		pos++;
3178798efc60SJoe Perches 		pos += snprintf(hdr + pos, hdrlen - pos,
3179c4e00daaSKay Sievers 				"DEVICE=%c%u:%u",
3180c4e00daaSKay Sievers 				c, MAJOR(dev->devt), MINOR(dev->devt));
3181c4e00daaSKay Sievers 	} else if (strcmp(subsys, "net") == 0) {
3182c4e00daaSKay Sievers 		struct net_device *net = to_net_dev(dev);
3183c4e00daaSKay Sievers 
3184798efc60SJoe Perches 		pos++;
3185798efc60SJoe Perches 		pos += snprintf(hdr + pos, hdrlen - pos,
3186c4e00daaSKay Sievers 				"DEVICE=n%u", net->ifindex);
3187c4e00daaSKay Sievers 	} else {
3188798efc60SJoe Perches 		pos++;
3189798efc60SJoe Perches 		pos += snprintf(hdr + pos, hdrlen - pos,
3190c4e00daaSKay Sievers 				"DEVICE=+%s:%s", subsys, dev_name(dev));
3191c4e00daaSKay Sievers 	}
3192af7f2158SJim Cromie 
3193655e5b7cSBen Hutchings 	if (pos >= hdrlen)
3194655e5b7cSBen Hutchings 		goto overflow;
3195655e5b7cSBen Hutchings 
3196798efc60SJoe Perches 	return pos;
3197655e5b7cSBen Hutchings 
3198655e5b7cSBen Hutchings overflow:
3199655e5b7cSBen Hutchings 	dev_WARN(dev, "device/subsystem name too long");
3200655e5b7cSBen Hutchings 	return 0;
320199bcf217SJoe Perches }
3202798efc60SJoe Perches 
320305e4e5b8SJoe Perches int dev_vprintk_emit(int level, const struct device *dev,
320405e4e5b8SJoe Perches 		     const char *fmt, va_list args)
320505e4e5b8SJoe Perches {
320605e4e5b8SJoe Perches 	char hdr[128];
320705e4e5b8SJoe Perches 	size_t hdrlen;
320805e4e5b8SJoe Perches 
320905e4e5b8SJoe Perches 	hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
321005e4e5b8SJoe Perches 
321105e4e5b8SJoe Perches 	return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
321205e4e5b8SJoe Perches }
321305e4e5b8SJoe Perches EXPORT_SYMBOL(dev_vprintk_emit);
321405e4e5b8SJoe Perches 
321505e4e5b8SJoe Perches int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
321605e4e5b8SJoe Perches {
321705e4e5b8SJoe Perches 	va_list args;
321805e4e5b8SJoe Perches 	int r;
321905e4e5b8SJoe Perches 
322005e4e5b8SJoe Perches 	va_start(args, fmt);
322105e4e5b8SJoe Perches 
322205e4e5b8SJoe Perches 	r = dev_vprintk_emit(level, dev, fmt, args);
322305e4e5b8SJoe Perches 
322405e4e5b8SJoe Perches 	va_end(args);
322505e4e5b8SJoe Perches 
322605e4e5b8SJoe Perches 	return r;
322705e4e5b8SJoe Perches }
322805e4e5b8SJoe Perches EXPORT_SYMBOL(dev_printk_emit);
322905e4e5b8SJoe Perches 
3230d1f1052cSJoe Perches static void __dev_printk(const char *level, const struct device *dev,
3231798efc60SJoe Perches 			struct va_format *vaf)
3232798efc60SJoe Perches {
3233d1f1052cSJoe Perches 	if (dev)
3234d1f1052cSJoe Perches 		dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
3235798efc60SJoe Perches 				dev_driver_string(dev), dev_name(dev), vaf);
3236d1f1052cSJoe Perches 	else
3237d1f1052cSJoe Perches 		printk("%s(NULL device *): %pV", level, vaf);
3238798efc60SJoe Perches }
323999bcf217SJoe Perches 
3240d1f1052cSJoe Perches void dev_printk(const char *level, const struct device *dev,
324199bcf217SJoe Perches 		const char *fmt, ...)
324299bcf217SJoe Perches {
324399bcf217SJoe Perches 	struct va_format vaf;
324499bcf217SJoe Perches 	va_list args;
324599bcf217SJoe Perches 
324699bcf217SJoe Perches 	va_start(args, fmt);
324799bcf217SJoe Perches 
324899bcf217SJoe Perches 	vaf.fmt = fmt;
324999bcf217SJoe Perches 	vaf.va = &args;
325099bcf217SJoe Perches 
3251d1f1052cSJoe Perches 	__dev_printk(level, dev, &vaf);
3252798efc60SJoe Perches 
325399bcf217SJoe Perches 	va_end(args);
325499bcf217SJoe Perches }
325599bcf217SJoe Perches EXPORT_SYMBOL(dev_printk);
325699bcf217SJoe Perches 
325799bcf217SJoe Perches #define define_dev_printk_level(func, kern_level)		\
3258d1f1052cSJoe Perches void func(const struct device *dev, const char *fmt, ...)	\
325999bcf217SJoe Perches {								\
326099bcf217SJoe Perches 	struct va_format vaf;					\
326199bcf217SJoe Perches 	va_list args;						\
326299bcf217SJoe Perches 								\
326399bcf217SJoe Perches 	va_start(args, fmt);					\
326499bcf217SJoe Perches 								\
326599bcf217SJoe Perches 	vaf.fmt = fmt;						\
326699bcf217SJoe Perches 	vaf.va = &args;						\
326799bcf217SJoe Perches 								\
3268d1f1052cSJoe Perches 	__dev_printk(kern_level, dev, &vaf);			\
3269798efc60SJoe Perches 								\
327099bcf217SJoe Perches 	va_end(args);						\
327199bcf217SJoe Perches }								\
327299bcf217SJoe Perches EXPORT_SYMBOL(func);
327399bcf217SJoe Perches 
3274663336eeSJoe Perches define_dev_printk_level(_dev_emerg, KERN_EMERG);
3275663336eeSJoe Perches define_dev_printk_level(_dev_alert, KERN_ALERT);
3276663336eeSJoe Perches define_dev_printk_level(_dev_crit, KERN_CRIT);
3277663336eeSJoe Perches define_dev_printk_level(_dev_err, KERN_ERR);
3278663336eeSJoe Perches define_dev_printk_level(_dev_warn, KERN_WARNING);
3279663336eeSJoe Perches define_dev_printk_level(_dev_notice, KERN_NOTICE);
328099bcf217SJoe Perches define_dev_printk_level(_dev_info, KERN_INFO);
328199bcf217SJoe Perches 
328299bcf217SJoe Perches #endif
328397badf87SRafael J. Wysocki 
328497badf87SRafael J. Wysocki static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
328597badf87SRafael J. Wysocki {
328697badf87SRafael J. Wysocki 	return fwnode && !IS_ERR(fwnode->secondary);
328797badf87SRafael J. Wysocki }
328897badf87SRafael J. Wysocki 
328997badf87SRafael J. Wysocki /**
329097badf87SRafael J. Wysocki  * set_primary_fwnode - Change the primary firmware node of a given device.
329197badf87SRafael J. Wysocki  * @dev: Device to handle.
329297badf87SRafael J. Wysocki  * @fwnode: New primary firmware node of the device.
329397badf87SRafael J. Wysocki  *
329497badf87SRafael J. Wysocki  * Set the device's firmware node pointer to @fwnode, but if a secondary
329597badf87SRafael J. Wysocki  * firmware node of the device is present, preserve it.
329697badf87SRafael J. Wysocki  */
329797badf87SRafael J. Wysocki void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
329897badf87SRafael J. Wysocki {
329997badf87SRafael J. Wysocki 	if (fwnode) {
330097badf87SRafael J. Wysocki 		struct fwnode_handle *fn = dev->fwnode;
330197badf87SRafael J. Wysocki 
330297badf87SRafael J. Wysocki 		if (fwnode_is_primary(fn))
330397badf87SRafael J. Wysocki 			fn = fn->secondary;
330497badf87SRafael J. Wysocki 
330555f89a8aSMika Westerberg 		if (fn) {
330655f89a8aSMika Westerberg 			WARN_ON(fwnode->secondary);
330797badf87SRafael J. Wysocki 			fwnode->secondary = fn;
330855f89a8aSMika Westerberg 		}
330997badf87SRafael J. Wysocki 		dev->fwnode = fwnode;
331097badf87SRafael J. Wysocki 	} else {
331197badf87SRafael J. Wysocki 		dev->fwnode = fwnode_is_primary(dev->fwnode) ?
331297badf87SRafael J. Wysocki 			dev->fwnode->secondary : NULL;
331397badf87SRafael J. Wysocki 	}
331497badf87SRafael J. Wysocki }
331597badf87SRafael J. Wysocki EXPORT_SYMBOL_GPL(set_primary_fwnode);
331697badf87SRafael J. Wysocki 
331797badf87SRafael J. Wysocki /**
331897badf87SRafael J. Wysocki  * set_secondary_fwnode - Change the secondary firmware node of a given device.
331997badf87SRafael J. Wysocki  * @dev: Device to handle.
332097badf87SRafael J. Wysocki  * @fwnode: New secondary firmware node of the device.
332197badf87SRafael J. Wysocki  *
332297badf87SRafael J. Wysocki  * If a primary firmware node of the device is present, set its secondary
332397badf87SRafael J. Wysocki  * pointer to @fwnode.  Otherwise, set the device's firmware node pointer to
332497badf87SRafael J. Wysocki  * @fwnode.
332597badf87SRafael J. Wysocki  */
332697badf87SRafael J. Wysocki void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
332797badf87SRafael J. Wysocki {
332897badf87SRafael J. Wysocki 	if (fwnode)
332997badf87SRafael J. Wysocki 		fwnode->secondary = ERR_PTR(-ENODEV);
333097badf87SRafael J. Wysocki 
333197badf87SRafael J. Wysocki 	if (fwnode_is_primary(dev->fwnode))
333297badf87SRafael J. Wysocki 		dev->fwnode->secondary = fwnode;
333397badf87SRafael J. Wysocki 	else
333497badf87SRafael J. Wysocki 		dev->fwnode = fwnode;
333597badf87SRafael J. Wysocki }
33364e75e1d7SJohan Hovold 
33374e75e1d7SJohan Hovold /**
33384e75e1d7SJohan Hovold  * device_set_of_node_from_dev - reuse device-tree node of another device
33394e75e1d7SJohan Hovold  * @dev: device whose device-tree node is being set
33404e75e1d7SJohan Hovold  * @dev2: device whose device-tree node is being reused
33414e75e1d7SJohan Hovold  *
33424e75e1d7SJohan Hovold  * Takes another reference to the new device-tree node after first dropping
33434e75e1d7SJohan Hovold  * any reference held to the old node.
33444e75e1d7SJohan Hovold  */
33454e75e1d7SJohan Hovold void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
33464e75e1d7SJohan Hovold {
33474e75e1d7SJohan Hovold 	of_node_put(dev->of_node);
33484e75e1d7SJohan Hovold 	dev->of_node = of_node_get(dev2->of_node);
33494e75e1d7SJohan Hovold 	dev->of_node_reused = true;
33504e75e1d7SJohan Hovold }
33514e75e1d7SJohan Hovold EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
335265b66682SSuzuki K Poulose 
33536cda08a2SSuzuki K Poulose int device_match_name(struct device *dev, const void *name)
33546cda08a2SSuzuki K Poulose {
33556cda08a2SSuzuki K Poulose 	return sysfs_streq(dev_name(dev), name);
33566cda08a2SSuzuki K Poulose }
33576cda08a2SSuzuki K Poulose EXPORT_SYMBOL_GPL(device_match_name);
33586cda08a2SSuzuki K Poulose 
335965b66682SSuzuki K Poulose int device_match_of_node(struct device *dev, const void *np)
336065b66682SSuzuki K Poulose {
336165b66682SSuzuki K Poulose 	return dev->of_node == np;
336265b66682SSuzuki K Poulose }
336365b66682SSuzuki K Poulose EXPORT_SYMBOL_GPL(device_match_of_node);
336467843bbaSSuzuki K Poulose 
336567843bbaSSuzuki K Poulose int device_match_fwnode(struct device *dev, const void *fwnode)
336667843bbaSSuzuki K Poulose {
336767843bbaSSuzuki K Poulose 	return dev_fwnode(dev) == fwnode;
336867843bbaSSuzuki K Poulose }
336967843bbaSSuzuki K Poulose EXPORT_SYMBOL_GPL(device_match_fwnode);
33704495dfddSSuzuki K Poulose 
33714495dfddSSuzuki K Poulose int device_match_devt(struct device *dev, const void *pdevt)
33724495dfddSSuzuki K Poulose {
33734495dfddSSuzuki K Poulose 	return dev->devt == *(dev_t *)pdevt;
33744495dfddSSuzuki K Poulose }
33754495dfddSSuzuki K Poulose EXPORT_SYMBOL_GPL(device_match_devt);
3376