kernel/abi/xv6/drivers/
console.rs

1use core::any::Any;
2
3use crate::object::capability::selectable::Selectable;
4use crate::{
5    device::{Device, DeviceType, char::CharDevice, manager::DeviceManager},
6    object::capability::{ControlOps, MemoryMappingOps},
7};
8
9/// Character device for xv6 console that bridges to TTY
10pub struct ConsoleDevice {
11    id: usize,
12    name: &'static str,
13}
14
15impl ConsoleDevice {
16    pub fn new(id: usize, name: &'static str) -> Self {
17        Self { id, name }
18    }
19}
20
21impl Device for ConsoleDevice {
22    fn device_type(&self) -> DeviceType {
23        DeviceType::Char
24    }
25
26    fn name(&self) -> &'static str {
27        self.name
28    }
29
30    fn as_any(&self) -> &dyn Any {
31        self
32    }
33
34    fn as_any_mut(&mut self) -> &mut dyn Any {
35        self
36    }
37
38    fn as_char_device(&self) -> Option<&dyn CharDevice> {
39        Some(self)
40    }
41}
42
43impl CharDevice for ConsoleDevice {
44    fn read_byte(&self) -> Option<u8> {
45        // Bridge to TTY device instead of direct serial access
46        let device_manager = DeviceManager::get_manager();
47        if let Some(tty_device) = device_manager.get_device_by_name("tty0") {
48            return tty_device
49                .as_char_device()
50                .and_then(|char_device| char_device.read_byte());
51        }
52
53        // Fallback: return None if TTY is not available
54        None
55    }
56
57    fn write_byte(&self, byte: u8) -> Result<(), &'static str> {
58        // Bridge to TTY device instead of direct serial access
59        let device_manager = DeviceManager::get_manager();
60        if let Some(tty_device) = device_manager.get_device_by_name("tty0") {
61            if let Some(char_device) = tty_device.as_char_device() {
62                return char_device.write_byte(byte);
63            }
64        }
65
66        Err("TTY device not available")
67    }
68
69    fn can_read(&self) -> bool {
70        // Check TTY availability and read capability
71        let device_manager = DeviceManager::get_manager();
72        if let Some(tty_device) = device_manager.get_device_by_name("tty0") {
73            if let Some(char_device) = tty_device.as_char_device() {
74                return char_device.can_read();
75            }
76        }
77        false
78    }
79
80    fn can_write(&self) -> bool {
81        // Check TTY availability and write capability
82        let device_manager = DeviceManager::get_manager();
83        if let Some(tty_device) = device_manager.get_device_by_name("tty0") {
84            if let Some(char_device) = tty_device.as_char_device() {
85                return char_device.can_write();
86            }
87        }
88        false
89    }
90}
91
92impl ControlOps for ConsoleDevice {
93    // Console devices don't support control operations by default
94    fn control(&self, _command: u32, _arg: usize) -> Result<i32, &'static str> {
95        Err("Control operations not supported")
96    }
97}
98
99impl MemoryMappingOps for ConsoleDevice {
100    fn get_mapping_info(
101        &self,
102        _offset: usize,
103        _length: usize,
104    ) -> Result<(usize, usize, bool), &'static str> {
105        Err("Memory mapping not supported by console device")
106    }
107
108    fn on_mapped(&self, _vaddr: usize, _paddr: usize, _length: usize, _offset: usize) {
109        // Console devices don't support memory mapping
110    }
111
112    fn on_unmapped(&self, _vaddr: usize, _length: usize) {
113        // Console devices don't support memory mapping
114    }
115
116    fn supports_mmap(&self) -> bool {
117        false
118    }
119}
120
121impl Selectable for ConsoleDevice {
122    fn wait_until_ready(
123        &self,
124        _interest: crate::object::capability::selectable::ReadyInterest,
125        _trapframe: &mut crate::arch::Trapframe,
126        _timeout_ticks: Option<u64>,
127    ) -> crate::object::capability::selectable::SelectWaitOutcome {
128        crate::object::capability::selectable::SelectWaitOutcome::Ready
129    }
130}