-
Notifications
You must be signed in to change notification settings - Fork 129
feat: add PS/2 keyboard interrupt driver #2532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
GloriousAlpaca
wants to merge
8
commits into
hermit-os:main
Choose a base branch
from
GloriousAlpaca:pr-keyboard-clean
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f59db0c
feat: add PS/2 keyboard interrupt driver
333866e
refactor: rename ps2 keyboard driver to pc-keyboard
1446146
refactor: use mutex with vecdeque instead of atomic ringbuffer
b8bb4aa
refactor: ps2 controller port access abstraction
af7acf0
style: change abstraction functions to oneliners
23313e4
refactor: systemcall inspired by linux design
9afbade
style: change Ps2 Commands to enums, add Ps2 Port test
3c3302d
style: safety comments, syscall doc comment, handler function simplif…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| use alloc::collections::VecDeque; | ||
| use core::num::NonZeroU8; | ||
|
|
||
| use hermit_sync::{InterruptTicketMutex, Lazy}; | ||
| use x86_64::instructions::port::Port; | ||
|
|
||
| use crate::arch::kernel::interrupts; | ||
| use crate::synch::semaphore::Semaphore; | ||
|
|
||
| const PS2_DATA_PORT: u16 = 0x60; | ||
| const PS2_CMD_PORT: u16 = 0x64; | ||
|
|
||
| #[repr(u8)] | ||
| enum Ps2Command { | ||
| ReadConfig = 0x20, | ||
| WriteConfig = 0x60, | ||
| DisableKeyboard = 0xad, | ||
| DisableMouse = 0xa7, | ||
| EnableKeyboard = 0xae, | ||
| #[allow(dead_code)] | ||
| EnableMouse = 0xa8, | ||
| TestFirstPort = 0xab, | ||
| } | ||
|
|
||
| const PS2_CNFG_ENABLE_KEYBOARD_INTERRUPT: u8 = 0x01; | ||
| const PS2_BUFFER_FULL: u8 = 0x01; | ||
|
|
||
| const MAX_INP_BUFFER_SIZE: usize = 256; | ||
| static KEYBOARD_SEMAPHORE: Semaphore = Semaphore::new(0); | ||
|
|
||
| struct Ps2; | ||
| impl Ps2 { | ||
| pub fn read_status() -> u8 { | ||
| // SAFETY: Correct port access without safety related side-effects. | ||
| unsafe { Port::<u8>::new(PS2_CMD_PORT).read() } | ||
| } | ||
|
|
||
| pub fn write_cmd(cmd: Ps2Command) { | ||
| // SAFETY: Correct port access without memory safety related side-effects. | ||
| unsafe { Port::<u8>::new(PS2_CMD_PORT).write(cmd as u8) } | ||
| } | ||
|
|
||
| pub fn read_data() -> u8 { | ||
| // SAFETY: Correct port access without safety related side-effects. | ||
| unsafe { Port::<u8>::new(PS2_DATA_PORT).read() } | ||
| } | ||
|
|
||
| pub fn write_data(data: u8) { | ||
| // SAFETY: Correct port access without memory safety related side-effects. | ||
| unsafe { Port::<u8>::new(PS2_DATA_PORT).write(data) } | ||
| } | ||
| } | ||
|
|
||
| static KEYBOARD_BUFFER: Lazy<InterruptTicketMutex<VecDeque<NonZeroU8>>> = | ||
| Lazy::new(|| InterruptTicketMutex::new(VecDeque::with_capacity(32))); | ||
|
|
||
| fn keyboard_handler() { | ||
| let scancode = Ps2::read_data(); | ||
| if let Some(valid_scancode) = NonZeroU8::new(scancode) { | ||
| { | ||
| let mut buffer = KEYBOARD_BUFFER.lock(); | ||
|
|
||
| // Pop the oldest scancode if the buffer is full. | ||
| if buffer.len() >= MAX_INP_BUFFER_SIZE { | ||
| buffer.pop_front(); | ||
| buffer.push_back(valid_scancode); | ||
| return; | ||
| } | ||
| buffer.push_back(valid_scancode); | ||
| } | ||
| KEYBOARD_SEMAPHORE.release(); | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn get_keyboard_handler() -> (u8, fn()) { | ||
| Ps2::write_cmd(Ps2Command::DisableKeyboard); | ||
| Ps2::write_cmd(Ps2Command::DisableMouse); | ||
|
|
||
| // Ensure an empty buffer to guard against stuck/garbage data | ||
| while (Ps2::read_status() & PS2_BUFFER_FULL) != 0 { | ||
| let _ = Ps2::read_data(); | ||
| } | ||
|
|
||
| Ps2::write_cmd(Ps2Command::ReadConfig); | ||
| let mut config = Ps2::read_data(); | ||
|
|
||
| config |= PS2_CNFG_ENABLE_KEYBOARD_INTERRUPT; | ||
|
|
||
| Ps2::write_cmd(Ps2Command::WriteConfig); | ||
| Ps2::write_data(config); | ||
|
|
||
| Ps2::write_cmd(Ps2Command::TestFirstPort); | ||
|
|
||
| if Ps2::read_data() != 0 { | ||
| error!("PS/2 keyboard test failed"); | ||
| } | ||
|
|
||
| Ps2::write_cmd(Ps2Command::EnableKeyboard); | ||
|
|
||
| // Force the initialization of the keyboard buffer to ensure it is ready before any interrupts occur. | ||
| Lazy::force(&KEYBOARD_BUFFER); | ||
|
|
||
| interrupts::add_irq_name(1, "PS/2 Keyboard"); | ||
|
|
||
| (1, keyboard_handler) | ||
| } | ||
|
|
||
| /// Pops scancodes from the keyboard buffer into the provided slice. If `nonblocking` is false, the | ||
| /// function will sleep the current thread until a scancode has been received. Returns the number of scancodes | ||
| /// popped into the slice. | ||
| pub fn pop_scancodes(slice: &mut [u8], nonblocking: bool) -> usize { | ||
| if slice.is_empty() { | ||
| return 0; | ||
| } | ||
| if nonblocking { | ||
| if !KEYBOARD_SEMAPHORE.try_acquire() { | ||
| return 0; | ||
| } | ||
| } else { | ||
| KEYBOARD_SEMAPHORE.acquire(None); | ||
| } | ||
| let mut amount: usize = 1; | ||
| while amount < slice.len() && KEYBOARD_SEMAPHORE.try_acquire() { | ||
| amount += 1; | ||
| } | ||
| let mut buffer = KEYBOARD_BUFFER.lock(); | ||
| for scancode in slice[..amount].iter_mut() { | ||
| *scancode = buffer.pop_front().unwrap().get(); | ||
| } | ||
| amount | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,3 +6,29 @@ use crate::arch::mm::paging::{BasePageSize, PageSize}; | |
| pub extern "C" fn sys_getpagesize() -> i32 { | ||
| BasePageSize::SIZE.try_into().unwrap() | ||
| } | ||
|
|
||
| // Writes the scancodes from the keyboard buffer into the provided buffer. | ||
| // If 'nonblock' is true, it will return immediately if there are no scancodes available, | ||
| // otherwise it will block until at least one scancode is available. | ||
| // Returns the number of bytes written to the buffer, | ||
| // or a negative error code on failure. | ||
| #[cfg(all(target_arch = "x86_64", feature = "pc-keyboard"))] | ||
| #[hermit_macro::system] | ||
| #[unsafe(no_mangle)] | ||
| pub unsafe extern "C" fn sys_read_keyboard(buffer: *mut u8, size: usize, nonblock: bool) -> isize { | ||
|
GloriousAlpaca marked this conversation as resolved.
GloriousAlpaca marked this conversation as resolved.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a doc comment |
||
| if buffer.is_null() { | ||
| return -(crate::errno::Errno::Fault as isize); | ||
| } | ||
| if size == 0 { | ||
| return 0; | ||
| } | ||
| // SAFETY: We have to trust the user input, because we are a unikernel and if the user wants to crash the program | ||
| // they are free to do so. | ||
| let buffer_slice: &mut [u8] = unsafe { core::slice::from_raw_parts_mut(buffer, size) }; | ||
|
GloriousAlpaca marked this conversation as resolved.
GloriousAlpaca marked this conversation as resolved.
|
||
| let result = crate::arch::kernel::pc_keyboard::pop_scancodes(buffer_slice, nonblock); | ||
| if result == 0 && nonblock { | ||
| -(crate::errno::Errno::Again as isize) | ||
| } else { | ||
| result as isize | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.