Add poll support

This commit is contained in:
pjht 2024-09-20 14:41:55 -05:00
parent 650b92b3ae
commit 2f55bfdfa3
Signed by: pjht
GPG Key ID: CA239FC6934E6F3A
4 changed files with 84 additions and 16 deletions

14
Cargo.lock generated
View File

@ -28,6 +28,9 @@ name = "bitflags"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
dependencies = [
"serde",
]
[[package]]
name = "byteorder"
@ -87,6 +90,7 @@ checksum = "edd0f118536f44f5ccd48bcb8b111bdc3de888b58c74639dfb034a357d0f206d"
name = "file_rpc"
version = "0.1.0"
dependencies = [
"bitflags",
"parking_lot",
"postcard",
"serde",
@ -204,6 +208,7 @@ dependencies = [
"file_rpc",
"parking_lot",
"pc-keyboard",
"slab",
"syslog_rpc",
"x86_64",
]
@ -273,6 +278,15 @@ dependencies = [
"syn 2.0.77",
]
[[package]]
name = "slab"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67"
dependencies = [
"autocfg",
]
[[package]]
name = "smallvec"
version = "1.13.2"

View File

@ -10,5 +10,6 @@ devfs_rpc = { version = "0.1.0", path = "../devfs/devfs_rpc" }
file_rpc = { version = "0.1.0", path = "../file_rpc" }
parking_lot = "0.12.3"
pc-keyboard = "0.7.0"
slab = "0.4.9"
syslog_rpc = { version = "0.1.0", path = "../syslog/syslog_rpc" }
x86_64 = "0.15.1"

View File

@ -191,8 +191,6 @@ impl Ps2Controller {
}
}
pub fn initialize(&mut self) -> Result<(), Ps2InitFailure> {
let mut ports = self.ports.lock();
@ -276,18 +274,21 @@ impl Ps2Controller {
}
pub fn write_port1(&self, data: u8) {
self.ports.lock().send_command(Ps2ControllerCommand::WritePort1Input(data));
self.ports
.lock()
.send_command(Ps2ControllerCommand::WritePort1Input(data));
}
pub fn write_port2(&self, data: u8) {
self.ports.lock().send_command(Ps2ControllerCommand::WritePort2Input(data));
self.ports
.lock()
.send_command(Ps2ControllerCommand::WritePort2Input(data));
}
pub fn handle_interrupt(&self) {
let mut ports = self.ports.lock();
if ports.get_status().contains(Ps2Status::OutputBufFull) {
let data = unsafe {ports.data_port.read()};
let data = unsafe { ports.data_port.read() };
self.port1_output_bytes.lock().push_back(data);
}
}

View File

@ -1,10 +1,19 @@
mod controller;
use std::{collections::VecDeque, os::mikros::{ipc, syscalls}, sync::OnceLock};
use std::{
collections::VecDeque,
os::mikros::{ipc, syscalls},
sync::OnceLock,
usize,
};
use controller::Ps2Controller;
use file_rpc::PollEvents;
use parking_lot::Mutex;
use pc_keyboard::{layouts::Us104Key, EventDecoder, HandleControl, KeyCode, KeyEvent, ScancodeSet, ScancodeSet2};
use pc_keyboard::{
layouts::Us104Key, EventDecoder, HandleControl, KeyCode, KeyEvent, ScancodeSet, ScancodeSet2,
};
use slab::Slab;
static CONTROLLER: OnceLock<Ps2Controller> = OnceLock::new();
@ -18,7 +27,9 @@ impl dev_driver_rpc::Server for DevServ {
}
}
struct FileServ;
struct FileServ {
polls: Mutex<Slab<PollEvents>>,
}
impl file_rpc::Server for FileServ {
fn read(
@ -33,14 +44,14 @@ impl file_rpc::Server for FileServ {
while remaining_len > 0 {
let mut keypress_buf = KEYPRESS_BUF.lock();
let Some(c) = keypress_buf.pop_front() else {
break
break;
};
let str = c.encode_utf8(&mut encode_buf);
let bytes = str.as_bytes();
if bytes.len() > remaining_len {
keypress_buf.push_front(c);
break;
}
}
buf.extend_from_slice(bytes);
remaining_len -= bytes.len();
}
@ -56,14 +67,49 @@ impl file_rpc::Server for FileServ {
fn size(&self, _fd: u64) -> Option<u64> {
None
}
fn dup(&self, fd: u64) -> Option<u64> {
Some(fd)
}
fn register_poll(&self, _fd: u64, events: PollEvents) -> u64 {
self.polls.lock().insert(events) as u64
}
fn poll(&self, poll_id: u64) -> Option<PollEvents> {
let poll_id = poll_id as usize;
let req_events = *(self.polls.lock().get(poll_id)?);
if !req_events.contains(PollEvents::POLLIN) {
return None;
}
loop {
if !KEYPRESS_BUF.lock().is_empty() {
if !self.polls.lock().contains(poll_id) {
return None;
} else {
self.polls.lock().remove(poll_id);
return Some(PollEvents::POLLIN);
}
} else {
ipc::process_messages();
process_keyboard_data();
if !self.polls.lock().contains(poll_id) {
return None;
}
}
}
}
fn cancel_poll(&self, poll_id: u64) {
self.polls.lock().remove(poll_id as usize);
}
}
static SCANDODE_DECODER: Mutex<ScancodeSet2> = Mutex::new(ScancodeSet2::new());
static EVENT_DECODER: Mutex<EventDecoder<Us104Key>> = Mutex::new(EventDecoder::new(Us104Key, HandleControl::MapLettersToUnicode));
static EVENT_DECODER: Mutex<EventDecoder<Us104Key>> = Mutex::new(EventDecoder::new(
Us104Key,
HandleControl::MapLettersToUnicode,
));
fn process_keyboard_data() {
let mut scancode_decoder = SCANDODE_DECODER.lock();
@ -150,7 +196,9 @@ fn main() {
}
});
syslog_client.send_text_message("ps2", "Setting keyboard to scan code set 2").unwrap();
syslog_client
.send_text_message("ps2", "Setting keyboard to scan code set 2")
.unwrap();
controller.write_port1(0xF5);
loop {
@ -190,10 +238,14 @@ fn main() {
break;
}
syslog_client.send_text_message("ps2", "Set keyboard to scan code set 2").unwrap();
syslog_client
.send_text_message("ps2", "Set keyboard to scan code set 2")
.unwrap();
dev_driver_rpc::register_server(Box::new(DevServ));
file_rpc::register_server(Box::new(FileServ));
file_rpc::register_server(Box::new(FileServ {
polls: Mutex::new(Slab::new()),
}));
let devfs_pid;
loop {
if let Some(pid) = syscalls::try_get_registered(1) {