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