main.rs 1.98 KB
Newer Older
1 2 3 4 5 6
#![feature(asm)]

extern crate syscall;

use std::fs::File;
use std::io::{Read, Write};
7
use std::mem;
8 9 10 11
use std::thread;

use syscall::iopl;

Jeremy Soller's avatar
Jeremy Soller committed
12
mod keymap;
13

Jeremy Soller's avatar
Jeremy Soller committed
14 15
fn keyboard() {
    let mut file = File::open("irq:1").expect("pskbd: failed to open irq:1");
16

Jeremy Soller's avatar
Jeremy Soller committed
17 18 19 20 21 22 23
    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");
            }
24

Jeremy Soller's avatar
Jeremy Soller committed
25 26 27 28 29 30
            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);
31

Jeremy Soller's avatar
Jeremy Soller committed
32 33 34 35 36 37
            file.write(&irqs).expect("pskbd: failed to write irq:1");
        } else {
            thread::yield_now();
        }
    }
}
38

Jeremy Soller's avatar
Jeremy Soller committed
39 40
fn mouse() {
    let mut file = File::open("irq:12").expect("psmsd: failed to open irq:12");
41

Jeremy Soller's avatar
Jeremy Soller committed
42 43 44 45 46 47
    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");
48
            }
Jeremy Soller's avatar
Jeremy Soller committed
49 50 51 52 53 54

            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();
55
        }
Jeremy Soller's avatar
Jeremy Soller committed
56 57
    }
}
58

Jeremy Soller's avatar
Jeremy Soller committed
59
fn main() {
60
    thread::spawn(|| {
61
        unsafe {
Jeremy Soller's avatar
Jeremy Soller committed
62
            iopl(3).expect("pskbd: failed to get I/O permission");
63 64 65
            asm!("cli" :::: "intel", "volatile");
        }

Jeremy Soller's avatar
Jeremy Soller committed
66 67
        keyboard();
    });
68

Jeremy Soller's avatar
Jeremy Soller committed
69 70 71 72
    thread::spawn(|| {
        unsafe {
            iopl(3).expect("psmsd: failed to get I/O permission");
            asm!("cli" :::: "intel", "volatile");
73
        }
74

Jeremy Soller's avatar
Jeremy Soller committed
75 76
        mouse();
    });
77
}