kernel/device/
mod.rs

1//! Device module.
2//!
3//! This module provides a framework for managing devices in the kernel.
4//! It includes device information and driver management,
5//! as well as platform-specific device handling.
6
7pub 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/// Device capability flags for neutral feature discovery across ABIs
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
28pub enum DeviceCapability {
29    /// Device behaves like a terminal/TTY (byte stream with line discipline hooks)
30    Tty,
31    /// Device provides raw serial I/O (low-level byte stream, no line discipline)
32    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
42/// Device driver trait.
43///
44/// This trait defines the interface for device drivers in the kernel.
45/// It includes methods for getting the driver's name,
46/// matching the driver to devices, and handling device probing and removal.
47///
48/// All device drivers must be Send + Sync to be stored in global DeviceManager.
49pub 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/// Device type enumeration.
57///
58/// This enum defines the types of devices that can be managed by the kernel.
59/// It includes block devices, character devices, network devices,
60/// and generic devices.
61///
62#[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
73/// Device trait.
74///
75/// This trait defines the interface for devices in the kernel.
76/// Device IDs are assigned by DeviceManager when devices are registered.
77/// All devices must support control operations through the ControlOps trait
78/// and memory mapping operations through the MemoryMappingOps trait.
79///
80pub 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    /// Optional capabilities exposed by this device (default: none)
87    fn capabilities(&self) -> &'static [DeviceCapability] {
88        &[]
89    }
90
91    /// Cast to EventCapableDevice if this device can emit events
92    fn as_event_capable(&self) -> Option<&dyn EventCapableDevice> {
93        None
94    }
95
96    /// Cast to CharDevice if this device is a character device
97    fn as_char_device(&self) -> Option<&dyn char::CharDevice> {
98        None
99    }
100
101    /// Cast to BlockDevice if this device is a block device  
102    fn as_block_device(&self) -> Option<&dyn block::BlockDevice> {
103        None
104    }
105
106    /// Cast to GraphicsDevice if this device is a graphics device
107    fn as_graphics_device(&self) -> Option<&dyn graphics::GraphicsDevice> {
108        None
109    }
110
111    /// Cast to NetworkDevice if this device is a network device
112    fn as_network_device(&self) -> Option<&dyn network::NetworkDevice> {
113        None
114    }
115
116    /// Cast Arc<Self> to Arc<dyn BlockDevice> if this device is a block device
117    /// This allows direct ownership of the block device for efficient I/O operations
118    fn into_block_device(
119        self: alloc::sync::Arc<Self>,
120    ) -> Option<alloc::sync::Arc<dyn block::BlockDevice>> {
121        None
122    }
123
124    /// Cast Arc<Self> to Arc<dyn CharDevice> if this device is a character device
125    /// This allows direct ownership of the char device for efficient I/O operations
126    fn into_char_device(
127        self: alloc::sync::Arc<Self>,
128    ) -> Option<alloc::sync::Arc<dyn char::CharDevice>> {
129        None
130    }
131
132    /// Cast Arc<Self> to Arc<dyn GraphicsDevice> if this device is a graphics device
133    /// This allows direct ownership of the graphics device for efficient operations
134    fn into_graphics_device(
135        self: alloc::sync::Arc<Self>,
136    ) -> Option<alloc::sync::Arc<dyn graphics::GraphicsDevice>> {
137        None
138    }
139
140    /// Cast Arc<Self> to Arc<dyn NetworkDevice> if this device is a network device
141    /// This allows direct ownership of the network device for efficient operations
142    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    // Generic devices don't support control operations by default
194    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        // Generic devices don't support memory mapping
210    }
211
212    fn on_unmapped(&self, _vaddr: usize, _length: usize) {
213        // Generic devices don't support memory mapping
214    }
215
216    fn supports_mmap(&self) -> bool {
217        false
218    }
219}