1/* @title: Device Structure */
2#pragma once
3#include <stddef.h>
4#include <stdint.h>
5#include <structures/rbt.h>
6#include <sync/mutex.h>
7#include <sync/spinlock.h>
8#include <types/types.h>
9
10#define DEVICE_MAX_RESOURCES 8
11
12struct device;
13enum device_physical_location_panel {
14 DEVICE_PANEL_TOP,
15 DEVICE_PANEL_BOTTOM,
16 DEVICE_PANEL_LEFT,
17 DEVICE_PANEL_RIGHT,
18 DEVICE_PANEL_FRONT,
19 DEVICE_PANEL_BACK,
20 DEVICE_PANEL_UNKNOWN,
21};
22
23enum device_physical_location_vertical_position {
24 DEVICE_VERT_POS_UPPER,
25 DEVICE_VERT_POS_CENTER,
26 DEVICE_VERT_POS_LOWER,
27};
28
29enum device_physical_location_horizontal_position {
30 DEVICE_HORI_POS_LEFT,
31 DEVICE_HORI_POS_CENTER,
32 DEVICE_HORI_POS_RIGHT,
33};
34
35struct device_physical_location {
36 enum device_physical_location_panel panel;
37 enum device_physical_location_vertical_position vertical_position;
38 enum device_physical_location_horizontal_position horizontal_position;
39 bool dock;
40 bool lid;
41};
42
43enum device_bus_type {
44 DEVICE_BUS_TYPE_PLATFORM, /* pseudo-bus for SoC / board-level devices */
45 DEVICE_BUS_TYPE_PCI,
46 DEVICE_BUS_TYPE_USB,
47 DEVICE_BUS_TYPE_I2C,
48 DEVICE_BUS_TYPE_SPI,
49 DEVICE_BUS_TYPE_VIRTIO,
50 DEVICE_BUS_TYPE_ACPI,
51};
52
53enum device_type {
54 DEVICE_TYPE_UNKNOWN,
55 DEVICE_TYPE_BLOCK,
56 DEVICE_TYPE_CHAR,
57 DEVICE_TYPE_NET,
58 DEVICE_TYPE_INPUT,
59 DEVICE_TYPE_DISPLAY,
60 DEVICE_TYPE_AUDIO,
61 DEVICE_TYPE_SERIAL,
62 DEVICE_TYPE_GPU,
63 DEVICE_TYPE_TIMER,
64 DEVICE_TYPE_BUS, /* bridge / host controller */
65};
66
67enum device_power_state {
68 DEVICE_POWER_D0, /* fully operational */
69 DEVICE_POWER_D1, /* light sleep (clock-gated) */
70 DEVICE_POWER_D2, /* deeper sleep (power-gated, context kept) */
71 DEVICE_POWER_D3_HOT, /* off, aux power, software-recoverable */
72 DEVICE_POWER_D3_COLD, /* off, no power, full re-init required */
73 DEVICE_POWER_REMOVED, /* hot-unplugged */
74};
75
76enum device_state {
77 DEVICE_STATE_UNBOUND, /* in the tree but no driver attached */
78 DEVICE_STATE_PROBING, /* driver probe() in progress */
79 DEVICE_STATE_BOUND, /* driver matched and probe() succeeded */
80 DEVICE_STATE_SUSPENDED, /* low-power, context saved */
81 DEVICE_STATE_REMOVING, /* being torn down */
82 DEVICE_STATE_REMOVED, /* gone, awaiting final release */
83 DEVICE_STATE_ERROR, /* unrecoverable fault */
84};
85
86enum device_flags {
87 DEVICE_FLAG_HOTPLUG = (1u << 0), /* supports hot-plug/remove */
88 DEVICE_FLAG_DMA = (1u << 1), /* DMA-capable */
89 DEVICE_FLAG_MMIO = (1u << 2), /* memory-mapped I/O */
90 DEVICE_FLAG_PIO = (1u << 3), /* port I/O */
91 DEVICE_FLAG_MSI = (1u << 4), /* MSI / MSI-X capable */
92 DEVICE_FLAG_VIRTUAL = (1u << 5), /* software-only / emulated */
93 DEVICE_FLAG_REMOVABLE = (1u << 6), /* user-removable */
94 DEVICE_FLAG_WAKEUP = (1u << 7), /* can wake system from sleep */
95};
96
97struct device_driver {
98 const char *name;
99 enum device_bus_type bus;
100
101 enum errno (*probe)(struct device *dev);
102 void (*remove)(struct device *dev);
103 enum errno (*suspend)(struct device *dev, enum device_power_state target);
104 enum errno (*resume)(struct device *dev);
105};
106
107enum device_resource_type {
108 DEVICE_RESOURCE_MEM,
109 DEVICE_RESOURCE_IO,
110 DEVICE_RESOURCE_IRQ,
111 DEVICE_RESOURCE_DMA,
112};
113
114struct device_resource {
115 enum device_resource_type type;
116 uint64_t start;
117 uint64_t size; /* length in bytes / count for IRQ,DMA */
118 uint32_t flags;
119};
120
121struct device_ops {
122 void (*print)(struct device *);
123};
124
125struct device {
126 const char *name;
127 uint32_t id; /* unique device-model ID */
128 enum device_type type;
129 enum device_bus_type bus;
130
131 struct device *parent;
132 struct list_head children; /* Children list */
133 struct rbt_node tree_node; /* global device tree indexing */
134 struct list_head list_node; /* List node to attach to parent 'children' */
135
136 struct device_ops *ops;
137 struct device_driver *driver;
138 void *driver_data; /* opaque, owned by the driver */
139
140 enum device_state state;
141 enum device_power_state power;
142 enum device_flags flags;
143 refcount_t refcount;
144
145 struct device_resource resources[DEVICE_MAX_RESOURCES];
146 uint8_t resource_count;
147
148 numa_node_t numa_node;
149 struct device_physical_location location;
150
151 struct iommu_domain *iommu_domain;
152
153 struct mutex mutex;
154 struct spinlock lock;
155
156 void *private;
157};
158
159void device_init(struct device *dev);
160