1pub mod block;
8pub mod char;
9pub mod events;
10pub mod fdt;
11pub mod graphics;
12pub mod input;
13pub mod manager;
14pub mod network;
15pub mod pci;
16pub mod platform;
17
18extern crate alloc;
19use core::any::Any;
20
21use crate::device::events::EventCapableDevice;
22use crate::object::capability::selectable::Selectable;
23use crate::object::capability::{ControlOps, MemoryMappingOps};
24use alloc::vec::Vec;
25
26#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
28pub enum DeviceCapability {
29 Tty,
31 Serial,
33}
34
35pub trait DeviceInfo {
36 fn name(&self) -> &'static str;
37 fn id(&self) -> usize;
38 fn compatible(&self) -> Vec<&'static str>;
39 fn as_any(&self) -> &dyn Any;
40}
41
42pub trait DeviceDriver: Send + Sync {
50 fn name(&self) -> &'static str;
51 fn match_table(&self) -> Vec<&'static str>;
52 fn probe(&self, device: &dyn DeviceInfo) -> Result<(), &'static str>;
53 fn remove(&self, device: &dyn DeviceInfo) -> Result<(), &'static str>;
54}
55
56#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
63pub enum DeviceType {
64 Block,
65 Char,
66 Network,
67 Graphics,
68 Generic,
69 #[cfg(test)]
70 NonExistent,
71}
72
73pub trait Device: Send + Sync + ControlOps + MemoryMappingOps + Selectable {
81 fn device_type(&self) -> DeviceType;
82 fn name(&self) -> &'static str;
83 fn as_any(&self) -> &dyn Any;
84 fn as_any_mut(&mut self) -> &mut dyn Any;
85
86 fn capabilities(&self) -> &'static [DeviceCapability] {
88 &[]
89 }
90
91 fn as_event_capable(&self) -> Option<&dyn EventCapableDevice> {
93 None
94 }
95
96 fn as_char_device(&self) -> Option<&dyn char::CharDevice> {
98 None
99 }
100
101 fn as_block_device(&self) -> Option<&dyn block::BlockDevice> {
103 None
104 }
105
106 fn as_graphics_device(&self) -> Option<&dyn graphics::GraphicsDevice> {
108 None
109 }
110
111 fn as_network_device(&self) -> Option<&dyn network::NetworkDevice> {
113 None
114 }
115
116 fn into_block_device(
119 self: alloc::sync::Arc<Self>,
120 ) -> Option<alloc::sync::Arc<dyn block::BlockDevice>> {
121 None
122 }
123
124 fn into_char_device(
127 self: alloc::sync::Arc<Self>,
128 ) -> Option<alloc::sync::Arc<dyn char::CharDevice>> {
129 None
130 }
131
132 fn into_graphics_device(
135 self: alloc::sync::Arc<Self>,
136 ) -> Option<alloc::sync::Arc<dyn graphics::GraphicsDevice>> {
137 None
138 }
139
140 fn into_network_device(
143 self: alloc::sync::Arc<Self>,
144 ) -> Option<alloc::sync::Arc<dyn network::NetworkDevice>> {
145 None
146 }
147}
148
149pub struct GenericDevice {
150 device_type: DeviceType,
151 name: &'static str,
152}
153
154impl GenericDevice {
155 pub fn new(name: &'static str) -> Self {
156 Self {
157 device_type: DeviceType::Generic,
158 name,
159 }
160 }
161}
162
163impl Device for GenericDevice {
164 fn device_type(&self) -> DeviceType {
165 self.device_type
166 }
167
168 fn name(&self) -> &'static str {
169 self.name
170 }
171
172 fn as_any(&self) -> &dyn Any {
173 self
174 }
175
176 fn as_any_mut(&mut self) -> &mut dyn Any {
177 self
178 }
179}
180
181impl Selectable for GenericDevice {
182 fn wait_until_ready(
183 &self,
184 _interest: crate::object::capability::selectable::ReadyInterest,
185 _trapframe: &mut crate::arch::Trapframe,
186 _timeout_ticks: Option<u64>,
187 ) -> crate::object::capability::selectable::SelectWaitOutcome {
188 crate::object::capability::selectable::SelectWaitOutcome::Ready
189 }
190}
191
192impl ControlOps for GenericDevice {
193 fn control(&self, _command: u32, _arg: usize) -> Result<i32, &'static str> {
195 Err("Control operations not supported")
196 }
197}
198
199impl MemoryMappingOps for GenericDevice {
200 fn get_mapping_info(
201 &self,
202 _offset: usize,
203 _length: usize,
204 ) -> Result<(usize, usize, bool), &'static str> {
205 Err("Memory mapping not supported by this generic device")
206 }
207
208 fn on_mapped(&self, _vaddr: usize, _paddr: usize, _length: usize, _offset: usize) {
209 }
211
212 fn on_unmapped(&self, _vaddr: usize, _length: usize) {
213 }
215
216 fn supports_mmap(&self) -> bool {
217 false
218 }
219}