|
| 1 | +use alloc::collections::VecDeque; |
| 2 | +use core::num::NonZeroU8; |
| 3 | + |
| 4 | +use hermit_sync::{InterruptTicketMutex, Lazy}; |
| 5 | +use x86_64::instructions::port::Port; |
| 6 | + |
| 7 | +use crate::arch::kernel::interrupts; |
| 8 | +use crate::synch::semaphore::Semaphore; |
| 9 | + |
| 10 | +const PS2_DATA_PORT: u16 = 0x60; |
| 11 | +const PS2_CMD_PORT: u16 = 0x64; |
| 12 | + |
| 13 | +#[repr(u8)] |
| 14 | +enum Ps2Command { |
| 15 | + ReadConfig = 0x20, |
| 16 | + WriteConfig = 0x60, |
| 17 | + DisableKeyboard = 0xad, |
| 18 | + DisableMouse = 0xa7, |
| 19 | + EnableKeyboard = 0xae, |
| 20 | + #[allow(dead_code)] |
| 21 | + EnableMouse = 0xa8, |
| 22 | + TestFirstPort = 0xab, |
| 23 | +} |
| 24 | + |
| 25 | +const PS2_CNFG_ENABLE_KEYBOARD_INTERRUPT: u8 = 0x01; |
| 26 | +const PS2_BUFFER_FULL: u8 = 0x01; |
| 27 | + |
| 28 | +const MAX_INP_BUFFER_SIZE: usize = 256; |
| 29 | +static KEYBOARD_SEMAPHORE: Semaphore = Semaphore::new(0); |
| 30 | + |
| 31 | +struct Ps2; |
| 32 | +impl Ps2 { |
| 33 | + pub fn read_status() -> u8 { |
| 34 | + // SAFETY: Correct port access without safety related side-effects. |
| 35 | + unsafe { Port::<u8>::new(PS2_CMD_PORT).read() } |
| 36 | + } |
| 37 | + |
| 38 | + pub fn write_cmd(cmd: Ps2Command) { |
| 39 | + // SAFETY: Correct port access without memory safety related side-effects. |
| 40 | + unsafe { Port::<u8>::new(PS2_CMD_PORT).write(cmd as u8) } |
| 41 | + } |
| 42 | + |
| 43 | + pub fn read_data() -> u8 { |
| 44 | + // SAFETY: Correct port access without safety related side-effects. |
| 45 | + unsafe { Port::<u8>::new(PS2_DATA_PORT).read() } |
| 46 | + } |
| 47 | + |
| 48 | + pub fn write_data(data: u8) { |
| 49 | + // SAFETY: Correct port access without memory safety related side-effects. |
| 50 | + unsafe { Port::<u8>::new(PS2_DATA_PORT).write(data) } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +static KEYBOARD_BUFFER: Lazy<InterruptTicketMutex<VecDeque<NonZeroU8>>> = |
| 55 | + Lazy::new(|| InterruptTicketMutex::new(VecDeque::with_capacity(32))); |
| 56 | + |
| 57 | +fn keyboard_handler() { |
| 58 | + let scancode = Ps2::read_data(); |
| 59 | + if let Some(valid_scancode) = NonZeroU8::new(scancode) { |
| 60 | + { |
| 61 | + let mut buffer = KEYBOARD_BUFFER.lock(); |
| 62 | + |
| 63 | + // Pop the oldest scancode if the buffer is full. |
| 64 | + if buffer.len() >= MAX_INP_BUFFER_SIZE { |
| 65 | + buffer.pop_front(); |
| 66 | + buffer.push_back(valid_scancode); |
| 67 | + return; |
| 68 | + } |
| 69 | + buffer.push_back(valid_scancode); |
| 70 | + } |
| 71 | + KEYBOARD_SEMAPHORE.release(); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +pub(crate) fn get_keyboard_handler() -> (u8, fn()) { |
| 76 | + Ps2::write_cmd(Ps2Command::DisableKeyboard); |
| 77 | + Ps2::write_cmd(Ps2Command::DisableMouse); |
| 78 | + |
| 79 | + // Ensure an empty buffer to guard against stuck/garbage data |
| 80 | + while (Ps2::read_status() & PS2_BUFFER_FULL) != 0 { |
| 81 | + let _ = Ps2::read_data(); |
| 82 | + } |
| 83 | + |
| 84 | + Ps2::write_cmd(Ps2Command::ReadConfig); |
| 85 | + let mut config = Ps2::read_data(); |
| 86 | + |
| 87 | + config |= PS2_CNFG_ENABLE_KEYBOARD_INTERRUPT; |
| 88 | + |
| 89 | + Ps2::write_cmd(Ps2Command::WriteConfig); |
| 90 | + Ps2::write_data(config); |
| 91 | + |
| 92 | + Ps2::write_cmd(Ps2Command::TestFirstPort); |
| 93 | + |
| 94 | + if Ps2::read_data() != 0 { |
| 95 | + error!("PS/2 keyboard test failed"); |
| 96 | + } |
| 97 | + |
| 98 | + Ps2::write_cmd(Ps2Command::EnableKeyboard); |
| 99 | + |
| 100 | + // Force the initialization of the keyboard buffer to ensure it is ready before any interrupts occur. |
| 101 | + Lazy::force(&KEYBOARD_BUFFER); |
| 102 | + |
| 103 | + interrupts::add_irq_name(1, "PS/2 Keyboard"); |
| 104 | + |
| 105 | + (1, keyboard_handler) |
| 106 | +} |
| 107 | + |
| 108 | +/// Pops scancodes from the keyboard buffer into the provided slice. If `nonblocking` is false, the |
| 109 | +/// function will sleep the current thread until a scancode has been received. Returns the number of scancodes |
| 110 | +/// popped into the slice. |
| 111 | +pub fn pop_scancodes(slice: &mut [u8], nonblocking: bool) -> usize { |
| 112 | + if slice.is_empty() { |
| 113 | + return 0; |
| 114 | + } |
| 115 | + if nonblocking { |
| 116 | + if !KEYBOARD_SEMAPHORE.try_acquire() { |
| 117 | + return 0; |
| 118 | + } |
| 119 | + } else { |
| 120 | + KEYBOARD_SEMAPHORE.acquire(None); |
| 121 | + } |
| 122 | + let mut amount: usize = 1; |
| 123 | + while amount < slice.len() && KEYBOARD_SEMAPHORE.try_acquire() { |
| 124 | + amount += 1; |
| 125 | + } |
| 126 | + let mut buffer = KEYBOARD_BUFFER.lock(); |
| 127 | + for scancode in slice[..amount].iter_mut() { |
| 128 | + *scancode = buffer.pop_front().unwrap().get(); |
| 129 | + } |
| 130 | + amount |
| 131 | +} |
0 commit comments