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/overflow.h>
30#include <linux/device/bus.h>
31#include <linux/device/class.h>
32#include <linux/device/driver.h>
33#include <linux/cleanup.h>
34#include <asm/device.h>
35
36struct device;
37struct device_private;
38struct device_driver;
39struct driver_private;
40struct module;
41struct class;
42struct subsys_private;
43struct device_node;
44struct fwnode_handle;
45struct iommu_group;
46struct dev_pin_info;
47struct dev_iommu;
48struct 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 */
63struct 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
71int subsys_interface_register(struct subsys_interface *sif);
72void subsys_interface_unregister(struct subsys_interface *sif);
73
74int subsys_system_register(const struct bus_type *subsys,
75 const struct attribute_group **groups);
76int 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 */
88struct 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 */
105struct 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 */
118struct dev_ext_attribute {
119 struct device_attribute attr;
120 void *var;
121};
122
123ssize_t device_show_ulong(struct device *dev, struct device_attribute *attr,
124 char *buf);
125ssize_t device_store_ulong(struct device *dev, struct device_attribute *attr,
126 const char *buf, size_t count);
127ssize_t device_show_int(struct device *dev, struct device_attribute *attr,
128 char *buf);
129ssize_t device_store_int(struct device *dev, struct device_attribute *attr,
130 const char *buf, size_t count);
131ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
132 char *buf);
133ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
134 const char *buf, size_t count);
135
136/**
137 * DEVICE_ATTR - Define a device attribute.
138 * @_name: Attribute name.
139 * @_mode: File mode.
140 * @_show: Show handler. Optional, but mandatory if attribute is readable.
141 * @_store: Store handler. Optional, but mandatory if attribute is writable.
142 *
143 * Convenience macro for defining a struct device_attribute.
144 *
145 * For example, ``DEVICE_ATTR(foo, 0644, foo_show, foo_store);`` expands to:
146 *
147 * .. code-block:: c
148 *
149 * struct device_attribute dev_attr_foo = {
150 * .attr = { .name = "foo", .mode = 0644 },
151 * .show = foo_show,
152 * .store = foo_store,
153 * };
154 */
155#define DEVICE_ATTR(_name, _mode, _show, _store) \
156 struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
157
158/**
159 * DEVICE_ATTR_PREALLOC - Define a preallocated device attribute.
160 * @_name: Attribute name.
161 * @_mode: File mode.
162 * @_show: Show handler. Optional, but mandatory if attribute is readable.
163 * @_store: Store handler. Optional, but mandatory if attribute is writable.
164 *
165 * Like DEVICE_ATTR(), but ``SYSFS_PREALLOC`` is set on @_mode.
166 */
167#define DEVICE_ATTR_PREALLOC(_name, _mode, _show, _store) \
168 struct device_attribute dev_attr_##_name = \
169 __ATTR_PREALLOC(_name, _mode, _show, _store)
170
171/**
172 * DEVICE_ATTR_RW - Define a read-write device attribute.
173 * @_name: Attribute name.
174 *
175 * Like DEVICE_ATTR(), but @_mode is 0644, @_show is <_name>_show,
176 * and @_store is <_name>_store.
177 */
178#define DEVICE_ATTR_RW(_name) \
179 struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
180
181/**
182 * DEVICE_ATTR_ADMIN_RW - Define an admin-only read-write device attribute.
183 * @_name: Attribute name.
184 *
185 * Like DEVICE_ATTR_RW(), but @_mode is 0600.
186 */
187#define DEVICE_ATTR_ADMIN_RW(_name) \
188 struct device_attribute dev_attr_##_name = __ATTR_RW_MODE(_name, 0600)
189
190/**
191 * DEVICE_ATTR_RO - Define a readable device attribute.
192 * @_name: Attribute name.
193 *
194 * Like DEVICE_ATTR(), but @_mode is 0444 and @_show is <_name>_show.
195 */
196#define DEVICE_ATTR_RO(_name) \
197 struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
198
199/**
200 * DEVICE_ATTR_ADMIN_RO - Define an admin-only readable device attribute.
201 * @_name: Attribute name.
202 *
203 * Like DEVICE_ATTR_RO(), but @_mode is 0400.
204 */
205#define DEVICE_ATTR_ADMIN_RO(_name) \
206 struct device_attribute dev_attr_##_name = __ATTR_RO_MODE(_name, 0400)
207
208/**
209 * DEVICE_ATTR_WO - Define an admin-only writable device attribute.
210 * @_name: Attribute name.
211 *
212 * Like DEVICE_ATTR(), but @_mode is 0200 and @_store is <_name>_store.
213 */
214#define DEVICE_ATTR_WO(_name) \
215 struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
216
217/**
218 * DEVICE_ULONG_ATTR - Define a device attribute backed by an unsigned long.
219 * @_name: Attribute name.
220 * @_mode: File mode.
221 * @_var: Identifier of unsigned long.
222 *
223 * Like DEVICE_ATTR(), but @_show and @_store are automatically provided
224 * such that reads and writes to the attribute from userspace affect @_var.
225 */
226#define DEVICE_ULONG_ATTR(_name, _mode, _var) \
227 struct dev_ext_attribute dev_attr_##_name = \
228 { __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) }
229
230/**
231 * DEVICE_INT_ATTR - Define a device attribute backed by an int.
232 * @_name: Attribute name.
233 * @_mode: File mode.
234 * @_var: Identifier of int.
235 *
236 * Like DEVICE_ULONG_ATTR(), but @_var is an int.
237 */
238#define DEVICE_INT_ATTR(_name, _mode, _var) \
239 struct dev_ext_attribute dev_attr_##_name = \
240 { __ATTR(_name, _mode, device_show_int, device_store_int), &(_var) }
241
242/**
243 * DEVICE_BOOL_ATTR - Define a device attribute backed by a bool.
244 * @_name: Attribute name.
245 * @_mode: File mode.
246 * @_var: Identifier of bool.
247 *
248 * Like DEVICE_ULONG_ATTR(), but @_var is a bool.
249 */
250#define DEVICE_BOOL_ATTR(_name, _mode, _var) \
251 struct dev_ext_attribute dev_attr_##_name = \
252 { __ATTR(_name, _mode, device_show_bool, device_store_bool), &(_var) }
253
254#define DEVICE_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
255 struct device_attribute dev_attr_##_name = \
256 __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
257
258int device_create_file(struct device *device,
259 const struct device_attribute *entry);
260void device_remove_file(struct device *dev,
261 const struct device_attribute *attr);
262bool device_remove_file_self(struct device *dev,
263 const struct device_attribute *attr);
264int __must_check device_create_bin_file(struct device *dev,
265 const struct bin_attribute *attr);
266void device_remove_bin_file(struct device *dev,
267 const struct bin_attribute *attr);
268
269/* device resource management */
270typedef void (*dr_release_t)(struct device *dev, void *res);
271typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
272
273void *__devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp,
274 int nid, const char *name) __malloc;
275#define devres_alloc(release, size, gfp) \
276 __devres_alloc_node(release, size, gfp, NUMA_NO_NODE, #release)
277#define devres_alloc_node(release, size, gfp, nid) \
278 __devres_alloc_node(release, size, gfp, nid, #release)
279
280void devres_for_each_res(struct device *dev, dr_release_t release,
281 dr_match_t match, void *match_data,
282 void (*fn)(struct device *, void *, void *),
283 void *data);
284void devres_free(void *res);
285void devres_add(struct device *dev, void *res);
286void *devres_find(struct device *dev, dr_release_t release,
287 dr_match_t match, void *match_data);
288void *devres_get(struct device *dev, void *new_res,
289 dr_match_t match, void *match_data);
290void *devres_remove(struct device *dev, dr_release_t release,
291 dr_match_t match, void *match_data);
292int devres_destroy(struct device *dev, dr_release_t release,
293 dr_match_t match, void *match_data);
294int devres_release(struct device *dev, dr_release_t release,
295 dr_match_t match, void *match_data);
296
297/* devres group */
298void * __must_check devres_open_group(struct device *dev, void *id, gfp_t gfp);
299void devres_close_group(struct device *dev, void *id);
300void devres_remove_group(struct device *dev, void *id);
301int devres_release_group(struct device *dev, void *id);
302
303/* managed devm_k.alloc/kfree for device drivers */
304void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) __alloc_size(2);
305void *devm_krealloc(struct device *dev, void *ptr, size_t size,
306 gfp_t gfp) __must_check __realloc_size(3);
307__printf(3, 0) char *devm_kvasprintf(struct device *dev, gfp_t gfp,
308 const char *fmt, va_list ap) __malloc;
309__printf(3, 4) char *devm_kasprintf(struct device *dev, gfp_t gfp,
310 const char *fmt, ...) __malloc;
311static inline void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)
312{
313 return devm_kmalloc(dev, size, gfp: gfp | __GFP_ZERO);
314}
315static inline void *devm_kmalloc_array(struct device *dev,
316 size_t n, size_t size, gfp_t flags)
317{
318 size_t bytes;
319
320 if (unlikely(check_mul_overflow(n, size, &bytes)))
321 return NULL;
322
323 return devm_kmalloc(dev, size: bytes, gfp: flags);
324}
325static inline void *devm_kcalloc(struct device *dev,
326 size_t n, size_t size, gfp_t flags)
327{
328 return devm_kmalloc_array(dev, n, size, flags: flags | __GFP_ZERO);
329}
330static inline __realloc_size(3, 4) void * __must_check
331devm_krealloc_array(struct device *dev, void *p, size_t new_n, size_t new_size, gfp_t flags)
332{
333 size_t bytes;
334
335 if (unlikely(check_mul_overflow(new_n, new_size, &bytes)))
336 return NULL;
337
338 return devm_krealloc(dev, ptr: p, size: bytes, gfp: flags);
339}
340
341void devm_kfree(struct device *dev, const void *p);
342char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) __malloc;
343const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp);
344void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp)
345 __realloc_size(3);
346
347unsigned long devm_get_free_pages(struct device *dev,
348 gfp_t gfp_mask, unsigned int order);
349void devm_free_pages(struct device *dev, unsigned long addr);
350
351#ifdef CONFIG_HAS_IOMEM
352void __iomem *devm_ioremap_resource(struct device *dev,
353 const struct resource *res);
354void __iomem *devm_ioremap_resource_wc(struct device *dev,
355 const struct resource *res);
356
357void __iomem *devm_of_iomap(struct device *dev,
358 struct device_node *node, int index,
359 resource_size_t *size);
360#else
361
362static inline
363void __iomem *devm_ioremap_resource(struct device *dev,
364 const struct resource *res)
365{
366 return ERR_PTR(-EINVAL);
367}
368
369static inline
370void __iomem *devm_ioremap_resource_wc(struct device *dev,
371 const struct resource *res)
372{
373 return ERR_PTR(-EINVAL);
374}
375
376static inline
377void __iomem *devm_of_iomap(struct device *dev,
378 struct device_node *node, int index,
379 resource_size_t *size)
380{
381 return ERR_PTR(-EINVAL);
382}
383
384#endif
385
386/* allows to add/remove a custom action to devres stack */
387void devm_remove_action(struct device *dev, void (*action)(void *), void *data);
388void devm_release_action(struct device *dev, void (*action)(void *), void *data);
389
390int __devm_add_action(struct device *dev, void (*action)(void *), void *data, const char *name);
391#define devm_add_action(dev, action, data) \
392 __devm_add_action(dev, action, data, #action)
393
394static inline int __devm_add_action_or_reset(struct device *dev, void (*action)(void *),
395 void *data, const char *name)
396{
397 int ret;
398
399 ret = __devm_add_action(dev, action, data, name);
400 if (ret)
401 action(data);
402
403 return ret;
404}
405#define devm_add_action_or_reset(dev, action, data) \
406 __devm_add_action_or_reset(dev, action, data, #action)
407
408/**
409 * devm_alloc_percpu - Resource-managed alloc_percpu
410 * @dev: Device to allocate per-cpu memory for
411 * @type: Type to allocate per-cpu memory for
412 *
413 * Managed alloc_percpu. Per-cpu memory allocated with this function is
414 * automatically freed on driver detach.
415 *
416 * RETURNS:
417 * Pointer to allocated memory on success, NULL on failure.
418 */
419#define devm_alloc_percpu(dev, type) \
420 ((typeof(type) __percpu *)__devm_alloc_percpu((dev), sizeof(type), \
421 __alignof__(type)))
422
423void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
424 size_t align);
425void devm_free_percpu(struct device *dev, void __percpu *pdata);
426
427struct device_dma_parameters {
428 /*
429 * a low level driver may set these to teach IOMMU code about
430 * sg limitations.
431 */
432 unsigned int max_segment_size;
433 unsigned int min_align_mask;
434 unsigned long segment_boundary_mask;
435};
436
437/**
438 * enum device_link_state - Device link states.
439 * @DL_STATE_NONE: The presence of the drivers is not being tracked.
440 * @DL_STATE_DORMANT: None of the supplier/consumer drivers is present.
441 * @DL_STATE_AVAILABLE: The supplier driver is present, but the consumer is not.
442 * @DL_STATE_CONSUMER_PROBE: The consumer is probing (supplier driver present).
443 * @DL_STATE_ACTIVE: Both the supplier and consumer drivers are present.
444 * @DL_STATE_SUPPLIER_UNBIND: The supplier driver is unbinding.
445 */
446enum device_link_state {
447 DL_STATE_NONE = -1,
448 DL_STATE_DORMANT = 0,
449 DL_STATE_AVAILABLE,
450 DL_STATE_CONSUMER_PROBE,
451 DL_STATE_ACTIVE,
452 DL_STATE_SUPPLIER_UNBIND,
453};
454
455/*
456 * Device link flags.
457 *
458 * STATELESS: The core will not remove this link automatically.
459 * AUTOREMOVE_CONSUMER: Remove the link automatically on consumer driver unbind.
460 * PM_RUNTIME: If set, the runtime PM framework will use this link.
461 * RPM_ACTIVE: Run pm_runtime_get_sync() on the supplier during link creation.
462 * AUTOREMOVE_SUPPLIER: Remove the link automatically on supplier driver unbind.
463 * AUTOPROBE_CONSUMER: Probe consumer driver automatically after supplier binds.
464 * MANAGED: The core tracks presence of supplier/consumer drivers (internal).
465 * SYNC_STATE_ONLY: Link only affects sync_state() behavior.
466 * INFERRED: Inferred from data (eg: firmware) and not from driver actions.
467 */
468#define DL_FLAG_STATELESS BIT(0)
469#define DL_FLAG_AUTOREMOVE_CONSUMER BIT(1)
470#define DL_FLAG_PM_RUNTIME BIT(2)
471#define DL_FLAG_RPM_ACTIVE BIT(3)
472#define DL_FLAG_AUTOREMOVE_SUPPLIER BIT(4)
473#define DL_FLAG_AUTOPROBE_CONSUMER BIT(5)
474#define DL_FLAG_MANAGED BIT(6)
475#define DL_FLAG_SYNC_STATE_ONLY BIT(7)
476#define DL_FLAG_INFERRED BIT(8)
477#define DL_FLAG_CYCLE BIT(9)
478
479/**
480 * enum dl_dev_state - Device driver presence tracking information.
481 * @DL_DEV_NO_DRIVER: There is no driver attached to the device.
482 * @DL_DEV_PROBING: A driver is probing.
483 * @DL_DEV_DRIVER_BOUND: The driver has been bound to the device.
484 * @DL_DEV_UNBINDING: The driver is unbinding from the device.
485 */
486enum dl_dev_state {
487 DL_DEV_NO_DRIVER = 0,
488 DL_DEV_PROBING,
489 DL_DEV_DRIVER_BOUND,
490 DL_DEV_UNBINDING,
491};
492
493/**
494 * enum device_removable - Whether the device is removable. The criteria for a
495 * device to be classified as removable is determined by its subsystem or bus.
496 * @DEVICE_REMOVABLE_NOT_SUPPORTED: This attribute is not supported for this
497 * device (default).
498 * @DEVICE_REMOVABLE_UNKNOWN: Device location is Unknown.
499 * @DEVICE_FIXED: Device is not removable by the user.
500 * @DEVICE_REMOVABLE: Device is removable by the user.
501 */
502enum device_removable {
503 DEVICE_REMOVABLE_NOT_SUPPORTED = 0, /* must be 0 */
504 DEVICE_REMOVABLE_UNKNOWN,
505 DEVICE_FIXED,
506 DEVICE_REMOVABLE,
507};
508
509/**
510 * struct dev_links_info - Device data related to device links.
511 * @suppliers: List of links to supplier devices.
512 * @consumers: List of links to consumer devices.
513 * @defer_sync: Hook to global list of devices that have deferred sync_state.
514 * @status: Driver status information.
515 */
516struct dev_links_info {
517 struct list_head suppliers;
518 struct list_head consumers;
519 struct list_head defer_sync;
520 enum dl_dev_state status;
521};
522
523/**
524 * struct dev_msi_info - Device data related to MSI
525 * @domain: The MSI interrupt domain associated to the device
526 * @data: Pointer to MSI device data
527 */
528struct dev_msi_info {
529#ifdef CONFIG_GENERIC_MSI_IRQ
530 struct irq_domain *domain;
531 struct msi_device_data *data;
532#endif
533};
534
535/**
536 * enum device_physical_location_panel - Describes which panel surface of the
537 * system's housing the device connection point resides on.
538 * @DEVICE_PANEL_TOP: Device connection point is on the top panel.
539 * @DEVICE_PANEL_BOTTOM: Device connection point is on the bottom panel.
540 * @DEVICE_PANEL_LEFT: Device connection point is on the left panel.
541 * @DEVICE_PANEL_RIGHT: Device connection point is on the right panel.
542 * @DEVICE_PANEL_FRONT: Device connection point is on the front panel.
543 * @DEVICE_PANEL_BACK: Device connection point is on the back panel.
544 * @DEVICE_PANEL_UNKNOWN: The panel with device connection point is unknown.
545 */
546enum device_physical_location_panel {
547 DEVICE_PANEL_TOP,
548 DEVICE_PANEL_BOTTOM,
549 DEVICE_PANEL_LEFT,
550 DEVICE_PANEL_RIGHT,
551 DEVICE_PANEL_FRONT,
552 DEVICE_PANEL_BACK,
553 DEVICE_PANEL_UNKNOWN,
554};
555
556/**
557 * enum device_physical_location_vertical_position - Describes vertical
558 * position of the device connection point on the panel surface.
559 * @DEVICE_VERT_POS_UPPER: Device connection point is at upper part of panel.
560 * @DEVICE_VERT_POS_CENTER: Device connection point is at center part of panel.
561 * @DEVICE_VERT_POS_LOWER: Device connection point is at lower part of panel.
562 */
563enum device_physical_location_vertical_position {
564 DEVICE_VERT_POS_UPPER,
565 DEVICE_VERT_POS_CENTER,
566 DEVICE_VERT_POS_LOWER,
567};
568
569/**
570 * enum device_physical_location_horizontal_position - Describes horizontal
571 * position of the device connection point on the panel surface.
572 * @DEVICE_HORI_POS_LEFT: Device connection point is at left part of panel.
573 * @DEVICE_HORI_POS_CENTER: Device connection point is at center part of panel.
574 * @DEVICE_HORI_POS_RIGHT: Device connection point is at right part of panel.
575 */
576enum device_physical_location_horizontal_position {
577 DEVICE_HORI_POS_LEFT,
578 DEVICE_HORI_POS_CENTER,
579 DEVICE_HORI_POS_RIGHT,
580};
581
582/**
583 * struct device_physical_location - Device data related to physical location
584 * of the device connection point.
585 * @panel: Panel surface of the system's housing that the device connection
586 * point resides on.
587 * @vertical_position: Vertical position of the device connection point within
588 * the panel.
589 * @horizontal_position: Horizontal position of the device connection point
590 * within the panel.
591 * @dock: Set if the device connection point resides in a docking station or
592 * port replicator.
593 * @lid: Set if this device connection point resides on the lid of laptop
594 * system.
595 */
596struct device_physical_location {
597 enum device_physical_location_panel panel;
598 enum device_physical_location_vertical_position vertical_position;
599 enum device_physical_location_horizontal_position horizontal_position;
600 bool dock;
601 bool lid;
602};
603
604/**
605 * struct device - The basic device structure
606 * @parent: The device's "parent" device, the device to which it is attached.
607 * In most cases, a parent device is some sort of bus or host
608 * controller. If parent is NULL, the device, is a top-level device,
609 * which is not usually what you want.
610 * @p: Holds the private data of the driver core portions of the device.
611 * See the comment of the struct device_private for detail.
612 * @kobj: A top-level, abstract class from which other classes are derived.
613 * @init_name: Initial name of the device.
614 * @type: The type of device.
615 * This identifies the device type and carries type-specific
616 * information.
617 * @mutex: Mutex to synchronize calls to its driver.
618 * @bus: Type of bus device is on.
619 * @driver: Which driver has allocated this
620 * @platform_data: Platform data specific to the device.
621 * Example: For devices on custom boards, as typical of embedded
622 * and SOC based hardware, Linux often uses platform_data to point
623 * to board-specific structures describing devices and how they
624 * are wired. That can include what ports are available, chip
625 * variants, which GPIO pins act in what additional roles, and so
626 * on. This shrinks the "Board Support Packages" (BSPs) and
627 * minimizes board-specific #ifdefs in drivers.
628 * @driver_data: Private pointer for driver specific info.
629 * @links: Links to suppliers and consumers of this device.
630 * @power: For device power management.
631 * See Documentation/driver-api/pm/devices.rst for details.
632 * @pm_domain: Provide callbacks that are executed during system suspend,
633 * hibernation, system resume and during runtime PM transitions
634 * along with subsystem-level and driver-level callbacks.
635 * @em_pd: device's energy model performance domain
636 * @pins: For device pin management.
637 * See Documentation/driver-api/pin-control.rst for details.
638 * @msi: MSI related data
639 * @numa_node: NUMA node this device is close to.
640 * @dma_ops: DMA mapping operations for this device.
641 * @dma_mask: Dma mask (if dma'ble device).
642 * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all
643 * hardware supports 64-bit addresses for consistent allocations
644 * such descriptors.
645 * @bus_dma_limit: Limit of an upstream bridge or bus which imposes a smaller
646 * DMA limit than the device itself supports.
647 * @dma_range_map: map for DMA memory ranges relative to that of RAM
648 * @dma_parms: A low level driver may set these to teach IOMMU code about
649 * segment limitations.
650 * @dma_pools: Dma pools (if dma'ble device).
651 * @dma_mem: Internal for coherent mem override.
652 * @cma_area: Contiguous memory area for dma allocations
653 * @dma_io_tlb_mem: Software IO TLB allocator. Not for driver use.
654 * @dma_io_tlb_pools: List of transient swiotlb memory pools.
655 * @dma_io_tlb_lock: Protects changes to the list of active pools.
656 * @dma_uses_io_tlb: %true if device has used the software IO TLB.
657 * @archdata: For arch-specific additions.
658 * @of_node: Associated device tree node.
659 * @fwnode: Associated device node supplied by platform firmware.
660 * @devt: For creating the sysfs "dev".
661 * @id: device instance
662 * @devres_lock: Spinlock to protect the resource of the device.
663 * @devres_head: The resources list of the device.
664 * @class: The class of the device.
665 * @groups: Optional attribute groups.
666 * @release: Callback to free the device after all references have
667 * gone away. This should be set by the allocator of the
668 * device (i.e. the bus driver that discovered the device).
669 * @iommu_group: IOMMU group the device belongs to.
670 * @iommu: Per device generic IOMMU runtime data
671 * @physical_location: Describes physical location of the device connection
672 * point in the system housing.
673 * @removable: Whether the device can be removed from the system. This
674 * should be set by the subsystem / bus driver that discovered
675 * the device.
676 *
677 * @offline_disabled: If set, the device is permanently online.
678 * @offline: Set after successful invocation of bus type's .offline().
679 * @of_node_reused: Set if the device-tree node is shared with an ancestor
680 * device.
681 * @state_synced: The hardware state of this device has been synced to match
682 * the software state of this device by calling the driver/bus
683 * sync_state() callback.
684 * @can_match: The device has matched with a driver at least once or it is in
685 * a bus (like AMBA) which can't check for matching drivers until
686 * other devices probe successfully.
687 * @dma_coherent: this particular device is dma coherent, even if the
688 * architecture supports non-coherent devices.
689 * @dma_ops_bypass: If set to %true then the dma_ops are bypassed for the
690 * streaming DMA operations (->map_* / ->unmap_* / ->sync_*),
691 * and optionall (if the coherent mask is large enough) also
692 * for dma allocations. This flag is managed by the dma ops
693 * instance from ->dma_supported.
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 */
703struct 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 mutex mutex; /* mutex to synchronize calls to
720 * its driver.
721 */
722
723 struct dev_links_info links;
724 struct dev_pm_info power;
725 struct dev_pm_domain *pm_domain;
726
727#ifdef CONFIG_ENERGY_MODEL
728 struct em_perf_domain *em_pd;
729#endif
730
731#ifdef CONFIG_PINCTRL
732 struct dev_pin_info *pins;
733#endif
734 struct dev_msi_info msi;
735#ifdef CONFIG_DMA_OPS
736 const struct dma_map_ops *dma_ops;
737#endif
738 u64 *dma_mask; /* dma mask (if dma'able device) */
739 u64 coherent_dma_mask;/* Like dma_mask, but for
740 alloc_coherent mappings as
741 not all hardware supports
742 64 bit addresses for consistent
743 allocations such descriptors. */
744 u64 bus_dma_limit; /* upstream dma constraint */
745 const struct bus_dma_region *dma_range_map;
746
747 struct device_dma_parameters *dma_parms;
748
749 struct list_head dma_pools; /* dma pools (if dma'ble) */
750
751#ifdef CONFIG_DMA_DECLARE_COHERENT
752 struct dma_coherent_mem *dma_mem; /* internal for coherent mem
753 override */
754#endif
755#ifdef CONFIG_DMA_CMA
756 struct cma *cma_area; /* contiguous memory area for dma
757 allocations */
758#endif
759#ifdef CONFIG_SWIOTLB
760 struct io_tlb_mem *dma_io_tlb_mem;
761#endif
762#ifdef CONFIG_SWIOTLB_DYNAMIC
763 struct list_head dma_io_tlb_pools;
764 spinlock_t dma_io_tlb_lock;
765 bool dma_uses_io_tlb;
766#endif
767 /* arch specific additions */
768 struct dev_archdata archdata;
769
770 struct device_node *of_node; /* associated device tree node */
771 struct fwnode_handle *fwnode; /* firmware device node */
772
773#ifdef CONFIG_NUMA
774 int numa_node; /* NUMA node this device is close to */
775#endif
776 dev_t devt; /* dev_t, creates the sysfs "dev" */
777 u32 id; /* device instance */
778
779 spinlock_t devres_lock;
780 struct list_head devres_head;
781
782 const struct class *class;
783 const struct attribute_group **groups; /* optional groups */
784
785 void (*release)(struct device *dev);
786 struct iommu_group *iommu_group;
787 struct dev_iommu *iommu;
788
789 struct device_physical_location *physical_location;
790
791 enum device_removable removable;
792
793 bool offline_disabled:1;
794 bool offline:1;
795 bool of_node_reused:1;
796 bool state_synced:1;
797 bool can_match:1;
798#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
799 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
800 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
801 bool dma_coherent:1;
802#endif
803#ifdef CONFIG_DMA_OPS_BYPASS
804 bool dma_ops_bypass : 1;
805#endif
806};
807
808/**
809 * struct device_link - Device link representation.
810 * @supplier: The device on the supplier end of the link.
811 * @s_node: Hook to the supplier device's list of links to consumers.
812 * @consumer: The device on the consumer end of the link.
813 * @c_node: Hook to the consumer device's list of links to suppliers.
814 * @link_dev: device used to expose link details in sysfs
815 * @status: The state of the link (with respect to the presence of drivers).
816 * @flags: Link flags.
817 * @rpm_active: Whether or not the consumer device is runtime-PM-active.
818 * @kref: Count repeated addition of the same link.
819 * @rm_work: Work structure used for removing the link.
820 * @supplier_preactivated: Supplier has been made active before consumer probe.
821 */
822struct device_link {
823 struct device *supplier;
824 struct list_head s_node;
825 struct device *consumer;
826 struct list_head c_node;
827 struct device link_dev;
828 enum device_link_state status;
829 u32 flags;
830 refcount_t rpm_active;
831 struct kref kref;
832 struct work_struct rm_work;
833 bool supplier_preactivated; /* Owned by consumer probe. */
834};
835
836#define kobj_to_dev(__kobj) container_of_const(__kobj, struct device, kobj)
837
838/**
839 * device_iommu_mapped - Returns true when the device DMA is translated
840 * by an IOMMU
841 * @dev: Device to perform the check on
842 */
843static inline bool device_iommu_mapped(struct device *dev)
844{
845 return (dev->iommu_group != NULL);
846}
847
848/* Get the wakeup routines, which depend on struct device */
849#include <linux/pm_wakeup.h>
850
851/**
852 * dev_name - Return a device's name.
853 * @dev: Device with name to get.
854 * Return: The kobject name of the device, or its initial name if unavailable.
855 */
856static inline const char *dev_name(const struct device *dev)
857{
858 /* Use the init name until the kobject becomes available */
859 if (dev->init_name)
860 return dev->init_name;
861
862 return kobject_name(kobj: &dev->kobj);
863}
864
865/**
866 * dev_bus_name - Return a device's bus/class name, if at all possible
867 * @dev: struct device to get the bus/class name of
868 *
869 * Will return the name of the bus/class the device is attached to. If it is
870 * not attached to a bus/class, an empty string will be returned.
871 */
872static inline const char *dev_bus_name(const struct device *dev)
873{
874 return dev->bus ? dev->bus->name : (dev->class ? dev->class->name : "");
875}
876
877__printf(2, 3) int dev_set_name(struct device *dev, const char *name, ...);
878
879#ifdef CONFIG_NUMA
880static inline int dev_to_node(struct device *dev)
881{
882 return dev->numa_node;
883}
884static inline void set_dev_node(struct device *dev, int node)
885{
886 dev->numa_node = node;
887}
888#else
889static inline int dev_to_node(struct device *dev)
890{
891 return NUMA_NO_NODE;
892}
893static inline void set_dev_node(struct device *dev, int node)
894{
895}
896#endif
897
898static inline struct irq_domain *dev_get_msi_domain(const struct device *dev)
899{
900#ifdef CONFIG_GENERIC_MSI_IRQ
901 return dev->msi.domain;
902#else
903 return NULL;
904#endif
905}
906
907static inline void dev_set_msi_domain(struct device *dev, struct irq_domain *d)
908{
909#ifdef CONFIG_GENERIC_MSI_IRQ
910 dev->msi.domain = d;
911#endif
912}
913
914static inline void *dev_get_drvdata(const struct device *dev)
915{
916 return dev->driver_data;
917}
918
919static inline void dev_set_drvdata(struct device *dev, void *data)
920{
921 dev->driver_data = data;
922}
923
924static inline struct pm_subsys_data *dev_to_psd(struct device *dev)
925{
926 return dev ? dev->power.subsys_data : NULL;
927}
928
929static inline unsigned int dev_get_uevent_suppress(const struct device *dev)
930{
931 return dev->kobj.uevent_suppress;
932}
933
934static inline void dev_set_uevent_suppress(struct device *dev, int val)
935{
936 dev->kobj.uevent_suppress = val;
937}
938
939static inline int device_is_registered(struct device *dev)
940{
941 return dev->kobj.state_in_sysfs;
942}
943
944static inline void device_enable_async_suspend(struct device *dev)
945{
946 if (!dev->power.is_prepared)
947 dev->power.async_suspend = true;
948}
949
950static inline void device_disable_async_suspend(struct device *dev)
951{
952 if (!dev->power.is_prepared)
953 dev->power.async_suspend = false;
954}
955
956static inline bool device_async_suspend_enabled(struct device *dev)
957{
958 return !!dev->power.async_suspend;
959}
960
961static inline bool device_pm_not_required(struct device *dev)
962{
963 return dev->power.no_pm;
964}
965
966static inline void device_set_pm_not_required(struct device *dev)
967{
968 dev->power.no_pm = true;
969}
970
971static inline void dev_pm_syscore_device(struct device *dev, bool val)
972{
973#ifdef CONFIG_PM_SLEEP
974 dev->power.syscore = val;
975#endif
976}
977
978static inline void dev_pm_set_driver_flags(struct device *dev, u32 flags)
979{
980 dev->power.driver_flags = flags;
981}
982
983static inline bool dev_pm_test_driver_flags(struct device *dev, u32 flags)
984{
985 return !!(dev->power.driver_flags & flags);
986}
987
988static inline void device_lock(struct device *dev)
989{
990 mutex_lock(&dev->mutex);
991}
992
993static inline int device_lock_interruptible(struct device *dev)
994{
995 return mutex_lock_interruptible(&dev->mutex);
996}
997
998static inline int device_trylock(struct device *dev)
999{
1000 return mutex_trylock(lock: &dev->mutex);
1001}
1002
1003static inline void device_unlock(struct device *dev)
1004{
1005 mutex_unlock(lock: &dev->mutex);
1006}
1007
1008DEFINE_GUARD(device, struct device *, device_lock(_T), device_unlock(_T))
1009
1010static inline void device_lock_assert(struct device *dev)
1011{
1012 lockdep_assert_held(&dev->mutex);
1013}
1014
1015static inline struct device_node *dev_of_node(struct device *dev)
1016{
1017 if (!IS_ENABLED(CONFIG_OF) || !dev)
1018 return NULL;
1019 return dev->of_node;
1020}
1021
1022static inline bool dev_has_sync_state(struct device *dev)
1023{
1024 if (!dev)
1025 return false;
1026 if (dev->driver && dev->driver->sync_state)
1027 return true;
1028 if (dev->bus && dev->bus->sync_state)
1029 return true;
1030 return false;
1031}
1032
1033static inline void dev_set_removable(struct device *dev,
1034 enum device_removable removable)
1035{
1036 dev->removable = removable;
1037}
1038
1039static inline bool dev_is_removable(struct device *dev)
1040{
1041 return dev->removable == DEVICE_REMOVABLE;
1042}
1043
1044static inline bool dev_removable_is_valid(struct device *dev)
1045{
1046 return dev->removable != DEVICE_REMOVABLE_NOT_SUPPORTED;
1047}
1048
1049/*
1050 * High level routines for use by the bus drivers
1051 */
1052int __must_check device_register(struct device *dev);
1053void device_unregister(struct device *dev);
1054void device_initialize(struct device *dev);
1055int __must_check device_add(struct device *dev);
1056void device_del(struct device *dev);
1057
1058DEFINE_FREE(device_del, struct device *, if (_T) device_del(_T))
1059
1060int device_for_each_child(struct device *dev, void *data,
1061 int (*fn)(struct device *dev, void *data));
1062int device_for_each_child_reverse(struct device *dev, void *data,
1063 int (*fn)(struct device *dev, void *data));
1064struct device *device_find_child(struct device *dev, void *data,
1065 int (*match)(struct device *dev, void *data));
1066struct device *device_find_child_by_name(struct device *parent,
1067 const char *name);
1068struct device *device_find_any_child(struct device *parent);
1069
1070int device_rename(struct device *dev, const char *new_name);
1071int device_move(struct device *dev, struct device *new_parent,
1072 enum dpm_order dpm_order);
1073int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid);
1074
1075static inline bool device_supports_offline(struct device *dev)
1076{
1077 return dev->bus && dev->bus->offline && dev->bus->online;
1078}
1079
1080#define __device_lock_set_class(dev, name, key) \
1081do { \
1082 struct device *__d2 __maybe_unused = dev; \
1083 lock_set_class(&__d2->mutex.dep_map, name, key, 0, _THIS_IP_); \
1084} while (0)
1085
1086/**
1087 * device_lock_set_class - Specify a temporary lock class while a device
1088 * is attached to a driver
1089 * @dev: device to modify
1090 * @key: lock class key data
1091 *
1092 * This must be called with the device_lock() already held, for example
1093 * from driver ->probe(). Take care to only override the default
1094 * lockdep_no_validate class.
1095 */
1096#ifdef CONFIG_LOCKDEP
1097#define device_lock_set_class(dev, key) \
1098do { \
1099 struct device *__d = dev; \
1100 dev_WARN_ONCE(__d, !lockdep_match_class(&__d->mutex, \
1101 &__lockdep_no_validate__), \
1102 "overriding existing custom lock class\n"); \
1103 __device_lock_set_class(__d, #key, key); \
1104} while (0)
1105#else
1106#define device_lock_set_class(dev, key) __device_lock_set_class(dev, #key, key)
1107#endif
1108
1109/**
1110 * device_lock_reset_class - Return a device to the default lockdep novalidate state
1111 * @dev: device to modify
1112 *
1113 * This must be called with the device_lock() already held, for example
1114 * from driver ->remove().
1115 */
1116#define device_lock_reset_class(dev) \
1117do { \
1118 struct device *__d __maybe_unused = dev; \
1119 lock_set_novalidate_class(&__d->mutex.dep_map, "&dev->mutex", \
1120 _THIS_IP_); \
1121} while (0)
1122
1123void lock_device_hotplug(void);
1124void unlock_device_hotplug(void);
1125int lock_device_hotplug_sysfs(void);
1126int device_offline(struct device *dev);
1127int device_online(struct device *dev);
1128void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
1129void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
1130void device_set_of_node_from_dev(struct device *dev, const struct device *dev2);
1131void device_set_node(struct device *dev, struct fwnode_handle *fwnode);
1132
1133static inline int dev_num_vf(struct device *dev)
1134{
1135 if (dev->bus && dev->bus->num_vf)
1136 return dev->bus->num_vf(dev);
1137 return 0;
1138}
1139
1140/*
1141 * Root device objects for grouping under /sys/devices
1142 */
1143struct device *__root_device_register(const char *name, struct module *owner);
1144
1145/* This is a macro to avoid include problems with THIS_MODULE */
1146#define root_device_register(name) \
1147 __root_device_register(name, THIS_MODULE)
1148
1149void root_device_unregister(struct device *root);
1150
1151static inline void *dev_get_platdata(const struct device *dev)
1152{
1153 return dev->platform_data;
1154}
1155
1156/*
1157 * Manual binding of a device to driver. See drivers/base/bus.c
1158 * for information on use.
1159 */
1160int __must_check device_driver_attach(struct device_driver *drv,
1161 struct device *dev);
1162int __must_check device_bind_driver(struct device *dev);
1163void device_release_driver(struct device *dev);
1164int __must_check device_attach(struct device *dev);
1165int __must_check driver_attach(struct device_driver *drv);
1166void device_initial_probe(struct device *dev);
1167int __must_check device_reprobe(struct device *dev);
1168
1169bool device_is_bound(struct device *dev);
1170
1171/*
1172 * Easy functions for dynamically creating devices on the fly
1173 */
1174__printf(5, 6) struct device *
1175device_create(const struct class *cls, struct device *parent, dev_t devt,
1176 void *drvdata, const char *fmt, ...);
1177__printf(6, 7) struct device *
1178device_create_with_groups(const struct class *cls, struct device *parent, dev_t devt,
1179 void *drvdata, const struct attribute_group **groups,
1180 const char *fmt, ...);
1181void device_destroy(const struct class *cls, dev_t devt);
1182
1183int __must_check device_add_groups(struct device *dev,
1184 const struct attribute_group **groups);
1185void device_remove_groups(struct device *dev,
1186 const struct attribute_group **groups);
1187
1188static inline int __must_check device_add_group(struct device *dev,
1189 const struct attribute_group *grp)
1190{
1191 const struct attribute_group *groups[] = { grp, NULL };
1192
1193 return device_add_groups(dev, groups);
1194}
1195
1196static inline void device_remove_group(struct device *dev,
1197 const struct attribute_group *grp)
1198{
1199 const struct attribute_group *groups[] = { grp, NULL };
1200
1201 return device_remove_groups(dev, groups);
1202}
1203
1204int __must_check devm_device_add_groups(struct device *dev,
1205 const struct attribute_group **groups);
1206int __must_check devm_device_add_group(struct device *dev,
1207 const struct attribute_group *grp);
1208
1209/*
1210 * Platform "fixup" functions - allow the platform to have their say
1211 * about devices and actions that the general device layer doesn't
1212 * know about.
1213 */
1214/* Notify platform of device discovery */
1215extern int (*platform_notify)(struct device *dev);
1216
1217extern int (*platform_notify_remove)(struct device *dev);
1218
1219
1220/*
1221 * get_device - atomically increment the reference count for the device.
1222 *
1223 */
1224struct device *get_device(struct device *dev);
1225void put_device(struct device *dev);
1226
1227DEFINE_FREE(put_device, struct device *, if (_T) put_device(_T))
1228
1229bool kill_device(struct device *dev);
1230
1231#ifdef CONFIG_DEVTMPFS
1232int devtmpfs_mount(void);
1233#else
1234static inline int devtmpfs_mount(void) { return 0; }
1235#endif
1236
1237/* drivers/base/power/shutdown.c */
1238void device_shutdown(void);
1239
1240/* debugging and troubleshooting/diagnostic helpers. */
1241const char *dev_driver_string(const struct device *dev);
1242
1243/* Device links interface. */
1244struct device_link *device_link_add(struct device *consumer,
1245 struct device *supplier, u32 flags);
1246void device_link_del(struct device_link *link);
1247void device_link_remove(void *consumer, struct device *supplier);
1248void device_links_supplier_sync_state_pause(void);
1249void device_links_supplier_sync_state_resume(void);
1250void device_link_wait_removal(void);
1251
1252/* Create alias, so I can be autoloaded. */
1253#define MODULE_ALIAS_CHARDEV(major,minor) \
1254 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
1255#define MODULE_ALIAS_CHARDEV_MAJOR(major) \
1256 MODULE_ALIAS("char-major-" __stringify(major) "-*")
1257
1258#endif /* _DEVICE_H_ */
1259

source code of linux/include/linux/device.h