PS/2 driver convert to char

This commit is contained in:
Jeremy Soller 2016-09-19 10:24:19 -06:00
parent 727647dbf1
commit c957c2a105
5 changed files with 120 additions and 82 deletions

View file

@ -1,15 +1,9 @@
use core::cmp; use core::cmp;
use spin::Mutex;
use io::{Io, Pio, ReadOnly, WriteOnly}; use io::{Io, Pio, ReadOnly, WriteOnly};
pub static PS2_KEYBOARD: Mutex<Option<Ps2Keyboard>> = Mutex::new(None);
pub static PS2_MOUSE: Mutex<Option<Ps2Mouse>> = Mutex::new(None);
pub unsafe fn init() { pub unsafe fn init() {
let (keyboard, mouse) = Ps2::new().init(); Ps2::new().init();
*PS2_KEYBOARD.lock() = keyboard;
*PS2_MOUSE.lock() = mouse;
} }
bitflags! { bitflags! {
@ -98,34 +92,6 @@ bitflags! {
} }
} }
pub struct Ps2Keyboard {
data: ReadOnly<Pio<u8>>,
key: [u8; 3],
key_i: usize,
}
impl Ps2Keyboard {
fn new() -> Self {
Ps2Keyboard {
data: ReadOnly::new(Pio::new(0x60)),
key: [0; 3],
key_i: 0
}
}
pub fn on_irq(&mut self) {
let scancode = self.data.read();
self.key[self.key_i] = scancode;
self.key_i += 1;
if self.key_i >= self.key.len() || scancode < 0xE0 {
println!("KEY: {:X} {:X} {:X}", self.key[0], self.key[1], self.key[2]);
self.key = [0; 3];
self.key_i = 0;
}
}
}
pub struct Ps2Mouse { pub struct Ps2Mouse {
data: ReadOnly<Pio<u8>>, data: ReadOnly<Pio<u8>>,
mouse: [u8; 4], mouse: [u8; 4],
@ -278,7 +244,7 @@ impl Ps2 {
self.read() self.read()
} }
fn init(&mut self) -> (Option<Ps2Keyboard>, Option<Ps2Mouse>) { fn init(&mut self) {
// Disable devices // Disable devices
self.command(Command::DisableFirst); self.command(Command::DisableFirst);
self.command(Command::DisableSecond); self.command(Command::DisableSecond);
@ -352,13 +318,12 @@ impl Ps2 {
let mut config = self.config(); let mut config = self.config();
config.remove(FIRST_DISABLED); config.remove(FIRST_DISABLED);
config.remove(SECOND_DISABLED); config.remove(SECOND_DISABLED);
config.insert(FIRST_TRANSLATE);
config.insert(FIRST_INTERRUPT); config.insert(FIRST_INTERRUPT);
config.insert(SECOND_INTERRUPT); config.insert(SECOND_INTERRUPT);
self.set_config(config); self.set_config(config);
} }
self.flush_read(); self.flush_read();
(Some(Ps2Keyboard::new()), Some(Ps2Mouse::new(mouse_extra)))
} }
} }

View file

@ -1,7 +1,6 @@
use spin::Mutex; use spin::Mutex;
use x86::io; use x86::io;
use device::ps2::{PS2_KEYBOARD, PS2_MOUSE};
use device::serial::{COM1, COM2}; use device::serial::{COM1, COM2};
pub static ACKS: Mutex<[usize; 16]> = Mutex::new([0; 16]); pub static ACKS: Mutex<[usize; 16]> = Mutex::new([0; 16]);

View file

@ -0,0 +1,68 @@
static ENGLISH: [[char; 3]; 58] = [
['\0', '\0', '\0'],
['\x1B', '\x1B', '\x1B'],
['1', '!', '1'],
['2', '@', '2'],
['3', '#', '3'],
['4', '$', '4'],
['5', '%', '5'],
['6', '^', '6'],
['7', '&', '7'],
['8', '*', '8'],
['9', '(', '9'],
['0', ')', '0'],
['-', '_', '-'],
['=', '+', '='],
['\0', '\0', '\0'],
['\t', '\t', '\t'],
['q', 'Q', 'q'],
['w', 'W', 'w'],
['e', 'E', 'e'],
['r', 'R', 'r'],
['t', 'T', 't'],
['y', 'Y', 'y'],
['u', 'U', 'u'],
['i', 'I', 'i'],
['o', 'O', 'o'],
['p', 'P', 'p'],
['[', '{', '['],
[']', '}', ']'],
['\n', '\n', '\n'],
['\0', '\0', '\0'],
['a', 'A', 'a'],
['s', 'S', 's'],
['d', 'D', 'd'],
['f', 'F', 'f'],
['g', 'G', 'g'],
['h', 'H', 'h'],
['j', 'J', 'j'],
['k', 'K', 'k'],
['l', 'L', 'l'],
[';', ':', ';'],
['\'', '"', '\''],
['`', '~', '`'],
['\0', '\0', '\0'],
['\\', '|', '\\'],
['z', 'Z', 'z'],
['x', 'X', 'x'],
['c', 'C', 'c'],
['v', 'V', 'v'],
['b', 'B', 'b'],
['n', 'N', 'n'],
['m', 'M', 'm'],
[',', '<', ','],
['.', '>', '.'],
['/', '?', '/'],
['\0', '\0', '\0'],
['\0', '\0', '\0'],
['\0', '\0', '\0'],
[' ', ' ', ' ']
];
pub fn get_char(scancode: u8) -> char {
if let Some(c) = ENGLISH.get(scancode as usize) {
c[0]
} else {
'\0'
}
}

View file

@ -9,34 +9,61 @@ use std::thread;
use syscall::iopl; use syscall::iopl;
fn main() { mod keymap;
println!("PS/2 driver launching");
fn keyboard() {
let mut file = File::open("irq:1").expect("pskbd: failed to open irq:1");
loop {
let mut irqs = [0; 8];
if file.read(&mut irqs).expect("pskbd: failed to read irq:1") >= mem::size_of::<usize>() {
let data: u8;
unsafe {
asm!("in al, dx" : "={al}"(data) : "{dx}"(0x60) : : "intel", "volatile");
}
let (scancode, pressed) = if data >= 0x80 {
(data - 0x80, false)
} else {
(data, true)
};
println!("pskbd: IRQ {}: {:X}: {:X}: {}: {}", unsafe { *(irqs.as_ptr() as *const usize) }, data, scancode, keymap::get_char(scancode), pressed);
file.write(&irqs).expect("pskbd: failed to write irq:1");
} else {
thread::yield_now();
}
}
}
fn mouse() {
let mut file = File::open("irq:12").expect("psmsd: failed to open irq:12");
loop {
let mut irqs = [0; 8];
if file.read(&mut irqs).expect("psmsd: failed to read irq:12") >= mem::size_of::<usize>() {
let data: u8;
unsafe {
asm!("in al, dx" : "={al}"(data) : "{dx}"(0x60) : : "intel", "volatile");
}
println!("psmsd: IRQ {}: {:X}", unsafe { *(irqs.as_ptr() as *const usize) }, data);
file.write(&irqs).expect("psmsd: failed to write irq:12");
} else {
thread::yield_now();
}
}
}
fn main() {
thread::spawn(|| { thread::spawn(|| {
unsafe { unsafe {
iopl(3).expect("pskbd: failed to get I/O permission"); iopl(3).expect("pskbd: failed to get I/O permission");
asm!("cli" :::: "intel", "volatile"); asm!("cli" :::: "intel", "volatile");
} }
let mut file = File::open("irq:1").expect("pskbd: failed to open irq:1"); keyboard();
println!("pskbd: Reading keyboard IRQs");
loop {
let mut irqs = [0; 8];
if file.read(&mut irqs).expect("pskbd: failed to read irq:1") >= mem::size_of::<usize>() {
let data: u8;
unsafe {
asm!("in al, dx" : "={al}"(data) : "{dx}"(0x60) : : "intel", "volatile");
}
println!("pskbd: IRQ {}: {:X}", unsafe { *(irqs.as_ptr() as *const usize) }, data);
file.write(&irqs).expect("pskbd: failed to write irq:1");
} else {
thread::yield_now();
}
}
}); });
thread::spawn(|| { thread::spawn(|| {
@ -45,26 +72,6 @@ fn main() {
asm!("cli" :::: "intel", "volatile"); asm!("cli" :::: "intel", "volatile");
} }
let mut file = File::open("irq:12").expect("psmsd: failed to open irq:12"); mouse();
println!("psmsd: Reading mouse IRQs");
loop {
let mut irqs = [0; 8];
if file.read(&mut irqs).expect("psmsd: failed to read irq:12") >= mem::size_of::<usize>() {
let data: u8;
unsafe {
asm!("in al, dx" : "={al}"(data) : "{dx}"(0x60) : : "intel", "volatile");
}
println!("psmsd: IRQ {}: {:X}", unsafe { *(irqs.as_ptr() as *const usize) }, data);
file.write(&irqs).expect("psmsd: failed to write irq:12");
} else {
thread::yield_now();
}
}
}); });
println!("PS/2 driver running in background");
} }

View file

@ -1,7 +1,6 @@
use core::{mem, str}; use core::{mem, str};
use arch::interrupt::irq::{ACKS, COUNTS, acknowledge}; use arch::interrupt::irq::{ACKS, COUNTS, acknowledge};
use context;
use syscall::{Error, Result}; use syscall::{Error, Result};
use super::Scheme; use super::Scheme;