xref: /linux/include/linux/device.h (revision 8c2bf4a2e5cb4b325e328cc8808858a68616067c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * device.h - generic, centralized driver model
4  *
5  * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
6  * Copyright (c) 2004-2009 Greg Kroah-Hartman <gregkh@suse.de>
7  * Copyright (c) 2008-2009 Novell Inc.
8  *
9  * See Documentation/driver-api/driver-model/ for more information.
10  */
11 
12 #ifndef _DEVICE_H_
13 #define _DEVICE_H_
14 
15 #include <linux/dev_printk.h>
16 #include <linux/energy_model.h>
17 #include <linux/ioport.h>
18 #include <linux/kobject.h>
19 #include <linux/klist.h>
20 #include <linux/list.h>
21 #include <linux/lockdep.h>
22 #include <linux/compiler.h>
23 #include <linux/types.h>
24 #include <linux/mutex.h>
25 #include <linux/pm.h>
26 #include <linux/atomic.h>
27 #include <linux/uidgid.h>
28 #include <linux/gfp.h>
29 #include <linux/device/bus.h>
30 #include <linux/device/class.h>
31 #include <linux/device/devres.h>
32 #include <linux/device/driver.h>
33 #include <linux/cleanup.h>
34 #include <asm/device.h>
35 
36 struct device;
37 struct device_private;
38 struct device_driver;
39 struct driver_private;
40 struct module;
41 struct class;
42 struct subsys_private;
43 struct device_node;
44 struct fwnode_handle;
45 struct iommu_group;
46 struct dev_pin_info;
47 struct dev_iommu;
48 struct msi_device_data;
49 
50 /**
51  * struct subsys_interface - interfaces to device functions
52  * @name:       name of the device function
53  * @subsys:     subsystem of the devices to attach to
54  * @node:       the list of functions registered at the subsystem
55  * @add_dev:    device hookup to device function handler
56  * @remove_dev: device hookup to device function handler
57  *
58  * Simple interfaces attached to a subsystem. Multiple interfaces can
59  * attach to a subsystem and its devices. Unlike drivers, they do not
60  * exclusively claim or control devices. Interfaces usually represent
61  * a specific functionality of a subsystem/class of devices.
62  */
63 struct subsys_interface {
64 	const char *name;
65 	const struct bus_type *subsys;
66 	struct list_head node;
67 	int (*add_dev)(struct device *dev, struct subsys_interface *sif);
68 	void (*remove_dev)(struct device *dev, struct subsys_interface *sif);
69 };
70 
71 int subsys_interface_register(struct subsys_interface *sif);
72 void subsys_interface_unregister(struct subsys_interface *sif);
73 
74 int subsys_system_register(const struct bus_type *subsys,
75 			   const struct attribute_group **groups);
76 int subsys_virtual_register(const struct bus_type *subsys,
77 			    const struct attribute_group **groups);
78 
79 /*
80  * The type of device, "struct device" is embedded in. A class
81  * or bus can contain devices of different types
82  * like "partitions" and "disks", "mouse" and "event".
83  * This identifies the device type and carries type-specific
84  * information, equivalent to the kobj_type of a kobject.
85  * If "name" is specified, the uevent will contain it in
86  * the DEVTYPE variable.
87  */
88 struct device_type {
89 	const char *name;
90 	const struct attribute_group **groups;
91 	int (*uevent)(const struct device *dev, struct kobj_uevent_env *env);
92 	char *(*devnode)(const struct device *dev, umode_t *mode,
93 			 kuid_t *uid, kgid_t *gid);
94 	void (*release)(struct device *dev);
95 
96 	const struct dev_pm_ops *pm;
97 };
98 
99 /**
100  * struct device_attribute - Interface for exporting device attributes.
101  * @attr: sysfs attribute definition.
102  * @show: Show handler.
103  * @store: Store handler.
104  */
105 struct device_attribute {
106 	struct attribute	attr;
107 	ssize_t (*show)(struct device *dev, struct device_attribute *attr,
108 			char *buf);
109 	ssize_t (*store)(struct device *dev, struct device_attribute *attr,
110 			 const char *buf, size_t count);
111 };
112 
113 /**
114  * struct dev_ext_attribute - Exported device attribute with extra context.
115  * @attr: Exported device attribute.
116  * @var: Pointer to context.
117  */
118 struct dev_ext_attribute {
119 	struct device_attribute attr;
120 	void *var;
121 };
122 
123 ssize_t device_show_ulong(struct device *dev, struct device_attribute *attr,
124 			  char *buf);
125 ssize_t device_store_ulong(struct device *dev, struct device_attribute *attr,
126 			   const char *buf, size_t count);
127 ssize_t device_show_int(struct device *dev, struct device_attribute *attr,
128 			char *buf);
129 ssize_t device_store_int(struct device *dev, struct device_attribute *attr,
130 			 const char *buf, size_t count);
131 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
132 			char *buf);
133 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
134 			 const char *buf, size_t count);
135 ssize_t device_show_string(struct device *dev, struct device_attribute *attr,
136 			   char *buf);
137 
138 /**
139  * DEVICE_ATTR - Define a device attribute.
140  * @_name: Attribute name.
141  * @_mode: File mode.
142  * @_show: Show handler. Optional, but mandatory if attribute is readable.
143  * @_store: Store handler. Optional, but mandatory if attribute is writable.
144  *
145  * Convenience macro for defining a struct device_attribute.
146  *
147  * For example, ``DEVICE_ATTR(foo, 0644, foo_show, foo_store);`` expands to:
148  *
149  * .. code-block:: c
150  *
151  *	struct device_attribute dev_attr_foo = {
152  *		.attr	= { .name = "foo", .mode = 0644 },
153  *		.show	= foo_show,
154  *		.store	= foo_store,
155  *	};
156  */
157 #define DEVICE_ATTR(_name, _mode, _show, _store) \
158 	struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
159 
160 /**
161  * DEVICE_ATTR_PREALLOC - Define a preallocated device attribute.
162  * @_name: Attribute name.
163  * @_mode: File mode.
164  * @_show: Show handler. Optional, but mandatory if attribute is readable.
165  * @_store: Store handler. Optional, but mandatory if attribute is writable.
166  *
167  * Like DEVICE_ATTR(), but ``SYSFS_PREALLOC`` is set on @_mode.
168  */
169 #define DEVICE_ATTR_PREALLOC(_name, _mode, _show, _store) \
170 	struct device_attribute dev_attr_##_name = \
171 		__ATTR_PREALLOC(_name, _mode, _show, _store)
172 
173 /**
174  * DEVICE_ATTR_RW - Define a read-write device attribute.
175  * @_name: Attribute name.
176  *
177  * Like DEVICE_ATTR(), but @_mode is 0644, @_show is <_name>_show,
178  * and @_store is <_name>_store.
179  */
180 #define DEVICE_ATTR_RW(_name) \
181 	struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
182 
183 /**
184  * DEVICE_ATTR_ADMIN_RW - Define an admin-only read-write device attribute.
185  * @_name: Attribute name.
186  *
187  * Like DEVICE_ATTR_RW(), but @_mode is 0600.
188  */
189 #define DEVICE_ATTR_ADMIN_RW(_name) \
190 	struct device_attribute dev_attr_##_name = __ATTR_RW_MODE(_name, 0600)
191 
192 /**
193  * DEVICE_ATTR_RW_NAMED - Define a read-write device attribute with a sysfs name
194  * that differs from the function name.
195  * @_name: Attribute function preface
196  * @_attrname: Attribute name as it wil be exposed in the sysfs.
197  *
198  * Like DEVICE_ATTR_RW(), but allows for reusing names under separate paths in
199  * the same driver.
200  */
201 #define DEVICE_ATTR_RW_NAMED(_name, _attrname)                            \
202 	struct device_attribute dev_attr_##_name = {                      \
203 		.attr = { .name = _attrname, .mode = 0644 }, \
204 		.show = _name##_show,                                     \
205 		.store = _name##_store,                                   \
206 	}
207 
208 /**
209  * DEVICE_ATTR_RO - Define a readable device attribute.
210  * @_name: Attribute name.
211  *
212  * Like DEVICE_ATTR(), but @_mode is 0444 and @_show is <_name>_show.
213  */
214 #define DEVICE_ATTR_RO(_name) \
215 	struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
216 
217 /**
218  * DEVICE_ATTR_ADMIN_RO - Define an admin-only readable device attribute.
219  * @_name: Attribute name.
220  *
221  * Like DEVICE_ATTR_RO(), but @_mode is 0400.
222  */
223 #define DEVICE_ATTR_ADMIN_RO(_name) \
224 	struct device_attribute dev_attr_##_name = __ATTR_RO_MODE(_name, 0400)
225 
226 /**
227  * DEVICE_ATTR_RO_NAMED - Define a read-only device attribute with a sysfs name
228  * that differs from the function name.
229  * @_name: Attribute function preface
230  * @_attrname: Attribute name as it wil be exposed in the sysfs.
231  *
232  * Like DEVICE_ATTR_RO(), but allows for reusing names under separate paths in
233  * the same driver.
234  */
235 #define DEVICE_ATTR_RO_NAMED(_name, _attrname)                            \
236 	struct device_attribute dev_attr_##_name = {                      \
237 		.attr = { .name = _attrname, .mode = 0444 }, \
238 		.show = _name##_show,                                     \
239 	}
240 
241 /**
242  * DEVICE_ATTR_WO - Define an admin-only writable device attribute.
243  * @_name: Attribute name.
244  *
245  * Like DEVICE_ATTR(), but @_mode is 0200 and @_store is <_name>_store.
246  */
247 #define DEVICE_ATTR_WO(_name) \
248 	struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
249 
250 /**
251  * DEVICE_ATTR_WO_NAMED - Define a read-only device attribute with a sysfs name
252  * that differs from the function name.
253  * @_name: Attribute function preface
254  * @_attrname: Attribute name as it wil be exposed in the sysfs.
255  *
256  * Like DEVICE_ATTR_WO(), but allows for reusing names under separate paths in
257  * the same driver.
258  */
259 #define DEVICE_ATTR_WO_NAMED(_name, _attrname)                            \
260 	struct device_attribute dev_attr_##_name = {                      \
261 		.attr = { .name = _attrname, .mode = 0200 }, \
262 		.store = _name##_store,                                   \
263 	}
264 
265 /**
266  * DEVICE_ULONG_ATTR - Define a device attribute backed by an unsigned long.
267  * @_name: Attribute name.
268  * @_mode: File mode.
269  * @_var: Identifier of unsigned long.
270  *
271  * Like DEVICE_ATTR(), but @_show and @_store are automatically provided
272  * such that reads and writes to the attribute from userspace affect @_var.
273  */
274 #define DEVICE_ULONG_ATTR(_name, _mode, _var) \
275 	struct dev_ext_attribute dev_attr_##_name = \
276 		{ __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) }
277 
278 /**
279  * DEVICE_INT_ATTR - Define a device attribute backed by an int.
280  * @_name: Attribute name.
281  * @_mode: File mode.
282  * @_var: Identifier of int.
283  *
284  * Like DEVICE_ULONG_ATTR(), but @_var is an int.
285  */
286 #define DEVICE_INT_ATTR(_name, _mode, _var) \
287 	struct dev_ext_attribute dev_attr_##_name = \
288 		{ __ATTR(_name, _mode, device_show_int, device_store_int), &(_var) }
289 
290 /**
291  * DEVICE_BOOL_ATTR - Define a device attribute backed by a bool.
292  * @_name: Attribute name.
293  * @_mode: File mode.
294  * @_var: Identifier of bool.
295  *
296  * Like DEVICE_ULONG_ATTR(), but @_var is a bool.
297  */
298 #define DEVICE_BOOL_ATTR(_name, _mode, _var) \
299 	struct dev_ext_attribute dev_attr_##_name = \
300 		{ __ATTR(_name, _mode, device_show_bool, device_store_bool), &(_var) }
301 
302 /**
303  * DEVICE_STRING_ATTR_RO - Define a device attribute backed by a r/o string.
304  * @_name: Attribute name.
305  * @_mode: File mode.
306  * @_var: Identifier of string.
307  *
308  * Like DEVICE_ULONG_ATTR(), but @_var is a string. Because the length of the
309  * string allocation is unknown, the attribute must be read-only.
310  */
311 #define DEVICE_STRING_ATTR_RO(_name, _mode, _var) \
312 	struct dev_ext_attribute dev_attr_##_name = \
313 		{ __ATTR(_name, (_mode) & ~0222, device_show_string, NULL), (_var) }
314 
315 #define DEVICE_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
316 	struct device_attribute dev_attr_##_name =		\
317 		__ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
318 
319 int device_create_file(struct device *device,
320 		       const struct device_attribute *entry);
321 void device_remove_file(struct device *dev,
322 			const struct device_attribute *attr);
323 bool device_remove_file_self(struct device *dev,
324 			     const struct device_attribute *attr);
325 int __must_check device_create_bin_file(struct device *dev,
326 					const struct bin_attribute *attr);
327 void device_remove_bin_file(struct device *dev,
328 			    const struct bin_attribute *attr);
329 
330 struct device_dma_parameters {
331 	/*
332 	 * a low level driver may set these to teach IOMMU code about
333 	 * sg limitations.
334 	 */
335 	unsigned int max_segment_size;
336 	unsigned int min_align_mask;
337 	unsigned long segment_boundary_mask;
338 };
339 
340 /**
341  * enum device_link_state - Device link states.
342  * @DL_STATE_NONE: The presence of the drivers is not being tracked.
343  * @DL_STATE_DORMANT: None of the supplier/consumer drivers is present.
344  * @DL_STATE_AVAILABLE: The supplier driver is present, but the consumer is not.
345  * @DL_STATE_CONSUMER_PROBE: The consumer is probing (supplier driver present).
346  * @DL_STATE_ACTIVE: Both the supplier and consumer drivers are present.
347  * @DL_STATE_SUPPLIER_UNBIND: The supplier driver is unbinding.
348  */
349 enum device_link_state {
350 	DL_STATE_NONE = -1,
351 	DL_STATE_DORMANT = 0,
352 	DL_STATE_AVAILABLE,
353 	DL_STATE_CONSUMER_PROBE,
354 	DL_STATE_ACTIVE,
355 	DL_STATE_SUPPLIER_UNBIND,
356 };
357 
358 /*
359  * Device link flags.
360  *
361  * STATELESS: The core will not remove this link automatically.
362  * AUTOREMOVE_CONSUMER: Remove the link automatically on consumer driver unbind.
363  * PM_RUNTIME: If set, the runtime PM framework will use this link.
364  * RPM_ACTIVE: Run pm_runtime_get_sync() on the supplier during link creation.
365  * AUTOREMOVE_SUPPLIER: Remove the link automatically on supplier driver unbind.
366  * AUTOPROBE_CONSUMER: Probe consumer driver automatically after supplier binds.
367  * MANAGED: The core tracks presence of supplier/consumer drivers (internal).
368  * SYNC_STATE_ONLY: Link only affects sync_state() behavior.
369  * INFERRED: Inferred from data (eg: firmware) and not from driver actions.
370  */
371 #define DL_FLAG_STATELESS		BIT(0)
372 #define DL_FLAG_AUTOREMOVE_CONSUMER	BIT(1)
373 #define DL_FLAG_PM_RUNTIME		BIT(2)
374 #define DL_FLAG_RPM_ACTIVE		BIT(3)
375 #define DL_FLAG_AUTOREMOVE_SUPPLIER	BIT(4)
376 #define DL_FLAG_AUTOPROBE_CONSUMER	BIT(5)
377 #define DL_FLAG_MANAGED			BIT(6)
378 #define DL_FLAG_SYNC_STATE_ONLY		BIT(7)
379 #define DL_FLAG_INFERRED		BIT(8)
380 #define DL_FLAG_CYCLE			BIT(9)
381 
382 /**
383  * enum dl_dev_state - Device driver presence tracking information.
384  * @DL_DEV_NO_DRIVER: There is no driver attached to the device.
385  * @DL_DEV_PROBING: A driver is probing.
386  * @DL_DEV_DRIVER_BOUND: The driver has been bound to the device.
387  * @DL_DEV_UNBINDING: The driver is unbinding from the device.
388  */
389 enum dl_dev_state {
390 	DL_DEV_NO_DRIVER = 0,
391 	DL_DEV_PROBING,
392 	DL_DEV_DRIVER_BOUND,
393 	DL_DEV_UNBINDING,
394 };
395 
396 /**
397  * enum device_removable - Whether the device is removable. The criteria for a
398  * device to be classified as removable is determined by its subsystem or bus.
399  * @DEVICE_REMOVABLE_NOT_SUPPORTED: This attribute is not supported for this
400  *				    device (default).
401  * @DEVICE_REMOVABLE_UNKNOWN:  Device location is Unknown.
402  * @DEVICE_FIXED: Device is not removable by the user.
403  * @DEVICE_REMOVABLE: Device is removable by the user.
404  */
405 enum device_removable {
406 	DEVICE_REMOVABLE_NOT_SUPPORTED = 0, /* must be 0 */
407 	DEVICE_REMOVABLE_UNKNOWN,
408 	DEVICE_FIXED,
409 	DEVICE_REMOVABLE,
410 };
411 
412 /**
413  * struct dev_links_info - Device data related to device links.
414  * @suppliers: List of links to supplier devices.
415  * @consumers: List of links to consumer devices.
416  * @defer_sync: Hook to global list of devices that have deferred sync_state.
417  * @status: Driver status information.
418  */
419 struct dev_links_info {
420 	struct list_head suppliers;
421 	struct list_head consumers;
422 	struct list_head defer_sync;
423 	enum dl_dev_state status;
424 };
425 
426 /**
427  * struct dev_msi_info - Device data related to MSI
428  * @domain:	The MSI interrupt domain associated to the device
429  * @data:	Pointer to MSI device data
430  */
431 struct dev_msi_info {
432 #ifdef CONFIG_GENERIC_MSI_IRQ
433 	struct irq_domain	*domain;
434 	struct msi_device_data	*data;
435 #endif
436 };
437 
438 /**
439  * enum device_physical_location_panel - Describes which panel surface of the
440  * system's housing the device connection point resides on.
441  * @DEVICE_PANEL_TOP: Device connection point is on the top panel.
442  * @DEVICE_PANEL_BOTTOM: Device connection point is on the bottom panel.
443  * @DEVICE_PANEL_LEFT: Device connection point is on the left panel.
444  * @DEVICE_PANEL_RIGHT: Device connection point is on the right panel.
445  * @DEVICE_PANEL_FRONT: Device connection point is on the front panel.
446  * @DEVICE_PANEL_BACK: Device connection point is on the back panel.
447  * @DEVICE_PANEL_UNKNOWN: The panel with device connection point is unknown.
448  */
449 enum device_physical_location_panel {
450 	DEVICE_PANEL_TOP,
451 	DEVICE_PANEL_BOTTOM,
452 	DEVICE_PANEL_LEFT,
453 	DEVICE_PANEL_RIGHT,
454 	DEVICE_PANEL_FRONT,
455 	DEVICE_PANEL_BACK,
456 	DEVICE_PANEL_UNKNOWN,
457 };
458 
459 /**
460  * enum device_physical_location_vertical_position - Describes vertical
461  * position of the device connection point on the panel surface.
462  * @DEVICE_VERT_POS_UPPER: Device connection point is at upper part of panel.
463  * @DEVICE_VERT_POS_CENTER: Device connection point is at center part of panel.
464  * @DEVICE_VERT_POS_LOWER: Device connection point is at lower part of panel.
465  */
466 enum device_physical_location_vertical_position {
467 	DEVICE_VERT_POS_UPPER,
468 	DEVICE_VERT_POS_CENTER,
469 	DEVICE_VERT_POS_LOWER,
470 };
471 
472 /**
473  * enum device_physical_location_horizontal_position - Describes horizontal
474  * position of the device connection point on the panel surface.
475  * @DEVICE_HORI_POS_LEFT: Device connection point is at left part of panel.
476  * @DEVICE_HORI_POS_CENTER: Device connection point is at center part of panel.
477  * @DEVICE_HORI_POS_RIGHT: Device connection point is at right part of panel.
478  */
479 enum device_physical_location_horizontal_position {
480 	DEVICE_HORI_POS_LEFT,
481 	DEVICE_HORI_POS_CENTER,
482 	DEVICE_HORI_POS_RIGHT,
483 };
484 
485 /**
486  * struct device_physical_location - Device data related to physical location
487  * of the device connection point.
488  * @panel: Panel surface of the system's housing that the device connection
489  *         point resides on.
490  * @vertical_position: Vertical position of the device connection point within
491  *                     the panel.
492  * @horizontal_position: Horizontal position of the device connection point
493  *                       within the panel.
494  * @dock: Set if the device connection point resides in a docking station or
495  *        port replicator.
496  * @lid: Set if this device connection point resides on the lid of laptop
497  *       system.
498  */
499 struct device_physical_location {
500 	enum device_physical_location_panel panel;
501 	enum device_physical_location_vertical_position vertical_position;
502 	enum device_physical_location_horizontal_position horizontal_position;
503 	bool dock;
504 	bool lid;
505 };
506 
507 /**
508  * enum struct_device_flags - Flags in struct device
509  *
510  * Each flag should have a set of accessor functions created via
511  * __create_dev_flag_accessors() for each access.
512  *
513  * @DEV_FLAG_READY_TO_PROBE: If set then device_add() has finished enough
514  *		initialization that probe could be called.
515  * @DEV_FLAG_COUNT: Number of defined struct_device_flags.
516  */
517 enum struct_device_flags {
518 	DEV_FLAG_READY_TO_PROBE = 0,
519 
520 	DEV_FLAG_COUNT
521 };
522 
523 /**
524  * struct device - The basic device structure
525  * @parent:	The device's "parent" device, the device to which it is attached.
526  * 		In most cases, a parent device is some sort of bus or host
527  * 		controller. If parent is NULL, the device, is a top-level device,
528  * 		which is not usually what you want.
529  * @p:		Holds the private data of the driver core portions of the device.
530  * 		See the comment of the struct device_private for detail.
531  * @kobj:	A top-level, abstract class from which other classes are derived.
532  * @init_name:	Initial name of the device.
533  * @type:	The type of device.
534  * 		This identifies the device type and carries type-specific
535  * 		information.
536  * @mutex:	Mutex to synchronize calls to its driver.
537  * @bus:	Type of bus device is on.
538  * @driver:	Which driver has allocated this
539  * @platform_data: Platform data specific to the device.
540  * 		Example: For devices on custom boards, as typical of embedded
541  * 		and SOC based hardware, Linux often uses platform_data to point
542  * 		to board-specific structures describing devices and how they
543  * 		are wired.  That can include what ports are available, chip
544  * 		variants, which GPIO pins act in what additional roles, and so
545  * 		on.  This shrinks the "Board Support Packages" (BSPs) and
546  * 		minimizes board-specific #ifdefs in drivers.
547  * @driver_data: Private pointer for driver specific info.
548  * @driver_override: Driver name to force a match.  Do not touch directly; use
549  *		     device_set_driver_override() instead.
550  * @links:	Links to suppliers and consumers of this device.
551  * @power:	For device power management.
552  *		See Documentation/driver-api/pm/devices.rst for details.
553  * @pm_domain:	Provide callbacks that are executed during system suspend,
554  * 		hibernation, system resume and during runtime PM transitions
555  * 		along with subsystem-level and driver-level callbacks.
556  * @em_pd:	device's energy model performance domain
557  * @pins:	For device pin management.
558  *		See Documentation/driver-api/pin-control.rst for details.
559  * @msi:	MSI related data
560  * @numa_node:	NUMA node this device is close to.
561  * @dma_ops:    DMA mapping operations for this device.
562  * @dma_mask:	Dma mask (if dma'ble device).
563  * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all
564  * 		hardware supports 64-bit addresses for consistent allocations
565  * 		such descriptors.
566  * @bus_dma_limit: Limit of an upstream bridge or bus which imposes a smaller
567  *		DMA limit than the device itself supports.
568  * @dma_range_map: map for DMA memory ranges relative to that of RAM
569  * @dma_parms:	A low level driver may set these to teach IOMMU code about
570  * 		segment limitations.
571  * @dma_pools:	Dma pools (if dma'ble device).
572  * @dma_mem:	Internal for coherent mem override.
573  * @cma_area:	Contiguous memory area for dma allocations
574  * @dma_io_tlb_mem: Software IO TLB allocator.  Not for driver use.
575  * @dma_io_tlb_pools:	List of transient swiotlb memory pools.
576  * @dma_io_tlb_lock:	Protects changes to the list of active pools.
577  * @dma_uses_io_tlb: %true if device has used the software IO TLB.
578  * @archdata:	For arch-specific additions.
579  * @of_node:	Associated device tree node.
580  * @fwnode:	Associated device node supplied by platform firmware.
581  * @devt:	For creating the sysfs "dev".
582  * @id:		device instance
583  * @devres_lock: Spinlock to protect the resource of the device.
584  * @devres_head: The resources list of the device.
585  * @class:	The class of the device.
586  * @groups:	Optional attribute groups.
587  * @release:	Callback to free the device after all references have
588  * 		gone away. This should be set by the allocator of the
589  * 		device (i.e. the bus driver that discovered the device).
590  * @iommu_group: IOMMU group the device belongs to.
591  * @iommu:	Per device generic IOMMU runtime data
592  * @physical_location: Describes physical location of the device connection
593  *		point in the system housing.
594  * @removable:  Whether the device can be removed from the system. This
595  *              should be set by the subsystem / bus driver that discovered
596  *              the device.
597  *
598  * @offline_disabled: If set, the device is permanently online.
599  * @offline:	Set after successful invocation of bus type's .offline().
600  * @of_node_reused: Set if the device-tree node is shared with an ancestor
601  *              device.
602  * @state_synced: The hardware state of this device has been synced to match
603  *		  the software state of this device by calling the driver/bus
604  *		  sync_state() callback.
605  * @can_match:	The device has matched with a driver at least once or it is in
606  *		a bus (like AMBA) which can't check for matching drivers until
607  *		other devices probe successfully.
608  * @dma_coherent: this particular device is dma coherent, even if the
609  *		architecture supports non-coherent devices.
610  * @dma_ops_bypass: If set to %true then the dma_ops are bypassed for the
611  *		streaming DMA operations (->map_* / ->unmap_* / ->sync_*),
612  *		and optionall (if the coherent mask is large enough) also
613  *		for dma allocations.  This flag is managed by the dma ops
614  *		instance from ->dma_supported.
615  * @dma_skip_sync: DMA sync operations can be skipped for coherent buffers.
616  * @dma_iommu: Device is using default IOMMU implementation for DMA and
617  *		doesn't rely on dma_ops structure.
618  * @flags:	DEV_FLAG_XXX flags. Use atomic bitfield operations to modify.
619  *
620  * At the lowest level, every device in a Linux system is represented by an
621  * instance of struct device. The device structure contains the information
622  * that the device model core needs to model the system. Most subsystems,
623  * however, track additional information about the devices they host. As a
624  * result, it is rare for devices to be represented by bare device structures;
625  * instead, that structure, like kobject structures, is usually embedded within
626  * a higher-level representation of the device.
627  */
628 struct device {
629 	struct kobject kobj;
630 	struct device		*parent;
631 
632 	struct device_private	*p;
633 
634 	const char		*init_name; /* initial name of the device */
635 	const struct device_type *type;
636 
637 	const struct bus_type	*bus;	/* type of bus device is on */
638 	struct device_driver *driver;	/* which driver has allocated this
639 					   device */
640 	void		*platform_data;	/* Platform specific data, device
641 					   core doesn't touch it */
642 	void		*driver_data;	/* Driver data, set and get with
643 					   dev_set_drvdata/dev_get_drvdata */
644 	struct {
645 		const char	*name;
646 		spinlock_t	lock;
647 	} driver_override;
648 	struct mutex		mutex;	/* mutex to synchronize calls to
649 					 * its driver.
650 					 */
651 
652 	struct dev_links_info	links;
653 	struct dev_pm_info	power;
654 	struct dev_pm_domain	*pm_domain;
655 
656 #ifdef CONFIG_ENERGY_MODEL
657 	struct em_perf_domain	*em_pd;
658 #endif
659 
660 #ifdef CONFIG_PINCTRL
661 	struct dev_pin_info	*pins;
662 #endif
663 	struct dev_msi_info	msi;
664 #ifdef CONFIG_ARCH_HAS_DMA_OPS
665 	const struct dma_map_ops *dma_ops;
666 #endif
667 	u64		*dma_mask;	/* dma mask (if dma'able device) */
668 	u64		coherent_dma_mask;/* Like dma_mask, but for
669 					     alloc_coherent mappings as
670 					     not all hardware supports
671 					     64 bit addresses for consistent
672 					     allocations such descriptors. */
673 	u64		bus_dma_limit;	/* upstream dma constraint */
674 	const struct bus_dma_region *dma_range_map;
675 
676 	struct device_dma_parameters *dma_parms;
677 
678 	struct list_head	dma_pools;	/* dma pools (if dma'ble) */
679 
680 #ifdef CONFIG_DMA_DECLARE_COHERENT
681 	struct dma_coherent_mem	*dma_mem; /* internal for coherent mem
682 					     override */
683 #endif
684 #ifdef CONFIG_DMA_CMA
685 	struct cma *cma_area;		/* contiguous memory area for dma
686 					   allocations */
687 #endif
688 #ifdef CONFIG_SWIOTLB
689 	struct io_tlb_mem *dma_io_tlb_mem;
690 #endif
691 #ifdef CONFIG_SWIOTLB_DYNAMIC
692 	struct list_head dma_io_tlb_pools;
693 	spinlock_t dma_io_tlb_lock;
694 	bool dma_uses_io_tlb;
695 #endif
696 	/* arch specific additions */
697 	struct dev_archdata	archdata;
698 
699 	struct device_node	*of_node; /* associated device tree node */
700 	struct fwnode_handle	*fwnode; /* firmware device node */
701 
702 #ifdef CONFIG_NUMA
703 	int		numa_node;	/* NUMA node this device is close to */
704 #endif
705 	dev_t			devt;	/* dev_t, creates the sysfs "dev" */
706 	u32			id;	/* device instance */
707 
708 	spinlock_t		devres_lock;
709 	struct list_head	devres_head;
710 
711 	const struct class	*class;
712 	const struct attribute_group **groups;	/* optional groups */
713 
714 	void	(*release)(struct device *dev);
715 	struct iommu_group	*iommu_group;
716 	struct dev_iommu	*iommu;
717 
718 	struct device_physical_location *physical_location;
719 
720 	enum device_removable	removable;
721 
722 	bool			offline_disabled:1;
723 	bool			offline:1;
724 	bool			of_node_reused:1;
725 	bool			state_synced:1;
726 	bool			can_match:1;
727 #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
728     defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
729     defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
730 	bool			dma_coherent:1;
731 #endif
732 #ifdef CONFIG_DMA_OPS_BYPASS
733 	bool			dma_ops_bypass : 1;
734 #endif
735 #ifdef CONFIG_DMA_NEED_SYNC
736 	bool			dma_skip_sync:1;
737 #endif
738 #ifdef CONFIG_IOMMU_DMA
739 	bool			dma_iommu:1;
740 #endif
741 
742 	DECLARE_BITMAP(flags, DEV_FLAG_COUNT);
743 };
744 
745 #define __create_dev_flag_accessors(accessor_name, flag_name) \
746 static inline bool dev_##accessor_name(const struct device *dev) \
747 { \
748 	return test_bit(flag_name, dev->flags); \
749 } \
750 static inline void dev_set_##accessor_name(struct device *dev) \
751 { \
752 	set_bit(flag_name, dev->flags); \
753 } \
754 static inline void dev_clear_##accessor_name(struct device *dev) \
755 { \
756 	clear_bit(flag_name, dev->flags); \
757 } \
758 static inline void dev_assign_##accessor_name(struct device *dev, bool value) \
759 { \
760 	assign_bit(flag_name, dev->flags, value); \
761 } \
762 static inline bool dev_test_and_set_##accessor_name(struct device *dev) \
763 { \
764 	return test_and_set_bit(flag_name, dev->flags); \
765 }
766 
767 __create_dev_flag_accessors(ready_to_probe, DEV_FLAG_READY_TO_PROBE);
768 
769 #undef __create_dev_flag_accessors
770 
771 /**
772  * struct device_link - Device link representation.
773  * @supplier: The device on the supplier end of the link.
774  * @s_node: Hook to the supplier device's list of links to consumers.
775  * @consumer: The device on the consumer end of the link.
776  * @c_node: Hook to the consumer device's list of links to suppliers.
777  * @link_dev: device used to expose link details in sysfs
778  * @status: The state of the link (with respect to the presence of drivers).
779  * @flags: Link flags.
780  * @rpm_active: Whether or not the consumer device is runtime-PM-active.
781  * @kref: Count repeated addition of the same link.
782  * @rm_work: Work structure used for removing the link.
783  * @supplier_preactivated: Supplier has been made active before consumer probe.
784  */
785 struct device_link {
786 	struct device *supplier;
787 	struct list_head s_node;
788 	struct device *consumer;
789 	struct list_head c_node;
790 	struct device link_dev;
791 	enum device_link_state status;
792 	u32 flags;
793 	refcount_t rpm_active;
794 	struct kref kref;
795 	struct work_struct rm_work;
796 	bool supplier_preactivated; /* Owned by consumer probe. */
797 };
798 
799 #define kobj_to_dev(__kobj)	container_of_const(__kobj, struct device, kobj)
800 
801 int __device_set_driver_override(struct device *dev, const char *s, size_t len);
802 
803 /**
804  * device_set_driver_override() - Helper to set or clear driver override.
805  * @dev: Device to change
806  * @s: NUL-terminated string, new driver name to force a match, pass empty
807  *     string to clear it ("" or "\n", where the latter is only for sysfs
808  *     interface).
809  *
810  * Helper to set or clear driver override of a device.
811  *
812  * Returns: 0 on success or a negative error code on failure.
813  */
814 static inline int device_set_driver_override(struct device *dev, const char *s)
815 {
816 	return __device_set_driver_override(dev, s, s ? strlen(s) : 0);
817 }
818 
819 /**
820  * device_has_driver_override() - Check if a driver override has been set.
821  * @dev: device to check
822  *
823  * Returns true if a driver override has been set for this device.
824  */
825 static inline bool device_has_driver_override(struct device *dev)
826 {
827 	guard(spinlock)(&dev->driver_override.lock);
828 	return !!dev->driver_override.name;
829 }
830 
831 /**
832  * device_match_driver_override() - Match a driver against the device's driver_override.
833  * @dev: device to check
834  * @drv: driver to match against
835  *
836  * Returns > 0 if a driver override is set and matches the given driver, 0 if a
837  * driver override is set but does not match, or < 0 if a driver override is not
838  * set at all.
839  */
840 static inline int device_match_driver_override(struct device *dev,
841 					       const struct device_driver *drv)
842 {
843 	guard(spinlock)(&dev->driver_override.lock);
844 	if (dev->driver_override.name)
845 		return !strcmp(dev->driver_override.name, drv->name);
846 	return -1;
847 }
848 
849 /**
850  * device_iommu_mapped - Returns true when the device DMA is translated
851  *			 by an IOMMU
852  * @dev: Device to perform the check on
853  */
854 static inline bool device_iommu_mapped(struct device *dev)
855 {
856 	return (dev->iommu_group != NULL);
857 }
858 
859 /* Get the wakeup routines, which depend on struct device */
860 #include <linux/pm_wakeup.h>
861 
862 /**
863  * dev_name - Return a device's name.
864  * @dev: Device with name to get.
865  * Return: The kobject name of the device, or its initial name if unavailable.
866  */
867 static inline const char *dev_name(const struct device *dev)
868 {
869 	/* Use the init name until the kobject becomes available */
870 	if (dev->init_name)
871 		return dev->init_name;
872 
873 	return kobject_name(&dev->kobj);
874 }
875 
876 /**
877  * dev_bus_name - Return a device's bus/class name, if at all possible
878  * @dev: struct device to get the bus/class name of
879  *
880  * Will return the name of the bus/class the device is attached to.  If it is
881  * not attached to a bus/class, an empty string will be returned.
882  */
883 static inline const char *dev_bus_name(const struct device *dev)
884 {
885 	return dev->bus ? dev->bus->name : (dev->class ? dev->class->name : "");
886 }
887 
888 __printf(2, 3) int dev_set_name(struct device *dev, const char *name, ...);
889 
890 #ifdef CONFIG_NUMA
891 static inline int dev_to_node(struct device *dev)
892 {
893 	return dev->numa_node;
894 }
895 static inline void set_dev_node(struct device *dev, int node)
896 {
897 	dev->numa_node = node;
898 }
899 #else
900 static inline int dev_to_node(struct device *dev)
901 {
902 	return NUMA_NO_NODE;
903 }
904 static inline void set_dev_node(struct device *dev, int node)
905 {
906 }
907 #endif
908 
909 static inline struct irq_domain *dev_get_msi_domain(const struct device *dev)
910 {
911 #ifdef CONFIG_GENERIC_MSI_IRQ
912 	return dev->msi.domain;
913 #else
914 	return NULL;
915 #endif
916 }
917 
918 static inline void dev_set_msi_domain(struct device *dev, struct irq_domain *d)
919 {
920 #ifdef CONFIG_GENERIC_MSI_IRQ
921 	dev->msi.domain = d;
922 #endif
923 }
924 
925 static inline void *dev_get_drvdata(const struct device *dev)
926 {
927 	return dev->driver_data;
928 }
929 
930 static inline void dev_set_drvdata(struct device *dev, void *data)
931 {
932 	dev->driver_data = data;
933 }
934 
935 static inline struct pm_subsys_data *dev_to_psd(struct device *dev)
936 {
937 	return dev ? dev->power.subsys_data : NULL;
938 }
939 
940 static inline unsigned int dev_get_uevent_suppress(const struct device *dev)
941 {
942 	return dev->kobj.uevent_suppress;
943 }
944 
945 static inline void dev_set_uevent_suppress(struct device *dev, int val)
946 {
947 	dev->kobj.uevent_suppress = val;
948 }
949 
950 static inline int device_is_registered(struct device *dev)
951 {
952 	return dev->kobj.state_in_sysfs;
953 }
954 
955 static inline void device_enable_async_suspend(struct device *dev)
956 {
957 	if (!dev->power.is_prepared)
958 		dev->power.async_suspend = true;
959 }
960 
961 static inline void device_disable_async_suspend(struct device *dev)
962 {
963 	if (!dev->power.is_prepared)
964 		dev->power.async_suspend = false;
965 }
966 
967 static inline bool device_async_suspend_enabled(struct device *dev)
968 {
969 	return !!dev->power.async_suspend;
970 }
971 
972 static inline bool device_pm_not_required(struct device *dev)
973 {
974 	return dev->power.no_pm;
975 }
976 
977 static inline void device_set_pm_not_required(struct device *dev)
978 {
979 	dev->power.no_pm = true;
980 #ifdef CONFIG_PM
981 	dev->power.no_callbacks = true;
982 #endif
983 }
984 
985 static inline void dev_pm_syscore_device(struct device *dev, bool val)
986 {
987 #ifdef CONFIG_PM_SLEEP
988 	dev->power.syscore = val;
989 #endif
990 }
991 
992 static inline void dev_pm_set_driver_flags(struct device *dev, u32 flags)
993 {
994 	dev->power.driver_flags = flags;
995 }
996 
997 static inline bool dev_pm_test_driver_flags(struct device *dev, u32 flags)
998 {
999 	return !!(dev->power.driver_flags & flags);
1000 }
1001 
1002 static inline bool dev_pm_smart_suspend(struct device *dev)
1003 {
1004 #ifdef CONFIG_PM_SLEEP
1005 	return dev->power.smart_suspend;
1006 #else
1007 	return false;
1008 #endif
1009 }
1010 
1011 /*
1012  * dev_pm_set_strict_midlayer - Update the device's power.strict_midlayer flag
1013  * @dev: Target device.
1014  * @val: New flag value.
1015  *
1016  * When set, power.strict_midlayer means that the middle layer power management
1017  * code (typically, a bus type or a PM domain) does not expect its runtime PM
1018  * suspend callback to be invoked at all during system-wide PM transitions and
1019  * it does not expect its runtime PM resume callback to be invoked at any point
1020  * when runtime PM is disabled for the device during system-wide PM transitions.
1021  */
1022 static inline void dev_pm_set_strict_midlayer(struct device *dev, bool val)
1023 {
1024 #ifdef CONFIG_PM_SLEEP
1025 	dev->power.strict_midlayer = val;
1026 #endif
1027 }
1028 
1029 static inline bool dev_pm_strict_midlayer_is_set(struct device *dev)
1030 {
1031 #ifdef CONFIG_PM_SLEEP
1032 	return dev->power.strict_midlayer;
1033 #else
1034 	return false;
1035 #endif
1036 }
1037 
1038 static inline void device_lock(struct device *dev)
1039 {
1040 	mutex_lock(&dev->mutex);
1041 }
1042 
1043 static inline int device_lock_interruptible(struct device *dev)
1044 {
1045 	return mutex_lock_interruptible(&dev->mutex);
1046 }
1047 
1048 static inline int device_trylock(struct device *dev)
1049 {
1050 	return mutex_trylock(&dev->mutex);
1051 }
1052 
1053 static inline void device_unlock(struct device *dev)
1054 {
1055 	mutex_unlock(&dev->mutex);
1056 }
1057 
1058 DEFINE_GUARD(device, struct device *, device_lock(_T), device_unlock(_T))
1059 DEFINE_GUARD_COND(device, _intr, device_lock_interruptible(_T), _RET == 0)
1060 
1061 static inline void device_lock_assert(struct device *dev)
1062 {
1063 	lockdep_assert_held(&dev->mutex);
1064 }
1065 
1066 static inline bool dev_has_sync_state(struct device *dev)
1067 {
1068 	if (!dev)
1069 		return false;
1070 	if (dev->driver && dev->driver->sync_state)
1071 		return true;
1072 	if (dev->bus && dev->bus->sync_state)
1073 		return true;
1074 	return false;
1075 }
1076 
1077 static inline int dev_set_drv_sync_state(struct device *dev,
1078 					 void (*fn)(struct device *dev))
1079 {
1080 	if (!dev || !dev->driver)
1081 		return 0;
1082 	if (dev->driver->sync_state && dev->driver->sync_state != fn)
1083 		return -EBUSY;
1084 	if (!dev->driver->sync_state)
1085 		dev->driver->sync_state = fn;
1086 	return 0;
1087 }
1088 
1089 static inline void dev_set_removable(struct device *dev,
1090 				     enum device_removable removable)
1091 {
1092 	dev->removable = removable;
1093 }
1094 
1095 static inline bool dev_is_removable(struct device *dev)
1096 {
1097 	return dev->removable == DEVICE_REMOVABLE;
1098 }
1099 
1100 static inline bool dev_removable_is_valid(struct device *dev)
1101 {
1102 	return dev->removable != DEVICE_REMOVABLE_NOT_SUPPORTED;
1103 }
1104 
1105 /*
1106  * High level routines for use by the bus drivers
1107  */
1108 int __must_check device_register(struct device *dev);
1109 void device_unregister(struct device *dev);
1110 void device_initialize(struct device *dev);
1111 int __must_check device_add(struct device *dev);
1112 void device_del(struct device *dev);
1113 
1114 DEFINE_FREE(device_del, struct device *, if (_T) device_del(_T))
1115 
1116 int device_for_each_child(struct device *parent, void *data,
1117 			  device_iter_t fn);
1118 int device_for_each_child_reverse(struct device *parent, void *data,
1119 				  device_iter_t fn);
1120 int device_for_each_child_reverse_from(struct device *parent,
1121 				       struct device *from, void *data,
1122 				       device_iter_t fn);
1123 struct device *device_find_child(struct device *parent, const void *data,
1124 				 device_match_t match);
1125 /**
1126  * device_find_child_by_name - device iterator for locating a child device.
1127  * @parent: parent struct device
1128  * @name: name of the child device
1129  *
1130  * This is similar to the device_find_child() function above, but it
1131  * returns a reference to a device that has the name @name.
1132  *
1133  * NOTE: you will need to drop the reference with put_device() after use.
1134  */
1135 static inline struct device *device_find_child_by_name(struct device *parent,
1136 						       const char *name)
1137 {
1138 	return device_find_child(parent, name, device_match_name);
1139 }
1140 
1141 /**
1142  * device_find_any_child - device iterator for locating a child device, if any.
1143  * @parent: parent struct device
1144  *
1145  * This is similar to the device_find_child() function above, but it
1146  * returns a reference to a child device, if any.
1147  *
1148  * NOTE: you will need to drop the reference with put_device() after use.
1149  */
1150 static inline struct device *device_find_any_child(struct device *parent)
1151 {
1152 	return device_find_child(parent, NULL, device_match_any);
1153 }
1154 
1155 int device_rename(struct device *dev, const char *new_name);
1156 int device_move(struct device *dev, struct device *new_parent,
1157 		enum dpm_order dpm_order);
1158 int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid);
1159 
1160 static inline bool device_supports_offline(struct device *dev)
1161 {
1162 	return dev->bus && dev->bus->offline && dev->bus->online;
1163 }
1164 
1165 #define __device_lock_set_class(dev, name, key)                        \
1166 do {                                                                   \
1167 	struct device *__d2 __maybe_unused = dev;                      \
1168 	lock_set_class(&__d2->mutex.dep_map, name, key, 0, _THIS_IP_); \
1169 } while (0)
1170 
1171 /**
1172  * device_lock_set_class - Specify a temporary lock class while a device
1173  *			   is attached to a driver
1174  * @dev: device to modify
1175  * @key: lock class key data
1176  *
1177  * This must be called with the device_lock() already held, for example
1178  * from driver ->probe(). Take care to only override the default
1179  * lockdep_no_validate class.
1180  */
1181 #ifdef CONFIG_LOCKDEP
1182 #define device_lock_set_class(dev, key)                                    \
1183 do {                                                                       \
1184 	struct device *__d = dev;                                          \
1185 	dev_WARN_ONCE(__d, !lockdep_match_class(&__d->mutex,               \
1186 						&__lockdep_no_validate__), \
1187 		 "overriding existing custom lock class\n");               \
1188 	__device_lock_set_class(__d, #key, key);                           \
1189 } while (0)
1190 #else
1191 #define device_lock_set_class(dev, key) __device_lock_set_class(dev, #key, key)
1192 #endif
1193 
1194 /**
1195  * device_lock_reset_class - Return a device to the default lockdep novalidate state
1196  * @dev: device to modify
1197  *
1198  * This must be called with the device_lock() already held, for example
1199  * from driver ->remove().
1200  */
1201 #define device_lock_reset_class(dev) \
1202 do { \
1203 	struct device *__d __maybe_unused = dev;                       \
1204 	lock_set_novalidate_class(&__d->mutex.dep_map, "&dev->mutex",  \
1205 				  _THIS_IP_);                          \
1206 } while (0)
1207 
1208 void lock_device_hotplug(void);
1209 void unlock_device_hotplug(void);
1210 int lock_device_hotplug_sysfs(void);
1211 int device_offline(struct device *dev);
1212 int device_online(struct device *dev);
1213 
1214 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
1215 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
1216 void device_set_node(struct device *dev, struct fwnode_handle *fwnode);
1217 int device_add_of_node(struct device *dev, struct device_node *of_node);
1218 void device_remove_of_node(struct device *dev);
1219 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2);
1220 struct device *get_dev_from_fwnode(struct fwnode_handle *fwnode);
1221 
1222 static inline struct device_node *dev_of_node(struct device *dev)
1223 {
1224 	if (!IS_ENABLED(CONFIG_OF) || !dev)
1225 		return NULL;
1226 	return dev->of_node;
1227 }
1228 
1229 static inline int dev_num_vf(struct device *dev)
1230 {
1231 	if (dev->bus && dev->bus->num_vf)
1232 		return dev->bus->num_vf(dev);
1233 	return 0;
1234 }
1235 
1236 /*
1237  * Root device objects for grouping under /sys/devices
1238  */
1239 struct device *__root_device_register(const char *name, struct module *owner);
1240 
1241 /* This is a macro to avoid include problems with THIS_MODULE */
1242 #define root_device_register(name) \
1243 	__root_device_register(name, THIS_MODULE)
1244 
1245 void root_device_unregister(struct device *root);
1246 
1247 static inline void *dev_get_platdata(const struct device *dev)
1248 {
1249 	return dev->platform_data;
1250 }
1251 
1252 /*
1253  * Manual binding of a device to driver. See drivers/base/bus.c
1254  * for information on use.
1255  */
1256 int __must_check device_driver_attach(const struct device_driver *drv,
1257 				      struct device *dev);
1258 int __must_check device_bind_driver(struct device *dev);
1259 void device_release_driver(struct device *dev);
1260 int  __must_check device_attach(struct device *dev);
1261 int __must_check driver_attach(const struct device_driver *drv);
1262 void device_initial_probe(struct device *dev);
1263 int __must_check device_reprobe(struct device *dev);
1264 
1265 bool device_is_bound(struct device *dev);
1266 
1267 /*
1268  * Easy functions for dynamically creating devices on the fly
1269  */
1270 __printf(5, 6) struct device *
1271 device_create(const struct class *cls, struct device *parent, dev_t devt,
1272 	      void *drvdata, const char *fmt, ...);
1273 __printf(6, 7) struct device *
1274 device_create_with_groups(const struct class *cls, struct device *parent, dev_t devt,
1275 			  void *drvdata, const struct attribute_group **groups,
1276 			  const char *fmt, ...);
1277 void device_destroy(const struct class *cls, dev_t devt);
1278 
1279 int __must_check device_add_groups(struct device *dev,
1280 				   const struct attribute_group *const *groups);
1281 void device_remove_groups(struct device *dev,
1282 			  const struct attribute_group *const *groups);
1283 
1284 static inline int __must_check device_add_group(struct device *dev,
1285 					const struct attribute_group *grp)
1286 {
1287 	const struct attribute_group *groups[] = { grp, NULL };
1288 
1289 	return device_add_groups(dev, groups);
1290 }
1291 
1292 static inline void device_remove_group(struct device *dev,
1293 				       const struct attribute_group *grp)
1294 {
1295 	const struct attribute_group *groups[] = { grp, NULL };
1296 
1297 	device_remove_groups(dev, groups);
1298 }
1299 
1300 int __must_check devm_device_add_group(struct device *dev,
1301 				       const struct attribute_group *grp);
1302 
1303 /*
1304  * get_device - atomically increment the reference count for the device.
1305  *
1306  */
1307 struct device *get_device(struct device *dev);
1308 void put_device(struct device *dev);
1309 
1310 DEFINE_FREE(put_device, struct device *, if (_T) put_device(_T))
1311 
1312 bool kill_device(struct device *dev);
1313 
1314 #ifdef CONFIG_DEVTMPFS
1315 int devtmpfs_mount(void);
1316 #else
1317 static inline int devtmpfs_mount(void) { return 0; }
1318 #endif
1319 
1320 /* drivers/base/power/shutdown.c */
1321 void device_shutdown(void);
1322 
1323 /* debugging and troubleshooting/diagnostic helpers. */
1324 const char *dev_driver_string(const struct device *dev);
1325 
1326 /* Device links interface. */
1327 struct device_link *device_link_add(struct device *consumer,
1328 				    struct device *supplier, u32 flags);
1329 void device_link_del(struct device_link *link);
1330 void device_link_remove(void *consumer, struct device *supplier);
1331 void device_links_supplier_sync_state_pause(void);
1332 void device_links_supplier_sync_state_resume(void);
1333 void device_link_wait_removal(void);
1334 
1335 static inline bool device_link_test(const struct device_link *link, u32 flags)
1336 {
1337 	return !!(link->flags & flags);
1338 }
1339 
1340 /* Create alias, so I can be autoloaded. */
1341 #define MODULE_ALIAS_CHARDEV(major,minor) \
1342 	MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
1343 #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
1344 	MODULE_ALIAS("char-major-" __stringify(major) "-*")
1345 
1346 #endif /* _DEVICE_H_ */
1347