Project

General

Profile

Statistics
| Revision:

root / lab3 / keyboard.c @ 60

History | View | Annotate | Download (1.33 KB)

1
#include <lcom/lcf.h>
2

    
3
#include "keyboard.h"
4

    
5
#include "kbc.h"
6
#include "kbc_macros.h"
7
#include "utils.h"
8

    
9
int (subscribe_keyboard_interrupt)(uint8_t interrupt_bit, int *interrupt_id) {
10
    if (interrupt_id == NULL) return 1;
11
    *interrupt_id = interrupt_bit;
12
    return (sys_irqsetpolicy(KBC_IRQ, IRQ_REENABLE | IRQ_EXCLUSIVE, interrupt_id));
13
}
14

    
15
int got_error_keyboard = 0;
16
int two_byte_scancode = 0;
17

    
18
void (kbc_ih)(void) {
19
    uint8_t status = 0;
20
    got_error_keyboard = 0;
21

    
22
    if (util_sys_inb(STATUS_REG, &status)) {
23
        got_error_keyboard = 1;
24
        return;
25
    }
26

    
27
    if (status & (TIME_OUT_REC | PARITY_ERROR)) {
28
        got_error_keyboard = 1;
29
        return;
30
    }
31

    
32
    uint8_t byte = 0;
33

    
34
    if (util_sys_inb(OUTPUT_BUF, &byte)) {
35
        got_error_keyboard = 1;
36
        return;
37
    }
38

    
39
    if (two_byte_scancode) {
40
        scancode[1] = byte;
41
        two_byte_scancode = 0;
42
    } else {
43
        scancode[0] = byte;
44
        two_byte_scancode = (byte == TWO_BYTE_CODE);
45
    }
46

    
47
}
48

    
49
int (keyboard_poll)(uint8_t bytes[], uint8_t *size){
50
    if(bytes == NULL || size == NULL) return 1;
51
    uint8_t c;
52
    if(kbc_read_byte(&c)) return 1;
53
    if(c == TWO_BYTE_CODE){
54
        if(kbc_read_byte(&bytes[1])) return 1;
55
        bytes[0] = c;
56
        *size = 2;
57
    }else{
58
        bytes[1] = 0;
59
        bytes[0] = c;
60
        *size = 1;
61
    }
62
    return 0;
63
}