xref: /linux-6.15/include/linux/device.h (revision 3b62449d)
1989d42e8SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * device.h - generic, centralized driver model
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (c) 2001-2003 Patrick Mochel <[email protected]>
6b4028437SGreg Kroah-Hartman  * Copyright (c) 2004-2009 Greg Kroah-Hartman <[email protected]>
7b4028437SGreg Kroah-Hartman  * Copyright (c) 2008-2009 Novell Inc.
81da177e4SLinus Torvalds  *
9fe34c89dSMauro Carvalho Chehab  * See Documentation/driver-api/driver-model/ for more information.
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #ifndef _DEVICE_H_
131da177e4SLinus Torvalds #define _DEVICE_H_
141da177e4SLinus Torvalds 
15af628aaeSGreg Kroah-Hartman #include <linux/dev_printk.h>
161bc138c6SLukasz Luba #include <linux/energy_model.h>
171da177e4SLinus Torvalds #include <linux/ioport.h>
181da177e4SLinus Torvalds #include <linux/kobject.h>
19465c7a3aS[email protected] #include <linux/klist.h>
201da177e4SLinus Torvalds #include <linux/list.h>
21d2a3b914SMatthew Wilcox #include <linux/lockdep.h>
224a7fb636SAndrew Morton #include <linux/compiler.h>
231da177e4SLinus Torvalds #include <linux/types.h>
24de477254SPaul Gortmaker #include <linux/mutex.h>
251da177e4SLinus Torvalds #include <linux/pm.h>
2660063497SArun Sharma #include <linux/atomic.h>
273c2670e6SKay Sievers #include <linux/uidgid.h>
2864c862a8SJoe Perches #include <linux/gfp.h>
292509b561SKees Cook #include <linux/device/bus.h>
305aee2bf2SGreg Kroah-Hartman #include <linux/device/class.h>
31a8ae6085SGreg Kroah-Hartman #include <linux/device/devres.h>
324c002c97SGreg Kroah-Hartman #include <linux/device/driver.h>
3354da6a09SPeter Zijlstra #include <linux/cleanup.h>
34c6dbaef2SBenjamin Herrenschmidt #include <asm/device.h>
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds struct device;
37fb069a5dSGreg Kroah-Hartman struct device_private;
381da177e4SLinus Torvalds struct device_driver;
39e5dd1278SGreg Kroah-Hartman struct driver_private;
40de477254SPaul Gortmaker struct module;
411da177e4SLinus Torvalds struct class;
426b6e39a6SKay Sievers struct subsys_private;
43d706c1b0SGrant Likely struct device_node;
44ce793486SRafael J. Wysocki struct fwnode_handle;
4574416e1eSAlex Williamson struct iommu_group;
4623c35f48SLinus Torvalds struct dev_pin_info;
47045a7042SJoerg Roedel struct dev_iommu;
48013bd8e5SThomas Gleixner struct msi_device_data;
49b8c5cec2SKay Sievers 
50880ffb5cSWanlong Gao /**
51ca22e56dSKay Sievers  * struct subsys_interface - interfaces to device functions
522eda013fSRandy Dunlap  * @name:       name of the device function
535dd5f934SBhaskar Chowdhury  * @subsys:     subsystem of the devices to attach to
542eda013fSRandy Dunlap  * @node:       the list of functions registered at the subsystem
552eda013fSRandy Dunlap  * @add_dev:    device hookup to device function handler
562eda013fSRandy Dunlap  * @remove_dev: device hookup to device function handler
57ca22e56dSKay Sievers  *
58ca22e56dSKay Sievers  * Simple interfaces attached to a subsystem. Multiple interfaces can
59ca22e56dSKay Sievers  * attach to a subsystem and its devices. Unlike drivers, they do not
60ca22e56dSKay Sievers  * exclusively claim or control devices. Interfaces usually represent
61ca22e56dSKay Sievers  * a specific functionality of a subsystem/class of devices.
62ca22e56dSKay Sievers  */
63ca22e56dSKay Sievers struct subsys_interface {
64ca22e56dSKay Sievers 	const char *name;
6532f78abeSGreg Kroah-Hartman 	const struct bus_type *subsys;
66ca22e56dSKay Sievers 	struct list_head node;
67ca22e56dSKay Sievers 	int (*add_dev)(struct device *dev, struct subsys_interface *sif);
6871db87baSViresh Kumar 	void (*remove_dev)(struct device *dev, struct subsys_interface *sif);
69ca22e56dSKay Sievers };
70ca22e56dSKay Sievers 
71ca22e56dSKay Sievers int subsys_interface_register(struct subsys_interface *sif);
72ca22e56dSKay Sievers void subsys_interface_unregister(struct subsys_interface *sif);
73ca22e56dSKay Sievers 
7432f78abeSGreg Kroah-Hartman int subsys_system_register(const struct bus_type *subsys,
75ca22e56dSKay Sievers 			   const struct attribute_group **groups);
7632f78abeSGreg Kroah-Hartman int subsys_virtual_register(const struct bus_type *subsys,
77d73ce004STejun Heo 			    const struct attribute_group **groups);
78ca22e56dSKay Sievers 
79414264f9SKay Sievers /*
80414264f9SKay Sievers  * The type of device, "struct device" is embedded in. A class
81414264f9SKay Sievers  * or bus can contain devices of different types
82414264f9SKay Sievers  * like "partitions" and "disks", "mouse" and "event".
83414264f9SKay Sievers  * This identifies the device type and carries type-specific
84414264f9SKay Sievers  * information, equivalent to the kobj_type of a kobject.
85414264f9SKay Sievers  * If "name" is specified, the uevent will contain it in
86414264f9SKay Sievers  * the DEVTYPE variable.
87414264f9SKay Sievers  */
88f9f852dfSKay Sievers struct device_type {
89414264f9SKay Sievers 	const char *name;
90a4dbd674SDavid Brownell 	const struct attribute_group **groups;
91162736b0SGreg Kroah-Hartman 	int (*uevent)(const struct device *dev, struct kobj_uevent_env *env);
92a9b12f8bSGreg Kroah-Hartman 	char *(*devnode)(const struct device *dev, umode_t *mode,
934e4098a3SGreg Kroah-Hartman 			 kuid_t *uid, kgid_t *gid);
94f9f852dfSKay Sievers 	void (*release)(struct device *dev);
951eede070SRafael J. Wysocki 
968150f32bSDmitry Torokhov 	const struct dev_pm_ops *pm;
97f9f852dfSKay Sievers };
98f9f852dfSKay Sievers 
99cd00bc2cSJames Seo /**
100cd00bc2cSJames Seo  * struct device_attribute - Interface for exporting device attributes.
101cd00bc2cSJames Seo  * @attr: sysfs attribute definition.
102cd00bc2cSJames Seo  * @show: Show handler.
103cd00bc2cSJames Seo  * @store: Store handler.
104cd00bc2cSJames Seo  */
105a7fd6706SKay Sievers struct device_attribute {
106a7fd6706SKay Sievers 	struct attribute	attr;
107a7fd6706SKay Sievers 	ssize_t (*show)(struct device *dev, struct device_attribute *attr,
108a7fd6706SKay Sievers 			char *buf);
109a7fd6706SKay Sievers 	ssize_t (*store)(struct device *dev, struct device_attribute *attr,
110a7fd6706SKay Sievers 			 const char *buf, size_t count);
111a7fd6706SKay Sievers };
112a7fd6706SKay Sievers 
113cd00bc2cSJames Seo /**
114cd00bc2cSJames Seo  * struct dev_ext_attribute - Exported device attribute with extra context.
115cd00bc2cSJames Seo  * @attr: Exported device attribute.
116cd00bc2cSJames Seo  * @var: Pointer to context.
117cd00bc2cSJames Seo  */
118ca22e56dSKay Sievers struct dev_ext_attribute {
119ca22e56dSKay Sievers 	struct device_attribute attr;
120ca22e56dSKay Sievers 	void *var;
121ca22e56dSKay Sievers };
122ca22e56dSKay Sievers 
123ca22e56dSKay Sievers ssize_t device_show_ulong(struct device *dev, struct device_attribute *attr,
124ca22e56dSKay Sievers 			  char *buf);
125ca22e56dSKay Sievers ssize_t device_store_ulong(struct device *dev, struct device_attribute *attr,
126ca22e56dSKay Sievers 			   const char *buf, size_t count);
127ca22e56dSKay Sievers ssize_t device_show_int(struct device *dev, struct device_attribute *attr,
128ca22e56dSKay Sievers 			char *buf);
129ca22e56dSKay Sievers ssize_t device_store_int(struct device *dev, struct device_attribute *attr,
130ca22e56dSKay Sievers 			 const char *buf, size_t count);
13191872392SBorislav Petkov ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
13291872392SBorislav Petkov 			char *buf);
13391872392SBorislav Petkov ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
13491872392SBorislav Petkov 			 const char *buf, size_t count);
1353cc50d07SLukas Wunner ssize_t device_show_string(struct device *dev, struct device_attribute *attr,
1363cc50d07SLukas Wunner 			   char *buf);
137ca22e56dSKay Sievers 
138cd00bc2cSJames Seo /**
139cd00bc2cSJames Seo  * DEVICE_ATTR - Define a device attribute.
140cd00bc2cSJames Seo  * @_name: Attribute name.
141cd00bc2cSJames Seo  * @_mode: File mode.
142cd00bc2cSJames Seo  * @_show: Show handler. Optional, but mandatory if attribute is readable.
143cd00bc2cSJames Seo  * @_store: Store handler. Optional, but mandatory if attribute is writable.
144cd00bc2cSJames Seo  *
145cd00bc2cSJames Seo  * Convenience macro for defining a struct device_attribute.
146cd00bc2cSJames Seo  *
147cd00bc2cSJames Seo  * For example, ``DEVICE_ATTR(foo, 0644, foo_show, foo_store);`` expands to:
148cd00bc2cSJames Seo  *
149cd00bc2cSJames Seo  * .. code-block:: c
150cd00bc2cSJames Seo  *
151cd00bc2cSJames Seo  *	struct device_attribute dev_attr_foo = {
152cd00bc2cSJames Seo  *		.attr	= { .name = "foo", .mode = 0644 },
153cd00bc2cSJames Seo  *		.show	= foo_show,
154cd00bc2cSJames Seo  *		.store	= foo_store,
155cd00bc2cSJames Seo  *	};
156cd00bc2cSJames Seo  */
157a7fd6706SKay Sievers #define DEVICE_ATTR(_name, _mode, _show, _store) \
158a7fd6706SKay Sievers 	struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
159cd00bc2cSJames Seo 
160cd00bc2cSJames Seo /**
161cd00bc2cSJames Seo  * DEVICE_ATTR_PREALLOC - Define a preallocated device attribute.
162cd00bc2cSJames Seo  * @_name: Attribute name.
163cd00bc2cSJames Seo  * @_mode: File mode.
164cd00bc2cSJames Seo  * @_show: Show handler. Optional, but mandatory if attribute is readable.
165cd00bc2cSJames Seo  * @_store: Store handler. Optional, but mandatory if attribute is writable.
166cd00bc2cSJames Seo  *
167cd00bc2cSJames Seo  * Like DEVICE_ATTR(), but ``SYSFS_PREALLOC`` is set on @_mode.
168cd00bc2cSJames Seo  */
1697fda9100SChristophe Leroy #define DEVICE_ATTR_PREALLOC(_name, _mode, _show, _store) \
1707fda9100SChristophe Leroy 	struct device_attribute dev_attr_##_name = \
1717fda9100SChristophe Leroy 		__ATTR_PREALLOC(_name, _mode, _show, _store)
172cd00bc2cSJames Seo 
173cd00bc2cSJames Seo /**
174cd00bc2cSJames Seo  * DEVICE_ATTR_RW - Define a read-write device attribute.
175cd00bc2cSJames Seo  * @_name: Attribute name.
176cd00bc2cSJames Seo  *
177cd00bc2cSJames Seo  * Like DEVICE_ATTR(), but @_mode is 0644, @_show is <_name>_show,
178cd00bc2cSJames Seo  * and @_store is <_name>_store.
179cd00bc2cSJames Seo  */
180ced321bfSGreg Kroah-Hartman #define DEVICE_ATTR_RW(_name) \
181ced321bfSGreg Kroah-Hartman 	struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
182cd00bc2cSJames Seo 
183cd00bc2cSJames Seo /**
184cd00bc2cSJames Seo  * DEVICE_ATTR_ADMIN_RW - Define an admin-only read-write device attribute.
185cd00bc2cSJames Seo  * @_name: Attribute name.
186cd00bc2cSJames Seo  *
187cd00bc2cSJames Seo  * Like DEVICE_ATTR_RW(), but @_mode is 0600.
188cd00bc2cSJames Seo  */
1893022c6a1SDan Williams #define DEVICE_ATTR_ADMIN_RW(_name) \
1903022c6a1SDan Williams 	struct device_attribute dev_attr_##_name = __ATTR_RW_MODE(_name, 0600)
191cd00bc2cSJames Seo 
192cd00bc2cSJames Seo /**
193cd00bc2cSJames Seo  * DEVICE_ATTR_RO - Define a readable device attribute.
194cd00bc2cSJames Seo  * @_name: Attribute name.
195cd00bc2cSJames Seo  *
196cd00bc2cSJames Seo  * Like DEVICE_ATTR(), but @_mode is 0444 and @_show is <_name>_show.
197cd00bc2cSJames Seo  */
198ced321bfSGreg Kroah-Hartman #define DEVICE_ATTR_RO(_name) \
199ced321bfSGreg Kroah-Hartman 	struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
200cd00bc2cSJames Seo 
201cd00bc2cSJames Seo /**
202cd00bc2cSJames Seo  * DEVICE_ATTR_ADMIN_RO - Define an admin-only readable device attribute.
203cd00bc2cSJames Seo  * @_name: Attribute name.
204cd00bc2cSJames Seo  *
205cd00bc2cSJames Seo  * Like DEVICE_ATTR_RO(), but @_mode is 0400.
206cd00bc2cSJames Seo  */
2073022c6a1SDan Williams #define DEVICE_ATTR_ADMIN_RO(_name) \
2083022c6a1SDan Williams 	struct device_attribute dev_attr_##_name = __ATTR_RO_MODE(_name, 0400)
209cd00bc2cSJames Seo 
210cd00bc2cSJames Seo /**
211cd00bc2cSJames Seo  * DEVICE_ATTR_WO - Define an admin-only writable device attribute.
212cd00bc2cSJames Seo  * @_name: Attribute name.
213cd00bc2cSJames Seo  *
214cd00bc2cSJames Seo  * Like DEVICE_ATTR(), but @_mode is 0200 and @_store is <_name>_store.
215cd00bc2cSJames Seo  */
2161130c55cSGreg Kroah-Hartman #define DEVICE_ATTR_WO(_name) \
2171130c55cSGreg Kroah-Hartman 	struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
218cd00bc2cSJames Seo 
219cd00bc2cSJames Seo /**
220cd00bc2cSJames Seo  * DEVICE_ULONG_ATTR - Define a device attribute backed by an unsigned long.
221cd00bc2cSJames Seo  * @_name: Attribute name.
222cd00bc2cSJames Seo  * @_mode: File mode.
223cd00bc2cSJames Seo  * @_var: Identifier of unsigned long.
224cd00bc2cSJames Seo  *
225cd00bc2cSJames Seo  * Like DEVICE_ATTR(), but @_show and @_store are automatically provided
226cd00bc2cSJames Seo  * such that reads and writes to the attribute from userspace affect @_var.
227cd00bc2cSJames Seo  */
228ca22e56dSKay Sievers #define DEVICE_ULONG_ATTR(_name, _mode, _var) \
229ca22e56dSKay Sievers 	struct dev_ext_attribute dev_attr_##_name = \
230ca22e56dSKay Sievers 		{ __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) }
231cd00bc2cSJames Seo 
232cd00bc2cSJames Seo /**
233cd00bc2cSJames Seo  * DEVICE_INT_ATTR - Define a device attribute backed by an int.
234cd00bc2cSJames Seo  * @_name: Attribute name.
235cd00bc2cSJames Seo  * @_mode: File mode.
236cd00bc2cSJames Seo  * @_var: Identifier of int.
237cd00bc2cSJames Seo  *
238cd00bc2cSJames Seo  * Like DEVICE_ULONG_ATTR(), but @_var is an int.
239cd00bc2cSJames Seo  */
240ca22e56dSKay Sievers #define DEVICE_INT_ATTR(_name, _mode, _var) \
241ca22e56dSKay Sievers 	struct dev_ext_attribute dev_attr_##_name = \
24294758185SMichael Davidson 		{ __ATTR(_name, _mode, device_show_int, device_store_int), &(_var) }
243cd00bc2cSJames Seo 
244cd00bc2cSJames Seo /**
245cd00bc2cSJames Seo  * DEVICE_BOOL_ATTR - Define a device attribute backed by a bool.
246cd00bc2cSJames Seo  * @_name: Attribute name.
247cd00bc2cSJames Seo  * @_mode: File mode.
248cd00bc2cSJames Seo  * @_var: Identifier of bool.
249cd00bc2cSJames Seo  *
250cd00bc2cSJames Seo  * Like DEVICE_ULONG_ATTR(), but @_var is a bool.
251cd00bc2cSJames Seo  */
25291872392SBorislav Petkov #define DEVICE_BOOL_ATTR(_name, _mode, _var) \
25391872392SBorislav Petkov 	struct dev_ext_attribute dev_attr_##_name = \
25491872392SBorislav Petkov 		{ __ATTR(_name, _mode, device_show_bool, device_store_bool), &(_var) }
255cd00bc2cSJames Seo 
2563cc50d07SLukas Wunner /**
2573cc50d07SLukas Wunner  * DEVICE_STRING_ATTR_RO - Define a device attribute backed by a r/o string.
2583cc50d07SLukas Wunner  * @_name: Attribute name.
2593cc50d07SLukas Wunner  * @_mode: File mode.
2603cc50d07SLukas Wunner  * @_var: Identifier of string.
2613cc50d07SLukas Wunner  *
2623cc50d07SLukas Wunner  * Like DEVICE_ULONG_ATTR(), but @_var is a string. Because the length of the
2633cc50d07SLukas Wunner  * string allocation is unknown, the attribute must be read-only.
2643cc50d07SLukas Wunner  */
2653cc50d07SLukas Wunner #define DEVICE_STRING_ATTR_RO(_name, _mode, _var) \
2663cc50d07SLukas Wunner 	struct dev_ext_attribute dev_attr_##_name = \
2673cc50d07SLukas Wunner 		{ __ATTR(_name, (_mode) & ~0222, device_show_string, NULL), (_var) }
2683cc50d07SLukas Wunner 
269356c05d5SAlan Stern #define DEVICE_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
270356c05d5SAlan Stern 	struct device_attribute dev_attr_##_name =		\
271356c05d5SAlan Stern 		__ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
272a7fd6706SKay Sievers 
27367dd0772SBartosz Golaszewski int device_create_file(struct device *device,
27426579ab7SPhil Carmody 		       const struct device_attribute *entry);
27567dd0772SBartosz Golaszewski void device_remove_file(struct device *dev,
27626579ab7SPhil Carmody 			const struct device_attribute *attr);
27767dd0772SBartosz Golaszewski bool device_remove_file_self(struct device *dev,
2786b0afc2aSTejun Heo 			     const struct device_attribute *attr);
27967dd0772SBartosz Golaszewski int __must_check device_create_bin_file(struct device *dev,
28066ecb92bSPhil Carmody 					const struct bin_attribute *attr);
28167dd0772SBartosz Golaszewski void device_remove_bin_file(struct device *dev,
28266ecb92bSPhil Carmody 			    const struct bin_attribute *attr);
2839ac7849eSTejun Heo 
2849ac7849eSTejun Heo /* allows to add/remove a custom action to devres stack */
2859ac7849eSTejun Heo int devm_remove_action_nowarn(struct device *dev, void (*action)(void *), void *data);
2869ac7849eSTejun Heo 
2879ac7849eSTejun Heo /**
28867dd0772SBartosz Golaszewski  * devm_remove_action() - removes previously added custom action
28948a27055SRasmus Villemoes  * @dev: Device that owns the action
2909ac7849eSTejun Heo  * @action: Function implementing the action
2917c683941SDan Williams  * @data: Pointer to data passed to @action implementation
2927c683941SDan Williams  *
2937c683941SDan Williams  * Removes instance of @action previously added by devm_add_action().
2947c683941SDan Williams  * Both action and data should match one of the existing entries.
29567dd0772SBartosz Golaszewski  */
296bddb1b90SMing Lei static inline
devm_remove_action(struct device * dev,void (* action)(void *),void * data)297bddb1b90SMing Lei void devm_remove_action(struct device *dev, void (*action)(void *), void *data)
298bddb1b90SMing Lei {
29967dd0772SBartosz Golaszewski 	WARN_ON(devm_remove_action_nowarn(dev, action, data));
30067dd0772SBartosz Golaszewski }
30167dd0772SBartosz Golaszewski 
3029ac7849eSTejun Heo void devm_release_action(struct device *dev, void (*action)(void *), void *data);
30367dd0772SBartosz Golaszewski 
3049ac7849eSTejun Heo int __devm_add_action(struct device *dev, void (*action)(void *), void *data, const char *name);
30567dd0772SBartosz Golaszewski #define devm_add_action(dev, action, data) \
3069ac7849eSTejun Heo 	__devm_add_action(dev, action, data, #action)
30767dd0772SBartosz Golaszewski 
__devm_add_action_or_reset(struct device * dev,void (* action)(void *),void * data,const char * name)3089ac7849eSTejun Heo static inline int __devm_add_action_or_reset(struct device *dev, void (*action)(void *),
30967dd0772SBartosz Golaszewski 					     void *data, const char *name)
310d926d0e4SMark Brown {
3119ac7849eSTejun Heo 	int ret;
3129ac7849eSTejun Heo 
31367dd0772SBartosz Golaszewski 	ret = __devm_add_action(dev, action, data, name);
31467dd0772SBartosz Golaszewski 	if (ret)
31567dd0772SBartosz Golaszewski 		action(data);
31667dd0772SBartosz Golaszewski 
3179ac7849eSTejun Heo 	return ret;
31864c862a8SJoe Perches }
31974c8e6bfSKees Cook #define devm_add_action_or_reset(dev, action, data) \
320f8248572SBartosz Golaszewski 	__devm_add_action_or_reset(dev, action, data, #action)
32174c8e6bfSKees Cook 
32267dd0772SBartosz Golaszewski /**
32367dd0772SBartosz Golaszewski  * devm_alloc_percpu - Resource-managed alloc_percpu
32467dd0772SBartosz Golaszewski  * @dev: Device to allocate per-cpu memory for
32567dd0772SBartosz Golaszewski  * @type: Type to allocate per-cpu memory for
32664c862a8SJoe Perches  *
32764c862a8SJoe Perches  * Managed alloc_percpu. Per-cpu memory allocated with this function is
32864c862a8SJoe Perches  * automatically freed on driver detach.
32964c862a8SJoe Perches  *
33064c862a8SJoe Perches  * RETURNS:
33164c862a8SJoe Perches  * Pointer to allocated memory on success, NULL on failure.
33264c862a8SJoe Perches  */
3332509b561SKees Cook #define devm_alloc_percpu(dev, type)      \
3342509b561SKees Cook 	((typeof(type) __percpu *)__devm_alloc_percpu((dev), sizeof(type), \
3352509b561SKees Cook 						      __alignof__(type)))
33664c862a8SJoe Perches 
3372509b561SKees Cook void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
3382509b561SKees Cook 				   size_t align);
33964c862a8SJoe Perches void devm_free_percpu(struct device *dev, void __percpu *pdata);
34064c862a8SJoe Perches 
34164c862a8SJoe Perches struct device_dma_parameters {
34264c862a8SJoe Perches 	/*
34364c862a8SJoe Perches 	 * a low level driver may set these to teach IOMMU code about
34464c862a8SJoe Perches 	 * sg limitations.
345d388f06aSJames Clark 	 */
346d388f06aSJames Clark 	unsigned int max_segment_size;
347d388f06aSJames Clark 	unsigned int min_align_mask;
348d388f06aSJames Clark 	unsigned long segment_boundary_mask;
349d388f06aSJames Clark };
350d388f06aSJames Clark 
351d388f06aSJames Clark /**
352d388f06aSJames Clark  * enum device_link_state - Device link states.
353d388f06aSJames Clark  * @DL_STATE_NONE: The presence of the drivers is not being tracked.
354d388f06aSJames Clark  * @DL_STATE_DORMANT: None of the supplier/consumer drivers is present.
355d388f06aSJames Clark  * @DL_STATE_AVAILABLE: The supplier driver is present, but the consumer is not.
35667dd0772SBartosz Golaszewski  * @DL_STATE_CONSUMER_PROBE: The consumer is probing (supplier driver present).
35767dd0772SBartosz Golaszewski  * @DL_STATE_ACTIVE: Both the supplier and consumer drivers are present.
35867dd0772SBartosz Golaszewski  * @DL_STATE_SUPPLIER_UNBIND: The supplier driver is unbinding.
35974c8e6bfSKees Cook  */
36074c8e6bfSKees Cook enum device_link_state {
3619ac7849eSTejun Heo 	DL_STATE_NONE = -1,
36267dd0772SBartosz Golaszewski 	DL_STATE_DORMANT = 0,
36343339bedSEli Billauer 	DL_STATE_AVAILABLE,
36467dd0772SBartosz Golaszewski 	DL_STATE_CONSUMER_PROBE,
3652589f188SGreg Kroah-Hartman 	DL_STATE_ACTIVE,
366da7c07b1SMark Brown 	DL_STATE_SUPPLIER_UNBIND,
367eef778c9SArnd Bergmann };
368eef778c9SArnd Bergmann 
369b873af62SBartosz Golaszewski /*
370b873af62SBartosz Golaszewski  * Device link flags.
37172f8c0bfSWolfram Sang  *
372d5e83827SBenjamin Herrenschmidt  * STATELESS: The core will not remove this link automatically.
373d5e83827SBenjamin Herrenschmidt  * AUTOREMOVE_CONSUMER: Remove the link automatically on consumer driver unbind.
374d5e83827SBenjamin Herrenschmidt  * PM_RUNTIME: If set, the runtime PM framework will use this link.
375da7c07b1SMark Brown  * RPM_ACTIVE: Run pm_runtime_get_sync() on the supplier during link creation.
376da7c07b1SMark Brown  * AUTOREMOVE_SUPPLIER: Remove the link automatically on supplier driver unbind.
377da7c07b1SMark Brown  * AUTOPROBE_CONSUMER: Probe consumer driver automatically after supplier binds.
378da7c07b1SMark Brown  * MANAGED: The core tracks presence of supplier/consumer drivers (internal).
379da7c07b1SMark Brown  * SYNC_STATE_ONLY: Link only affects sync_state() behavior.
380da7c07b1SMark Brown  * INFERRED: Inferred from data (eg: firmware) and not from driver actions.
381da7c07b1SMark Brown  */
382da7c07b1SMark Brown #define DL_FLAG_STATELESS		BIT(0)
383da7c07b1SMark Brown #define DL_FLAG_AUTOREMOVE_CONSUMER	BIT(1)
384da7c07b1SMark Brown #define DL_FLAG_PM_RUNTIME		BIT(2)
385da7c07b1SMark Brown #define DL_FLAG_RPM_ACTIVE		BIT(3)
386da7c07b1SMark Brown #define DL_FLAG_AUTOREMOVE_SUPPLIER	BIT(4)
387da7c07b1SMark Brown #define DL_FLAG_AUTOPROBE_CONSUMER	BIT(5)
388da7c07b1SMark Brown #define DL_FLAG_MANAGED			BIT(6)
389da7c07b1SMark Brown #define DL_FLAG_SYNC_STATE_ONLY		BIT(7)
390da7c07b1SMark Brown #define DL_FLAG_INFERRED		BIT(8)
391da7c07b1SMark Brown #define DL_FLAG_CYCLE			BIT(9)
392da7c07b1SMark Brown 
393da7c07b1SMark Brown /**
394da7c07b1SMark Brown  * enum dl_dev_state - Device driver presence tracking information.
395da7c07b1SMark Brown  * @DL_DEV_NO_DRIVER: There is no driver attached to the device.
396da7c07b1SMark Brown  * @DL_DEV_PROBING: A driver is probing.
397da7c07b1SMark Brown  * @DL_DEV_DRIVER_BOUND: The driver has been bound to the device.
398da7c07b1SMark Brown  * @DL_DEV_UNBINDING: The driver is unbinding from the device.
399da7c07b1SMark Brown  */
400d5e83827SBenjamin Herrenschmidt enum dl_dev_state {
401d6b0c580SDmitry Torokhov 	DL_DEV_NO_DRIVER = 0,
402f1725160SDanilo Krummrich 	DL_DEV_PROBING,
403f1725160SDanilo Krummrich 	DL_DEV_DRIVER_BOUND,
404f1725160SDanilo Krummrich 	DL_DEV_UNBINDING,
405f1725160SDanilo Krummrich };
406f1725160SDanilo Krummrich 
407f1725160SDanilo Krummrich /**
408f1725160SDanilo Krummrich  * enum device_removable - Whether the device is removable. The criteria for a
409f1725160SDanilo Krummrich  * device to be classified as removable is determined by its subsystem or bus.
410f1725160SDanilo Krummrich  * @DEVICE_REMOVABLE_NOT_SUPPORTED: This attribute is not supported for this
411f1725160SDanilo Krummrich  *				    device (default).
412f1725160SDanilo Krummrich  * @DEVICE_REMOVABLE_UNKNOWN:  Device location is Unknown.
413f1725160SDanilo Krummrich  * @DEVICE_FIXED: Device is not removable by the user.
414f1725160SDanilo Krummrich  * @DEVICE_REMOVABLE: Device is removable by the user.
415f1725160SDanilo Krummrich  */
416f1725160SDanilo Krummrich enum device_removable {
417f1725160SDanilo Krummrich 	DEVICE_REMOVABLE_NOT_SUPPORTED = 0, /* must be 0 */
418f1725160SDanilo Krummrich 	DEVICE_REMOVABLE_UNKNOWN,
4192374b682SDan Williams 	DEVICE_FIXED,
420d6b0c580SDmitry Torokhov 	DEVICE_REMOVABLE,
4210433686cSAndy Shevchenko };
422a083c755SMasahiro Yamada 
423a083c755SMasahiro Yamada /**
4240433686cSAndy Shevchenko  * struct dev_links_info - Device data related to device links.
425410e7088SAndy Shevchenko  * @suppliers: List of links to supplier devices.
426410e7088SAndy Shevchenko  * @consumers: List of links to consumer devices.
427a3499e9bSSudip Mukherjee  * @defer_sync: Hook to global list of devices that have deferred sync_state.
428a3499e9bSSudip Mukherjee  * @status: Driver status information.
429a3499e9bSSudip Mukherjee  */
430410e7088SAndy Shevchenko struct dev_links_info {
431a3499e9bSSudip Mukherjee 	struct list_head suppliers;
432a3499e9bSSudip Mukherjee 	struct list_head consumers;
433a3499e9bSSudip Mukherjee 	struct list_head defer_sync;
434a3499e9bSSudip Mukherjee 	enum dl_dev_state status;
435a3499e9bSSudip Mukherjee };
436a083c755SMasahiro Yamada 
437a083c755SMasahiro Yamada /**
438a3499e9bSSudip Mukherjee  * struct dev_msi_info - Device data related to MSI
439ff86aae3SMadalin Bucur  * @domain:	The MSI interrupt domain associated to the device
440ff86aae3SMadalin Bucur  * @data:	Pointer to MSI device data
441ff86aae3SMadalin Bucur  */
442ff86aae3SMadalin Bucur struct dev_msi_info {
443ff86aae3SMadalin Bucur #ifdef CONFIG_GENERIC_MSI_IRQ
444ff86aae3SMadalin Bucur 	struct irq_domain	*domain;
445ff86aae3SMadalin Bucur 	struct msi_device_data	*data;
446ff86aae3SMadalin Bucur #endif
447ff86aae3SMadalin Bucur };
448ff86aae3SMadalin Bucur 
449ff86aae3SMadalin Bucur /**
450ff86aae3SMadalin Bucur  * enum device_physical_location_panel - Describes which panel surface of the
451ff86aae3SMadalin Bucur  * system's housing the device connection point resides on.
452ff86aae3SMadalin Bucur  * @DEVICE_PANEL_TOP: Device connection point is on the top panel.
453ff86aae3SMadalin Bucur  * @DEVICE_PANEL_BOTTOM: Device connection point is on the bottom panel.
454ff86aae3SMadalin Bucur  * @DEVICE_PANEL_LEFT: Device connection point is on the left panel.
455ff86aae3SMadalin Bucur  * @DEVICE_PANEL_RIGHT: Device connection point is on the right panel.
456ff86aae3SMadalin Bucur  * @DEVICE_PANEL_FRONT: Device connection point is on the front panel.
457ff86aae3SMadalin Bucur  * @DEVICE_PANEL_BACK: Device connection point is on the back panel.
4586b7b6510SFUJITA Tomonori  * @DEVICE_PANEL_UNKNOWN: The panel with device connection point is unknown.
4596b7b6510SFUJITA Tomonori  */
4606b7b6510SFUJITA Tomonori enum device_physical_location_panel {
4616b7b6510SFUJITA Tomonori 	DEVICE_PANEL_TOP,
4626b7b6510SFUJITA Tomonori 	DEVICE_PANEL_BOTTOM,
4636b7b6510SFUJITA Tomonori 	DEVICE_PANEL_LEFT,
46436950f2dSJianxiong Gao 	DEVICE_PANEL_RIGHT,
4656b7b6510SFUJITA Tomonori 	DEVICE_PANEL_FRONT,
4666b7b6510SFUJITA Tomonori 	DEVICE_PANEL_BACK,
4676b7b6510SFUJITA Tomonori 	DEVICE_PANEL_UNKNOWN,
468cd7753d3SHeikki Krogerus };
4699ed98953SRafael J. Wysocki 
4709ed98953SRafael J. Wysocki /**
4719ed98953SRafael J. Wysocki  * enum device_physical_location_vertical_position - Describes vertical
4729ed98953SRafael J. Wysocki  * position of the device connection point on the panel surface.
4739ed98953SRafael J. Wysocki  * @DEVICE_VERT_POS_UPPER: Device connection point is at upper part of panel.
4749ed98953SRafael J. Wysocki  * @DEVICE_VERT_POS_CENTER: Device connection point is at center part of panel.
4759ed98953SRafael J. Wysocki  * @DEVICE_VERT_POS_LOWER: Device connection point is at lower part of panel.
4769ed98953SRafael J. Wysocki  */
4779ed98953SRafael J. Wysocki enum device_physical_location_vertical_position {
4789ed98953SRafael J. Wysocki 	DEVICE_VERT_POS_UPPER,
4799ed98953SRafael J. Wysocki 	DEVICE_VERT_POS_CENTER,
4809ed98953SRafael J. Wysocki 	DEVICE_VERT_POS_LOWER,
4819ed98953SRafael J. Wysocki };
4829ed98953SRafael J. Wysocki 
4839ed98953SRafael J. Wysocki /**
4849ed98953SRafael J. Wysocki  * enum device_physical_location_horizontal_position - Describes horizontal
4859ed98953SRafael J. Wysocki  * position of the device connection point on the panel surface.
4869ed98953SRafael J. Wysocki  * @DEVICE_HORI_POS_LEFT: Device connection point is at left part of panel.
4879ed98953SRafael J. Wysocki  * @DEVICE_HORI_POS_CENTER: Device connection point is at center part of panel.
4889ed98953SRafael J. Wysocki  * @DEVICE_HORI_POS_RIGHT: Device connection point is at right part of panel.
489515db266SRafael J. Wysocki  */
490e88728f4SVivek Gautam enum device_physical_location_horizontal_position {
49121d5c57bSRafael J. Wysocki 	DEVICE_HORI_POS_LEFT,
49221d5c57bSRafael J. Wysocki 	DEVICE_HORI_POS_CENTER,
4931689cac5SVivek Gautam 	DEVICE_HORI_POS_RIGHT,
494e7dd4010SRafael J. Wysocki };
495515db266SRafael J. Wysocki 
49605ef983eSSaravana Kannan /**
4974b9bbb29SSaravana Kannan  * struct device_physical_location - Device data related to physical location
4989ed98953SRafael J. Wysocki  * of the device connection point.
4999ed98953SRafael J. Wysocki  * @panel: Panel surface of the system's housing that the device connection
500e88728f4SVivek Gautam  *         point resides on.
50121d5c57bSRafael J. Wysocki  * @vertical_position: Vertical position of the device connection point within
50221d5c57bSRafael J. Wysocki  *                     the panel.
5031689cac5SVivek Gautam  * @horizontal_position: Horizontal position of the device connection point
504e7dd4010SRafael J. Wysocki  *                       within the panel.
505515db266SRafael J. Wysocki  * @dock: Set if the device connection point resides in a docking station or
50605ef983eSSaravana Kannan  *        port replicator.
5074b9bbb29SSaravana Kannan  * @lid: Set if this device connection point resides on the lid of laptop
50867cad5c6SSaravana Kannan  *       system.
5099ed98953SRafael J. Wysocki  */
5109ed98953SRafael J. Wysocki struct device_physical_location {
5119ed98953SRafael J. Wysocki 	enum device_physical_location_panel panel;
5129ed98953SRafael J. Wysocki 	enum device_physical_location_vertical_position vertical_position;
5139ed98953SRafael J. Wysocki 	enum device_physical_location_horizontal_position horizontal_position;
5149ed98953SRafael J. Wysocki 	bool dock;
5159ed98953SRafael J. Wysocki 	bool lid;
5169ed98953SRafael J. Wysocki };
5179ed98953SRafael J. Wysocki 
5189ed98953SRafael J. Wysocki /**
5199ed98953SRafael J. Wysocki  * struct device - The basic device structure
5209ed98953SRafael J. Wysocki  * @parent:	The device's "parent" device, the device to which it is attached.
5219ed98953SRafael J. Wysocki  * 		In most cases, a parent device is some sort of bus or host
5229ed98953SRafael J. Wysocki  * 		controller. If parent is NULL, the device, is a top-level device,
5239ed98953SRafael J. Wysocki  * 		which is not usually what you want.
5249ed98953SRafael J. Wysocki  * @p:		Holds the private data of the driver core portions of the device.
52570f400d4SRajat Jain  * 		See the comment of the struct device_private for detail.
52670f400d4SRajat Jain  * @kobj:	A top-level, abstract class from which other classes are derived.
52770f400d4SRajat Jain  * @init_name:	Initial name of the device.
52870f400d4SRajat Jain  * @type:	The type of device.
52970f400d4SRajat Jain  * 		This identifies the device type and carries type-specific
53070f400d4SRajat Jain  * 		information.
53170f400d4SRajat Jain  * @mutex:	Mutex to synchronize calls to its driver.
53270f400d4SRajat Jain  * @bus:	Type of bus device is on.
53370f400d4SRajat Jain  * @driver:	Which driver has allocated this
53470f400d4SRajat Jain  * @platform_data: Platform data specific to the device.
53570f400d4SRajat Jain  * 		Example: For devices on custom boards, as typical of embedded
53670f400d4SRajat Jain  * 		and SOC based hardware, Linux often uses platform_data to point
53770f400d4SRajat Jain  * 		to board-specific structures describing devices and how they
53870f400d4SRajat Jain  * 		are wired.  That can include what ports are available, chip
53970f400d4SRajat Jain  * 		variants, which GPIO pins act in what additional roles, and so
54070f400d4SRajat Jain  * 		on.  This shrinks the "Board Support Packages" (BSPs) and
5419ed98953SRafael J. Wysocki  * 		minimizes board-specific #ifdefs in drivers.
5429ed98953SRafael J. Wysocki  * @driver_data: Private pointer for driver specific info.
5439ed98953SRafael J. Wysocki  * @links:	Links to suppliers and consumers of this device.
5443b052a3eSSaravana Kannan  * @power:	For device power management.
5459ed98953SRafael J. Wysocki  *		See Documentation/driver-api/pm/devices.rst for details.
5469ed98953SRafael J. Wysocki  * @pm_domain:	Provide callbacks that are executed during system suspend,
5479ed98953SRafael J. Wysocki  * 		hibernation, system resume and during runtime PM transitions
5489ed98953SRafael J. Wysocki  * 		along with subsystem-level and driver-level callbacks.
5499ed98953SRafael J. Wysocki  * @em_pd:	device's energy model performance domain
5503b052a3eSSaravana Kannan  * @pins:	For device pin management.
5519ed98953SRafael J. Wysocki  *		See Documentation/driver-api/pin-control.rst for details.
5529ed98953SRafael J. Wysocki  * @msi:	MSI related data
5539ed98953SRafael J. Wysocki  * @numa_node:	NUMA node this device is close to.
5549ed98953SRafael J. Wysocki  * @dma_ops:    DMA mapping operations for this device.
55534fff628SThomas Gleixner  * @dma_mask:	Dma mask (if dma'ble device).
55634fff628SThomas Gleixner  * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all
557013bd8e5SThomas Gleixner  * 		hardware supports 64-bit addresses for consistent allocations
55834fff628SThomas Gleixner  * 		such descriptors.
55934fff628SThomas Gleixner  * @bus_dma_limit: Limit of an upstream bridge or bus which imposes a smaller
560013bd8e5SThomas Gleixner  *		DMA limit than the device itself supports.
56113e7accbSThomas Gleixner  * @dma_range_map: map for DMA memory ranges relative to that of RAM
562013bd8e5SThomas Gleixner  * @dma_parms:	A low level driver may set these to teach IOMMU code about
563013bd8e5SThomas Gleixner  * 		segment limitations.
56434fff628SThomas Gleixner  * @dma_pools:	Dma pools (if dma'ble device).
56534fff628SThomas Gleixner  * @dma_mem:	Internal for coherent mem override.
56634fff628SThomas Gleixner  * @cma_area:	Contiguous memory area for dma allocations
5676423d295SWon Chung  * @dma_io_tlb_mem: Software IO TLB allocator.  Not for driver use.
5686423d295SWon Chung  * @dma_io_tlb_pools:	List of transient swiotlb memory pools.
5696423d295SWon Chung  * @dma_io_tlb_lock:	Protects changes to the list of active pools.
5706423d295SWon Chung  * @dma_uses_io_tlb: %true if device has used the software IO TLB.
5716423d295SWon Chung  * @archdata:	For arch-specific additions.
5726423d295SWon Chung  * @of_node:	Associated device tree node.
5736423d295SWon Chung  * @fwnode:	Associated device node supplied by platform firmware.
5746423d295SWon Chung  * @devt:	For creating the sysfs "dev".
5756423d295SWon Chung  * @id:		device instance
5766423d295SWon Chung  * @devres_lock: Spinlock to protect the resource of the device.
5776423d295SWon Chung  * @devres_head: The resources list of the device.
5786423d295SWon Chung  * @class:	The class of the device.
5796423d295SWon Chung  * @groups:	Optional attribute groups.
5806423d295SWon Chung  * @release:	Callback to free the device after all references have
5816423d295SWon Chung  * 		gone away. This should be set by the allocator of the
5826423d295SWon Chung  * 		device (i.e. the bus driver that discovered the device).
5836423d295SWon Chung  * @iommu_group: IOMMU group the device belongs to.
5846423d295SWon Chung  * @iommu:	Per device generic IOMMU runtime data
5856423d295SWon Chung  * @physical_location: Describes physical location of the device connection
5866423d295SWon Chung  *		point in the system housing.
5876423d295SWon Chung  * @removable:  Whether the device can be removed from the system. This
5886423d295SWon Chung  *              should be set by the subsystem / bus driver that discovered
5896423d295SWon Chung  *              the device.
5906423d295SWon Chung  *
5916423d295SWon Chung  * @offline_disabled: If set, the device is permanently online.
5926423d295SWon Chung  * @offline:	Set after successful invocation of bus type's .offline().
5936423d295SWon Chung  * @of_node_reused: Set if the device-tree node is shared with an ancestor
5946423d295SWon Chung  *              device.
5956423d295SWon Chung  * @state_synced: The hardware state of this device has been synced to match
5966423d295SWon Chung  *		  the software state of this device by calling the driver/bus
5976423d295SWon Chung  *		  sync_state() callback.
5986423d295SWon Chung  * @can_match:	The device has matched with a driver at least once or it is in
5996423d295SWon Chung  *		a bus (like AMBA) which can't check for matching drivers until
6006423d295SWon Chung  *		other devices probe successfully.
6016423d295SWon Chung  * @dma_coherent: this particular device is dma coherent, even if the
6026423d295SWon Chung  *		architecture supports non-coherent devices.
6036423d295SWon Chung  * @dma_ops_bypass: If set to %true then the dma_ops are bypassed for the
6046423d295SWon Chung  *		streaming DMA operations (->map_* / ->unmap_* / ->sync_*),
6056423d295SWon Chung  *		and optionall (if the coherent mask is large enough) also
6066423d295SWon Chung  *		for dma allocations.  This flag is managed by the dma ops
6076423d295SWon Chung  *		instance from ->dma_supported.
6086423d295SWon Chung  * @dma_skip_sync: DMA sync operations can be skipped for coherent buffers.
6096423d295SWon Chung  * @dma_iommu: Device is using default IOMMU implementation for DMA and
6106423d295SWon Chung  *		doesn't rely on dma_ops structure.
6116423d295SWon Chung  *
6126423d295SWon Chung  * At the lowest level, every device in a Linux system is represented by an
6136423d295SWon Chung  * instance of struct device. The device structure contains the information
6146423d295SWon Chung  * that the device model core needs to model the system. Most subsystems,
6156423d295SWon Chung  * however, track additional information about the devices they host. As a
6166423d295SWon Chung  * result, it is rare for devices to be represented by bare device structures;
6176423d295SWon Chung  * instead, that structure, like kobject structures, is usually embedded within
6186423d295SWon Chung  * a higher-level representation of the device.
6196423d295SWon Chung  */
6206423d295SWon Chung struct device {
6216423d295SWon Chung 	struct kobject kobj;
6226423d295SWon Chung 	struct device		*parent;
6236423d295SWon Chung 
6246423d295SWon Chung 	struct device_private	*p;
6256423d295SWon Chung 
6266423d295SWon Chung 	const char		*init_name; /* initial name of the device */
6276423d295SWon Chung 	const struct device_type *type;
6286423d295SWon Chung 
6296423d295SWon Chung 	const struct bus_type	*bus;	/* type of bus device is on */
6306423d295SWon Chung 	struct device_driver *driver;	/* which driver has allocated this
6316423d295SWon Chung 					   device */
6326423d295SWon Chung 	void		*platform_data;	/* Platform specific data, device
6336423d295SWon Chung 					   core doesn't touch it */
6346423d295SWon Chung 	void		*driver_data;	/* Driver data, set and get with
6356423d295SWon Chung 					   dev_set_drvdata/dev_get_drvdata */
636880ffb5cSWanlong Gao 	struct mutex		mutex;	/* mutex to synchronize calls to
637880ffb5cSWanlong Gao 					 * its driver.
638880ffb5cSWanlong Gao 					 */
639880ffb5cSWanlong Gao 
640880ffb5cSWanlong Gao 	struct dev_links_info	links;
641880ffb5cSWanlong Gao 	struct dev_pm_info	power;
642880ffb5cSWanlong Gao 	struct dev_pm_domain	*pm_domain;
643880ffb5cSWanlong Gao 
644880ffb5cSWanlong Gao #ifdef CONFIG_ENERGY_MODEL
645880ffb5cSWanlong Gao 	struct em_perf_domain	*em_pd;
646880ffb5cSWanlong Gao #endif
647880ffb5cSWanlong Gao 
648880ffb5cSWanlong Gao #ifdef CONFIG_PINCTRL
649880ffb5cSWanlong Gao 	struct dev_pin_info	*pins;
650880ffb5cSWanlong Gao #endif
651880ffb5cSWanlong Gao 	struct dev_msi_info	msi;
652880ffb5cSWanlong Gao #ifdef CONFIG_ARCH_HAS_DMA_OPS
653880ffb5cSWanlong Gao 	const struct dma_map_ops *dma_ops;
654880ffb5cSWanlong Gao #endif
655880ffb5cSWanlong Gao 	u64		*dma_mask;	/* dma mask (if dma'able device) */
656880ffb5cSWanlong Gao 	u64		coherent_dma_mask;/* Like dma_mask, but for
657880ffb5cSWanlong Gao 					     alloc_coherent mappings as
658880ffb5cSWanlong Gao 					     not all hardware supports
6591bb6c08aSJean Delvare 					     64 bit addresses for consistent
66064df1148SLukas Wunner 					     allocations such descriptors. */
661880ffb5cSWanlong Gao 	u64		bus_dma_limit;	/* upstream dma constraint */
66274378c5cSGeert Uytterhoeven 	const struct bus_dma_region *dma_range_map;
663564b905aSRafael J. Wysocki 
664880ffb5cSWanlong Gao 	struct device_dma_parameters *dma_parms;
665880ffb5cSWanlong Gao 
66695035eacSRandy Dunlap 	struct list_head	dma_pools;	/* dma pools (if dma'ble) */
667ab78029eSLinus Walleij 
6684b0c9948SMauro Carvalho Chehab #ifdef CONFIG_DMA_DECLARE_COHERENT
66934fff628SThomas Gleixner 	struct dma_coherent_mem	*dma_mem; /* internal for coherent mem
670880ffb5cSWanlong Gao 					     override */
6716a7a8176SJonathan Corbet #endif
672880ffb5cSWanlong Gao #ifdef CONFIG_DMA_CMA
673880ffb5cSWanlong Gao 	struct cma *cma_area;		/* contiguous memory area for dma
674880ffb5cSWanlong Gao 					   allocations */
675880ffb5cSWanlong Gao #endif
676a7ba70f1SNicolas Saenz Julienne #ifdef CONFIG_SWIOTLB
677a7ba70f1SNicolas Saenz Julienne 	struct io_tlb_mem *dma_io_tlb_mem;
678e0d07278SJim Quinlan #endif
679880ffb5cSWanlong Gao #ifdef CONFIG_SWIOTLB_DYNAMIC
680880ffb5cSWanlong Gao 	struct list_head dma_io_tlb_pools;
681880ffb5cSWanlong Gao 	spinlock_t dma_io_tlb_lock;
682880ffb5cSWanlong Gao 	bool dma_uses_io_tlb;
683bfd63cd2SMichael Opdenacker #endif
684158dbe9cSPetr Tesarik 	/* arch specific additions */
68579636caaSPetr Tesarik 	struct dev_archdata	archdata;
68679636caaSPetr Tesarik 
6871395706aSPetr Tesarik 	struct device_node	*of_node; /* associated device tree node */
688880ffb5cSWanlong Gao 	struct fwnode_handle	*fwnode; /* firmware device node */
689880ffb5cSWanlong Gao 
690ce793486SRafael J. Wysocki #ifdef CONFIG_NUMA
691880ffb5cSWanlong Gao 	int		numa_node;	/* NUMA node this device is close to */
6922eda013fSRandy Dunlap #endif
693880ffb5cSWanlong Gao 	dev_t			devt;	/* dev_t, creates the sysfs "dev" */
694880ffb5cSWanlong Gao 	u32			id;	/* device instance */
695880ffb5cSWanlong Gao 
696880ffb5cSWanlong Gao 	spinlock_t		devres_lock;
697880ffb5cSWanlong Gao 	struct list_head	devres_head;
698880ffb5cSWanlong Gao 
699880ffb5cSWanlong Gao 	const struct class	*class;
700bfd63cd2SMichael Opdenacker 	const struct attribute_group **groups;	/* optional groups */
701045a7042SJoerg Roedel 
7026423d295SWon Chung 	void	(*release)(struct device *dev);
7036423d295SWon Chung 	struct iommu_group	*iommu_group;
70470f400d4SRajat Jain 	struct dev_iommu	*iommu;
70570f400d4SRajat Jain 
70670f400d4SRajat Jain 	struct device_physical_location *physical_location;
707880ffb5cSWanlong Gao 
7084f3549d7SRafael J. Wysocki 	enum device_removable	removable;
7094f3549d7SRafael J. Wysocki 
7104e75e1d7SJohan Hovold 	bool			offline_disabled:1;
7114e75e1d7SJohan Hovold 	bool			offline:1;
712fc5a251dSSaravana Kannan 	bool			of_node_reused:1;
713fc5a251dSSaravana Kannan 	bool			state_synced:1;
714fc5a251dSSaravana Kannan 	bool			can_match:1;
715f2db85b6SSaravana Kannan #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
716f2db85b6SSaravana Kannan     defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
717f2db85b6SSaravana Kannan     defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
718f3ecc0ffSChristoph Hellwig 	bool			dma_coherent:1;
719f3ecc0ffSChristoph Hellwig #endif
720d35834c6SChristoph Hellwig #ifdef CONFIG_DMA_OPS_BYPASS
721d35834c6SChristoph Hellwig 	bool			dma_ops_bypass : 1;
722d35834c6SChristoph Hellwig #endif
723d35834c6SChristoph Hellwig #ifdef CONFIG_DMA_NEED_SYNC
724d35834c6SChristoph Hellwig 	bool			dma_skip_sync:1;
725a6016aacSAlexander Lobakin #endif
726b5c58b2fSLeon Romanovsky #ifdef CONFIG_IOMMU_DMA
727b5c58b2fSLeon Romanovsky 	bool			dma_iommu:1;
728880ffb5cSWanlong Gao #endif
729880ffb5cSWanlong Gao };
730880ffb5cSWanlong Gao 
731880ffb5cSWanlong Gao /**
732880ffb5cSWanlong Gao  * struct device_link - Device link representation.
733880ffb5cSWanlong Gao  * @supplier: The device on the supplier end of the link.
734880ffb5cSWanlong Gao  * @s_node: Hook to the supplier device's list of links to consumers.
735880ffb5cSWanlong Gao  * @consumer: The device on the consumer end of the link.
736880ffb5cSWanlong Gao  * @c_node: Hook to the consumer device's list of links to suppliers.
7371da177e4SLinus Torvalds  * @link_dev: device used to expose link details in sysfs
738159ef31eSGreg Kroah-Hartman  * @status: The state of the link (with respect to the presence of drivers).
7391da177e4SLinus Torvalds  * @flags: Link flags.
7401da177e4SLinus Torvalds  * @rpm_active: Whether or not the consumer device is runtime-PM-active.
741fb069a5dSGreg Kroah-Hartman  * @kref: Count repeated addition of the same link.
742fb069a5dSGreg Kroah-Hartman  * @rm_work: Work structure used for removing the link.
743c906a48aSGreg Kroah-Hartman  * @supplier_preactivated: Supplier has been made active before consumer probe.
744aed65af1SStephen Hemminger  */
7451da177e4SLinus Torvalds struct device_link {
746d492cc25SGreg Kroah-Hartman 	struct device *supplier;
7471da177e4SLinus Torvalds 	struct list_head s_node;
7481da177e4SLinus Torvalds 	struct device *consumer;
749e67c8562SGreg Kroah-Hartman 	struct list_head c_node;
750e67c8562SGreg Kroah-Hartman 	struct device link_dev;
7511bb6c08aSJean Delvare 	enum device_link_state status;
7522c6f4fc8SDavid Engraf 	u32 flags;
753159ef31eSGreg Kroah-Hartman 	refcount_t rpm_active;
754159ef31eSGreg Kroah-Hartman 	struct kref kref;
755159ef31eSGreg Kroah-Hartman 	struct work_struct rm_work;
756159ef31eSGreg Kroah-Hartman 	bool supplier_preactivated; /* Owned by consumer probe. */
7579ed98953SRafael J. Wysocki };
7581da177e4SLinus Torvalds 
759564b905aSRafael J. Wysocki #define kobj_to_dev(__kobj)	container_of_const(__kobj, struct device, kobj)
7601da177e4SLinus Torvalds 
7611bc138c6SLukasz Luba /**
7621bc138c6SLukasz Luba  * device_iommu_mapped - Returns true when the device DMA is translated
7631bc138c6SLukasz Luba  *			 by an IOMMU
7641bc138c6SLukasz Luba  * @dev: Device to perform the check on
765ab78029eSLinus Walleij  */
device_iommu_mapped(struct device * dev)766ab78029eSLinus Walleij static inline bool device_iommu_mapped(struct device *dev)
767ab78029eSLinus Walleij {
76834fff628SThomas Gleixner 	return (dev->iommu_group != NULL);
769de6c85bfSChristoph Hellwig }
7705657933dSBart Van Assche 
7712f9237d4SChristoph Hellwig /* Get the wakeup routines, which depend on struct device */
7721da177e4SLinus Torvalds #include <linux/pm_wakeup.h>
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds /**
7751da177e4SLinus Torvalds  * dev_name - Return a device's name.
7761da177e4SLinus Torvalds  * @dev: Device with name to get.
7771da177e4SLinus Torvalds  * Return: The kobject name of the device, or its initial name if unavailable.
778a7ba70f1SNicolas Saenz Julienne  */
dev_name(const struct device * dev)779e0d07278SJim Quinlan static inline const char *dev_name(const struct device *dev)
7801da177e4SLinus Torvalds {
7816b7b6510SFUJITA Tomonori 	/* Use the init name until the kobject becomes available */
7826b7b6510SFUJITA Tomonori 	if (dev->init_name)
7831da177e4SLinus Torvalds 		return dev->init_name;
7841da177e4SLinus Torvalds 
785ff4c25f2SChristoph Hellwig 	return kobject_name(&dev->kobj);
7861da177e4SLinus Torvalds }
7871da177e4SLinus Torvalds 
7882b281296SChristoph Hellwig /**
789a2547380SMarek Szyprowski  * dev_bus_name - Return a device's bus/class name, if at all possible
790c64be2bbSMarek Szyprowski  * @dev: struct device to get the bus/class name of
791c64be2bbSMarek Szyprowski  *
792c64be2bbSMarek Szyprowski  * Will return the name of the bus/class the device is attached to.  If it is
79369031f50SClaire Chang  * not attached to a bus/class, an empty string will be returned.
79469031f50SClaire Chang  */
dev_bus_name(const struct device * dev)79569031f50SClaire Chang static inline const char *dev_bus_name(const struct device *dev)
79679636caaSPetr Tesarik {
79779636caaSPetr Tesarik 	return dev->bus ? dev->bus->name : (dev->class ? dev->class->name : "");
79879636caaSPetr Tesarik }
7991395706aSPetr Tesarik 
80079636caaSPetr Tesarik __printf(2, 3) int dev_set_name(struct device *dev, const char *name, ...);
801c6dbaef2SBenjamin Herrenschmidt 
802c6dbaef2SBenjamin Herrenschmidt #ifdef CONFIG_NUMA
dev_to_node(struct device * dev)803c9e358dfSGrant Likely static inline int dev_to_node(struct device *dev)
804c9e358dfSGrant Likely {
805ce793486SRafael J. Wysocki 	return dev->numa_node;
8061da177e4SLinus Torvalds }
set_dev_node(struct device * dev,int node)807159ef31eSGreg Kroah-Hartman static inline void set_dev_node(struct device *dev, int node)
808159ef31eSGreg Kroah-Hartman {
809159ef31eSGreg Kroah-Hartman 	dev->numa_node = node;
810929d2fa5SMatthew Wilcox }
811ca22e56dSKay Sievers #else
dev_to_node(struct device * dev)812929d2fa5SMatthew Wilcox static inline int dev_to_node(struct device *dev)
8139ac7849eSTejun Heo {
8149ac7849eSTejun Heo 	return NUMA_NO_NODE;
8159ac7849eSTejun Heo }
set_dev_node(struct device * dev,int node)8169fa120fbSGreg Kroah-Hartman static inline void set_dev_node(struct device *dev, int node)
817a4dbd674SDavid Brownell {
81823681e47SGreg Kroah-Hartman }
8191da177e4SLinus Torvalds #endif
82074416e1eSAlex Williamson 
dev_get_msi_domain(const struct device * dev)821045a7042SJoerg Roedel static inline struct irq_domain *dev_get_msi_domain(const struct device *dev)
8224f3549d7SRafael J. Wysocki {
8236423d295SWon Chung #ifdef CONFIG_GENERIC_MSI_IRQ
8246423d295SWon Chung 	return dev->msi.domain;
82570f400d4SRajat Jain #else
82670f400d4SRajat Jain 	return NULL;
8274f3549d7SRafael J. Wysocki #endif
8284f3549d7SRafael J. Wysocki }
8294e75e1d7SJohan Hovold 
dev_set_msi_domain(struct device * dev,struct irq_domain * d)830fc5a251dSSaravana Kannan static inline void dev_set_msi_domain(struct device *dev, struct irq_domain *d)
831f2db85b6SSaravana Kannan {
832f3ecc0ffSChristoph Hellwig #ifdef CONFIG_GENERIC_MSI_IRQ
833f3ecc0ffSChristoph Hellwig 	dev->msi.domain = d;
834f3ecc0ffSChristoph Hellwig #endif
835f3ecc0ffSChristoph Hellwig }
836f3ecc0ffSChristoph Hellwig 
dev_get_drvdata(const struct device * dev)837d35834c6SChristoph Hellwig static inline void *dev_get_drvdata(const struct device *dev)
838d35834c6SChristoph Hellwig {
839d35834c6SChristoph Hellwig 	return dev->driver_data;
840f406c8e4SAlexander Lobakin }
841a6016aacSAlexander Lobakin 
dev_set_drvdata(struct device * dev,void * data)842f406c8e4SAlexander Lobakin static inline void dev_set_drvdata(struct device *dev, void *data)
843b5c58b2fSLeon Romanovsky {
844b5c58b2fSLeon Romanovsky 	dev->driver_data = data;
845b5c58b2fSLeon Romanovsky }
8461da177e4SLinus Torvalds 
dev_to_psd(struct device * dev)8471da177e4SLinus Torvalds static inline struct pm_subsys_data *dev_to_psd(struct device *dev)
848287905e6SSaravana Kannan {
849287905e6SSaravana Kannan 	return dev ? dev->power.subsys_data : NULL;
850287905e6SSaravana Kannan }
851287905e6SSaravana Kannan 
dev_get_uevent_suppress(const struct device * dev)852287905e6SSaravana Kannan static inline unsigned int dev_get_uevent_suppress(const struct device *dev)
853287905e6SSaravana Kannan {
854287905e6SSaravana Kannan 	return dev->kobj.uevent_suppress;
855287905e6SSaravana Kannan }
856287905e6SSaravana Kannan 
dev_set_uevent_suppress(struct device * dev,int val)857287905e6SSaravana Kannan static inline void dev_set_uevent_suppress(struct device *dev, int val)
858287905e6SSaravana Kannan {
85980dd33cfSRafael J. Wysocki 	dev->kobj.uevent_suppress = val;
860287905e6SSaravana Kannan }
861287905e6SSaravana Kannan 
device_is_registered(struct device * dev)862287905e6SSaravana Kannan static inline int device_is_registered(struct device *dev)
863287905e6SSaravana Kannan {
864287905e6SSaravana Kannan 	return dev->kobj.state_in_sysfs;
865287905e6SSaravana Kannan }
866287905e6SSaravana Kannan 
device_enable_async_suspend(struct device * dev)867287905e6SSaravana Kannan static inline void device_enable_async_suspend(struct device *dev)
868287905e6SSaravana Kannan {
869287905e6SSaravana Kannan 	if (!dev->power.is_prepared)
870287905e6SSaravana Kannan 		dev->power.async_suspend = true;
871287905e6SSaravana Kannan }
87280dd33cfSRafael J. Wysocki 
device_disable_async_suspend(struct device * dev)873287905e6SSaravana Kannan static inline void device_disable_async_suspend(struct device *dev)
874287905e6SSaravana Kannan {
875287905e6SSaravana Kannan 	if (!dev->power.is_prepared)
8766149f83bSGreg Kroah-Hartman 		dev->power.async_suspend = false;
877a4232963SLars-Peter Clausen }
878dbba197eSJoerg Roedel 
device_async_suspend_enabled(struct device * dev)879dbba197eSJoerg Roedel static inline bool device_async_suspend_enabled(struct device *dev)
880dbba197eSJoerg Roedel {
881dbba197eSJoerg Roedel 	return !!dev->power.async_suspend;
882dbba197eSJoerg Roedel }
883dbba197eSJoerg Roedel 
device_pm_not_required(struct device * dev)884dbba197eSJoerg Roedel static inline bool device_pm_not_required(struct device *dev)
885dbba197eSJoerg Roedel {
886dbba197eSJoerg Roedel 	return dev->power.no_pm;
887dbba197eSJoerg Roedel }
8889a3df1f7SAlan Stern 
device_set_pm_not_required(struct device * dev)8899a3df1f7SAlan Stern static inline void device_set_pm_not_required(struct device *dev)
8909a3df1f7SAlan Stern {
891cd00bc2cSJames Seo 	dev->power.no_pm = true;
892cd00bc2cSJames Seo }
893cd00bc2cSJames Seo 
dev_pm_syscore_device(struct device * dev,bool val)894cd00bc2cSJames Seo static inline void dev_pm_syscore_device(struct device *dev, bool val)
895cd00bc2cSJames Seo {
896bf9ca69fSJean Delvare #ifdef CONFIG_PM_SLEEP
89706916639SKay Sievers 	dev->power.syscore = val;
898a636ee7fSPaul Mundt #endif
899a636ee7fSPaul Mundt }
900a636ee7fSPaul Mundt 
dev_pm_set_driver_flags(struct device * dev,u32 flags)901a636ee7fSPaul Mundt static inline void dev_pm_set_driver_flags(struct device *dev, u32 flags)
9021fa5ae85SKay Sievers {
90306916639SKay Sievers 	dev->power.driver_flags = flags;
90406916639SKay Sievers }
905e020ff61SSaravana Kannan 
dev_pm_test_driver_flags(struct device * dev,u32 flags)906e020ff61SSaravana Kannan static inline bool dev_pm_test_driver_flags(struct device *dev, u32 flags)
907e020ff61SSaravana Kannan {
908e020ff61SSaravana Kannan 	return !!(dev->power.driver_flags & flags);
909e020ff61SSaravana Kannan }
910e020ff61SSaravana Kannan 
dev_pm_smart_suspend(struct device * dev)911e020ff61SSaravana Kannan static inline bool dev_pm_smart_suspend(struct device *dev)
912e020ff61SSaravana Kannan {
913e020ff61SSaravana Kannan #ifdef CONFIG_PM_SLEEP
914e020ff61SSaravana Kannan 	return dev->power.smart_suspend;
915e020ff61SSaravana Kannan #else
916e020ff61SSaravana Kannan 	return false;
91767dd0772SBartosz Golaszewski #endif
918413c239fSStephen Rothwell }
91987348136SChristoph Hellwig 
device_lock(struct device * dev)92087348136SChristoph Hellwig static inline void device_lock(struct device *dev)
92187348136SChristoph Hellwig {
92287348136SChristoph Hellwig 	mutex_lock(&dev->mutex);
92387348136SChristoph Hellwig }
92487348136SChristoph Hellwig 
device_lock_interruptible(struct device * dev)92587348136SChristoph Hellwig static inline int device_lock_interruptible(struct device *dev)
92687348136SChristoph Hellwig {
92787348136SChristoph Hellwig 	return mutex_lock_interruptible(&dev->mutex);
92887348136SChristoph Hellwig }
92987348136SChristoph Hellwig 
device_trylock(struct device * dev)93087348136SChristoph Hellwig static inline int device_trylock(struct device *dev)
93198fa15f3SAnshuman Khandual {
93287348136SChristoph Hellwig 	return mutex_trylock(&dev->mutex);
93387348136SChristoph Hellwig }
93487348136SChristoph Hellwig 
device_unlock(struct device * dev)93587348136SChristoph Hellwig static inline void device_unlock(struct device *dev)
93687348136SChristoph Hellwig {
93787348136SChristoph Hellwig 	mutex_unlock(&dev->mutex);
938f1421db8SMarc Zyngier }
939f1421db8SMarc Zyngier 
DEFINE_GUARD(device,struct device *,device_lock (_T),device_unlock (_T))94013e7accbSThomas Gleixner DEFINE_GUARD(device, struct device *, device_lock(_T), device_unlock(_T))
94134fff628SThomas Gleixner 
942f1421db8SMarc Zyngier static inline void device_lock_assert(struct device *dev)
943f1421db8SMarc Zyngier {
944f1421db8SMarc Zyngier 	lockdep_assert_held(&dev->mutex);
945f1421db8SMarc Zyngier }
946f1421db8SMarc Zyngier 
dev_has_sync_state(struct device * dev)947f1421db8SMarc Zyngier static inline bool dev_has_sync_state(struct device *dev)
948f1421db8SMarc Zyngier {
94913e7accbSThomas Gleixner 	if (!dev)
95034fff628SThomas Gleixner 		return false;
951f1421db8SMarc Zyngier 	if (dev->driver && dev->driver->sync_state)
952f1421db8SMarc Zyngier 		return true;
953f1421db8SMarc Zyngier 	if (dev->bus && dev->bus->sync_state)
954a996d010SJean Delvare 		return true;
955a996d010SJean Delvare 	return false;
956a996d010SJean Delvare }
957a996d010SJean Delvare 
dev_set_removable(struct device * dev,enum device_removable removable)958a996d010SJean Delvare static inline void dev_set_removable(struct device *dev,
959a996d010SJean Delvare 				     enum device_removable removable)
960a996d010SJean Delvare {
961a996d010SJean Delvare 	dev->removable = removable;
962a996d010SJean Delvare }
963a996d010SJean Delvare 
dev_is_removable(struct device * dev)9645c095a0eSRafael J. Wysocki static inline bool dev_is_removable(struct device *dev)
9655c095a0eSRafael J. Wysocki {
9665c095a0eSRafael J. Wysocki 	return dev->removable == DEVICE_REMOVABLE;
9675c095a0eSRafael J. Wysocki }
9685c095a0eSRafael J. Wysocki 
dev_removable_is_valid(struct device * dev)969f67f129eSMing Lei static inline bool dev_removable_is_valid(struct device *dev)
970f67f129eSMing Lei {
971f67f129eSMing Lei 	return dev->removable != DEVICE_REMOVABLE_NOT_SUPPORTED;
972f67f129eSMing Lei }
973f67f129eSMing Lei 
974f67f129eSMing Lei /*
975f67f129eSMing Lei  * High level routines for use by the bus drivers
976f67f129eSMing Lei  */
977f67f129eSMing Lei int __must_check device_register(struct device *dev);
978f67f129eSMing Lei void device_unregister(struct device *dev);
979d305ef5dSDaniel Ritz void device_initialize(struct device *dev);
980d305ef5dSDaniel Ritz int __must_check device_add(struct device *dev);
9813f62e570SGreg Kroah-Hartman void device_del(struct device *dev);
982d305ef5dSDaniel Ritz 
983d305ef5dSDaniel Ritz DEFINE_FREE(device_del, struct device *, if (_T) device_del(_T))
9845af84b82SRafael J. Wysocki 
9855af84b82SRafael J. Wysocki int device_for_each_child(struct device *parent, void *data,
986f76b168bSAlan Stern 			  device_iter_t fn);
9875af84b82SRafael J. Wysocki int device_for_each_child_reverse(struct device *parent, void *data,
9885af84b82SRafael J. Wysocki 				  device_iter_t fn);
9895af84b82SRafael J. Wysocki int device_for_each_child_reverse_from(struct device *parent,
9905a2eb858SRafael J. Wysocki 				       struct device *from, void *data,
9915a2eb858SRafael J. Wysocki 				       device_iter_t fn);
992f76b168bSAlan Stern struct device *device_find_child(struct device *parent, const void *data,
9935a2eb858SRafael J. Wysocki 				 device_match_t match);
9945a2eb858SRafael J. Wysocki /**
9955a2eb858SRafael J. Wysocki  * device_find_child_by_name - device iterator for locating a child device.
9965a2eb858SRafael J. Wysocki  * @parent: parent struct device
9975a2eb858SRafael J. Wysocki  * @name: name of the child device
9985a2eb858SRafael J. Wysocki  *
9995a2eb858SRafael J. Wysocki  * This is similar to the device_find_child() function above, but it
10005a2eb858SRafael J. Wysocki  * returns a reference to a device that has the name @name.
100185945c28SSudeep Holla  *
100285945c28SSudeep Holla  * NOTE: you will need to drop the reference with put_device() after use.
100385945c28SSudeep Holla  */
device_find_child_by_name(struct device * parent,const char * name)100485945c28SSudeep Holla static inline struct device *device_find_child_by_name(struct device *parent,
100585945c28SSudeep Holla 						       const char *name)
100685945c28SSudeep Holla {
100785945c28SSudeep Holla 	return device_find_child(parent, name, device_match_name);
100885945c28SSudeep Holla }
100985945c28SSudeep Holla 
101085945c28SSudeep Holla /**
1011feb70af0SRafael J. Wysocki  * device_find_any_child - device iterator for locating a child device, if any.
1012feb70af0SRafael J. Wysocki  * @parent: parent struct device
1013feb70af0SRafael J. Wysocki  *
1014feb70af0SRafael J. Wysocki  * This is similar to the device_find_child() function above, but it
1015feb70af0SRafael J. Wysocki  * returns a reference to a child device, if any.
1016feb70af0SRafael J. Wysocki  *
1017feb70af0SRafael J. Wysocki  * NOTE: you will need to drop the reference with put_device() after use.
101808810a41SRafael J. Wysocki  */
device_find_any_child(struct device * parent)101908810a41SRafael J. Wysocki static inline struct device *device_find_any_child(struct device *parent)
102008810a41SRafael J. Wysocki {
102108810a41SRafael J. Wysocki 	return device_find_child(parent, NULL, device_match_any);
102208810a41SRafael J. Wysocki }
102308810a41SRafael J. Wysocki 
102408810a41SRafael J. Wysocki int device_rename(struct device *dev, const char *new_name);
102508810a41SRafael J. Wysocki int device_move(struct device *dev, struct device *new_parent,
102608810a41SRafael J. Wysocki 		enum dpm_order dpm_order);
102708810a41SRafael J. Wysocki int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid);
10288e9394ceSGreg Kroah-Hartman 
device_supports_offline(struct device * dev)10298e9394ceSGreg Kroah-Hartman static inline bool device_supports_offline(struct device *dev)
10303142788bSThomas Gleixner {
10318e9394ceSGreg Kroah-Hartman 	return dev->bus && dev->bus->offline && dev->bus->online;
10328e9394ceSGreg Kroah-Hartman }
10337dd9cba5SOliver Neukum 
10347dd9cba5SOliver Neukum #define __device_lock_set_class(dev, name, key)                        \
10357dd9cba5SOliver Neukum do {                                                                   \
10367dd9cba5SOliver Neukum 	struct device *__d2 __maybe_unused = dev;                      \
10377dd9cba5SOliver Neukum 	lock_set_class(&__d2->mutex.dep_map, name, key, 0, _THIS_IP_); \
10388e9394ceSGreg Kroah-Hartman } while (0)
10398e9394ceSGreg Kroah-Hartman 
10403142788bSThomas Gleixner /**
10418e9394ceSGreg Kroah-Hartman  * device_lock_set_class - Specify a temporary lock class while a device
10428e9394ceSGreg Kroah-Hartman  *			   is attached to a driver
10438e9394ceSGreg Kroah-Hartman  * @dev: device to modify
10448e9394ceSGreg Kroah-Hartman  * @key: lock class key data
10453142788bSThomas Gleixner  *
10468e9394ceSGreg Kroah-Hartman  * This must be called with the device_lock() already held, for example
10478e9394ceSGreg Kroah-Hartman  * from driver ->probe(). Take care to only override the default
1048134c6eaaSDan Williams  * lockdep_no_validate class.
1049134c6eaaSDan Williams  */
1050ac801022SKonrad Rzeszutek Wilk #ifdef CONFIG_LOCKDEP
1051ac801022SKonrad Rzeszutek Wilk #define device_lock_set_class(dev, key)                                    \
1052ac801022SKonrad Rzeszutek Wilk do {                                                                       \
1053ac801022SKonrad Rzeszutek Wilk 	struct device *__d = dev;                                          \
1054ac801022SKonrad Rzeszutek Wilk 	dev_WARN_ONCE(__d, !lockdep_match_class(&__d->mutex,               \
1055ac338acfSSaravana Kannan 						&__lockdep_no_validate__), \
1056ac338acfSSaravana Kannan 		 "overriding existing custom lock class\n");               \
1057ac338acfSSaravana Kannan 	__device_lock_set_class(__d, #key, key);                           \
1058ac338acfSSaravana Kannan } while (0)
1059ac338acfSSaravana Kannan #else
1060ac338acfSSaravana Kannan #define device_lock_set_class(dev, key) __device_lock_set_class(dev, #key, key)
1061ac338acfSSaravana Kannan #endif
1062ac338acfSSaravana Kannan 
1063ac338acfSSaravana Kannan /**
1064ac338acfSSaravana Kannan  * device_lock_reset_class - Return a device to the default lockdep novalidate state
1065ac338acfSSaravana Kannan  * @dev: device to modify
106670f400d4SRajat Jain  *
106770f400d4SRajat Jain  * This must be called with the device_lock() already held, for example
106870f400d4SRajat Jain  * from driver ->remove().
106970f400d4SRajat Jain  */
107070f400d4SRajat Jain #define device_lock_reset_class(dev) \
107170f400d4SRajat Jain do { \
107270f400d4SRajat Jain 	struct device *__d __maybe_unused = dev;                       \
107370f400d4SRajat Jain 	lock_set_novalidate_class(&__d->mutex.dep_map, "&dev->mutex",  \
107470f400d4SRajat Jain 				  _THIS_IP_);                          \
107570f400d4SRajat Jain } while (0)
107670f400d4SRajat Jain 
107770f400d4SRajat Jain void lock_device_hotplug(void);
107870f400d4SRajat Jain void unlock_device_hotplug(void);
107970f400d4SRajat Jain int lock_device_hotplug_sysfs(void);
108070f400d4SRajat Jain int device_offline(struct device *dev);
108170f400d4SRajat Jain int device_online(struct device *dev);
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
10841da177e4SLinus Torvalds void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
108567dd0772SBartosz Golaszewski void device_set_node(struct device *dev, struct fwnode_handle *fwnode);
108667dd0772SBartosz Golaszewski int device_add_of_node(struct device *dev, struct device_node *of_node);
108767dd0772SBartosz Golaszewski void device_remove_of_node(struct device *dev);
108867dd0772SBartosz Golaszewski void device_set_of_node_from_dev(struct device *dev, const struct device *dev2);
108967dd0772SBartosz Golaszewski 
dev_of_node(struct device * dev)109054da6a09SPeter Zijlstra static inline struct device_node *dev_of_node(struct device *dev)
109154da6a09SPeter Zijlstra {
109254da6a09SPeter Zijlstra 	if (!IS_ENABLED(CONFIG_OF) || !dev)
1093ab017a15SZijun Hu 		return NULL;
1094767b74e0SZijun Hu 	return dev->of_node;
1095ab017a15SZijun Hu }
1096767b74e0SZijun Hu 
dev_num_vf(struct device * dev)1097101c268bSDan Williams static inline int dev_num_vf(struct device *dev)
1098523c6b3eSZijun Hu {
1099767b74e0SZijun Hu 	if (dev->bus && dev->bus->num_vf)
1100ab017a15SZijun Hu 		return dev->bus->num_vf(dev);
1101f1e8bf56SZijun Hu 	return 0;
110251796f5eSZijun Hu }
110351796f5eSZijun Hu 
110451796f5eSZijun Hu /*
110551796f5eSZijun Hu  * Root device objects for grouping under /sys/devices
110651796f5eSZijun Hu  */
110751796f5eSZijun Hu struct device *__root_device_register(const char *name, struct module *owner);
110851796f5eSZijun Hu 
110951796f5eSZijun Hu /* This is a macro to avoid include problems with THIS_MODULE */
111051796f5eSZijun Hu #define root_device_register(name) \
111151796f5eSZijun Hu 	__root_device_register(name, THIS_MODULE)
111251796f5eSZijun Hu 
111351796f5eSZijun Hu void root_device_unregister(struct device *root);
111451796f5eSZijun Hu 
dev_get_platdata(const struct device * dev)111551796f5eSZijun Hu static inline void *dev_get_platdata(const struct device *dev)
111651796f5eSZijun Hu {
111751796f5eSZijun Hu 	return dev->platform_data;
111851796f5eSZijun Hu }
111951796f5eSZijun Hu 
112051796f5eSZijun Hu /*
112151796f5eSZijun Hu  * Manual binding of a device to driver. See drivers/base/bus.c
112251796f5eSZijun Hu  * for information on use.
112351796f5eSZijun Hu  */
112451796f5eSZijun Hu int __must_check device_driver_attach(const struct device_driver *drv,
112551796f5eSZijun Hu 				      struct device *dev);
112651796f5eSZijun Hu int __must_check device_bind_driver(struct device *dev);
112751796f5eSZijun Hu void device_release_driver(struct device *dev);
112851796f5eSZijun Hu int  __must_check device_attach(struct device *dev);
112951796f5eSZijun Hu int __must_check driver_attach(const struct device_driver *drv);
113051796f5eSZijun Hu void device_initial_probe(struct device *dev);
113182b070beSAndy Shevchenko int __must_check device_reprobe(struct device *dev);
113267dd0772SBartosz Golaszewski 
113367dd0772SBartosz Golaszewski bool device_is_bound(struct device *dev);
1134ffa6a705SCornelia Huck 
113567dd0772SBartosz Golaszewski /*
11361da177e4SLinus Torvalds  * Easy functions for dynamically creating devices on the fly
11374f3549d7SRafael J. Wysocki  */
11384f3549d7SRafael J. Wysocki __printf(5, 6) struct device *
11394f3549d7SRafael J. Wysocki device_create(const struct class *cls, struct device *parent, dev_t devt,
11404f3549d7SRafael J. Wysocki 	      void *drvdata, const char *fmt, ...);
11414f3549d7SRafael J. Wysocki __printf(6, 7) struct device *
1142d864b8eaSDan Williams device_create_with_groups(const struct class *cls, struct device *parent, dev_t devt,
1143d864b8eaSDan Williams 			  void *drvdata, const struct attribute_group **groups,
1144d864b8eaSDan Williams 			  const char *fmt, ...);
1145d864b8eaSDan Williams void device_destroy(const struct class *cls, dev_t devt);
1146d864b8eaSDan Williams 
1147d864b8eaSDan Williams int __must_check device_add_groups(struct device *dev,
1148d864b8eaSDan Williams 				   const struct attribute_group **groups);
1149d864b8eaSDan Williams void device_remove_groups(struct device *dev,
1150d864b8eaSDan Williams 			  const struct attribute_group **groups);
1151d864b8eaSDan Williams 
device_add_group(struct device * dev,const struct attribute_group * grp)1152d864b8eaSDan Williams static inline int __must_check device_add_group(struct device *dev,
1153d864b8eaSDan Williams 					const struct attribute_group *grp)
1154d864b8eaSDan Williams {
1155d864b8eaSDan Williams 	const struct attribute_group *groups[] = { grp, NULL };
1156d864b8eaSDan Williams 
1157d864b8eaSDan Williams 	return device_add_groups(dev, groups);
1158d864b8eaSDan Williams }
1159d864b8eaSDan Williams 
device_remove_group(struct device * dev,const struct attribute_group * grp)1160d864b8eaSDan Williams static inline void device_remove_group(struct device *dev,
1161d864b8eaSDan Williams 				       const struct attribute_group *grp)
1162d864b8eaSDan Williams {
1163d864b8eaSDan Williams 	const struct attribute_group *groups[] = { grp, NULL };
1164d864b8eaSDan Williams 
1165d864b8eaSDan Williams 	device_remove_groups(dev, groups);
1166d864b8eaSDan Williams }
1167d864b8eaSDan Williams 
1168d864b8eaSDan Williams int __must_check devm_device_add_group(struct device *dev,
1169d864b8eaSDan Williams 				       const struct attribute_group *grp);
1170d864b8eaSDan Williams 
1171d864b8eaSDan Williams /*
1172d864b8eaSDan Williams  * get_device - atomically increment the reference count for the device.
1173d864b8eaSDan Williams  *
1174d864b8eaSDan Williams  */
1175d864b8eaSDan Williams struct device *get_device(struct device *dev);
1176d864b8eaSDan Williams void put_device(struct device *dev);
1177d864b8eaSDan Williams 
1178d864b8eaSDan Williams DEFINE_FREE(put_device, struct device *, if (_T) put_device(_T))
1179d864b8eaSDan Williams 
1180d864b8eaSDan Williams bool kill_device(struct device *dev);
1181d864b8eaSDan Williams 
1182d864b8eaSDan Williams #ifdef CONFIG_DEVTMPFS
1183d864b8eaSDan Williams int devtmpfs_mount(void);
1184d864b8eaSDan Williams #else
devtmpfs_mount(void)118567dd0772SBartosz Golaszewski static inline int devtmpfs_mount(void) { return 0; }
118667dd0772SBartosz Golaszewski #endif
118767dd0772SBartosz Golaszewski 
118867dd0772SBartosz Golaszewski /* drivers/base/power/shutdown.c */
118967dd0772SBartosz Golaszewski void device_shutdown(void);
11901968845dSAndy Shevchenko 
119167dd0772SBartosz Golaszewski /* debugging and troubleshooting/diagnostic helpers. */
119267dd0772SBartosz Golaszewski const char *dev_driver_string(const struct device *dev);
119343e76d46SIoana Ciornei 
1194*3b62449dSHerve Codina /* Device links interface. */
1195*3b62449dSHerve Codina struct device_link *device_link_add(struct device *consumer,
11961968845dSAndy Shevchenko 				    struct device *supplier, u32 flags);
11971968845dSAndy Shevchenko void device_link_del(struct device_link *link);
11981968845dSAndy Shevchenko void device_link_remove(void *consumer, struct device *supplier);
11991968845dSAndy Shevchenko void device_links_supplier_sync_state_pause(void);
12001968845dSAndy Shevchenko void device_links_supplier_sync_state_resume(void);
12011968845dSAndy Shevchenko void device_link_wait_removal(void);
12021968845dSAndy Shevchenko 
12031968845dSAndy Shevchenko /* Create alias, so I can be autoloaded. */
120497badf87SRafael J. Wysocki #define MODULE_ALIAS_CHARDEV(major,minor) \
12059af15c38SPhil Sutter 	MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
12069af15c38SPhil Sutter #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
12079af15c38SPhil Sutter 	MODULE_ALIAS("char-major-" __stringify(major) "-*")
12089af15c38SPhil Sutter 
12099af15c38SPhil Sutter #endif /* _DEVICE_H_ */
12109af15c38SPhil Sutter