Support virtio console device

This commit is contained in:
Yuke Peng
2023-11-07 22:13:15 -08:00
committed by Tate, Hongliang Tian
parent e9544d489f
commit 01e485b96e
16 changed files with 324 additions and 6 deletions

View File

@ -0,0 +1,16 @@
[package]
name = "jinux-console"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bitflags = "1.3"
spin = "0.9.4"
jinux-frame = { path = "../../../framework/jinux-frame" }
jinux-util = { path = "../../libs/jinux-util" }
component = { path = "../../libs/comp-sys/component" }
log = "0.4"
[features]

View File

@ -0,0 +1,71 @@
//! The console device of jinux
#![no_std]
#![forbid(unsafe_code)]
#![feature(fn_traits)]
extern crate alloc;
use alloc::{collections::BTreeMap, fmt::Debug, string::String, sync::Arc, vec::Vec};
use core::any::Any;
use component::{init_component, ComponentInitError};
use jinux_frame::sync::SpinLock;
use spin::Once;
pub type ConsoleCallback = dyn Fn(&[u8]) + Send + Sync;
pub trait AnyConsoleDevice: Send + Sync + Any + Debug {
fn send(&self, buf: &[u8]);
fn recv(&self, buf: &mut [u8]) -> Option<usize>;
fn register_callback(&self, callback: &'static ConsoleCallback);
fn handle_irq(&self);
}
pub fn register_device(name: String, device: Arc<dyn AnyConsoleDevice>) {
COMPONENT
.get()
.unwrap()
.console_table
.lock()
.insert(name, device);
}
pub fn get_device(str: &str) -> Option<Arc<dyn AnyConsoleDevice>> {
COMPONENT
.get()
.unwrap()
.console_table
.lock()
.get(str)
.cloned()
}
pub fn all_devices() -> Vec<(String, Arc<dyn AnyConsoleDevice>)> {
let consoles = COMPONENT.get().unwrap().console_table.lock();
consoles
.iter()
.map(|(name, device)| (name.clone(), device.clone()))
.collect()
}
static COMPONENT: Once<Component> = Once::new();
#[init_component]
fn component_init() -> Result<(), ComponentInitError> {
let a = Component::init()?;
COMPONENT.call_once(|| a);
Ok(())
}
#[derive(Debug)]
struct Component {
console_table: SpinLock<BTreeMap<String, Arc<dyn AnyConsoleDevice>>>,
}
impl Component {
pub fn init() -> Result<Self, ComponentInitError> {
Ok(Self {
console_table: SpinLock::new(BTreeMap::new()),
})
}
}