Project

General

Profile

Statistics
| Revision:

root / lab3 / kbc.c @ 36

History | View | Annotate | Download (2.09 KB)

1
#include <lcom/lcf.h>
2

    
3
#include "kbc.h"
4
#include "kbc_func.h"
5

    
6
int (subscribe_kbc_interrupt)(uint8_t interrupt_bit, int *interrupt_id) {
7
    if (interrupt_id == NULL) return 1;
8
    *interrupt_id = interrupt_bit;
9
    return (sys_irqsetpolicy(KBC_IRQ, IRQ_REENABLE | IRQ_EXCLUSIVE, interrupt_id));
10
}
11

    
12
int (unsubscribe_kbc_interrupt)(int *interrupt_id) {
13
    if (interrupt_id == NULL) return 1;
14
    return sys_irqrmpolicy(interrupt_id);
15
}
16

    
17
uint8_t scancode[2];
18
int two_byte_scancode = 0;
19
int got_error = 0;
20

    
21
void (kbc_ih)(void) {
22
    uint8_t status = 0;
23
    got_error = 0;
24

    
25
    #ifdef LAB3
26
    sys_inb_counter(INCREMENT);
27
    #endif
28

    
29
    if (util_sys_inb(STATUS_REG, &status)) {
30
        got_error = 1;
31
        return;
32
    }
33

    
34
    if (status & (TIME_OUT_REC | PARITY_ERROR)) {
35
        got_error = 1;
36
        return;
37
    }
38

    
39
    #ifdef LAB3
40
    sys_inb_counter(INCREMENT);
41
    #endif
42

    
43
    uint8_t byte = 0;
44

    
45
    if (util_sys_inb(OUTPUT_BUF, &byte)) {
46
        got_error = 1;
47
        return;
48
    }
49

    
50
    if (two_byte_scancode) {
51
        scancode[1] = byte;
52
        two_byte_scancode = 0;
53
    } else {
54
        scancode[0] = byte;
55
        two_byte_scancode = (byte == TWO_BYTE_CODE);
56
    }
57

    
58
}
59

    
60
uint32_t (sys_inb_counter)(int increment) {
61
    static uint32_t counter = 0;
62
    if (increment) return ++counter;
63
    return counter;
64
}
65

    
66
int (kbd_poll)(uint8_t bytes[], uint8_t *size){
67
    if(bytes == NULL || size == NULL) return 1;
68
    uint8_t c;
69
    if(kbd_read_byte(&c)) return 1;
70
    if(c == TWO_BYTE_CODE){
71
        if(kbd_read_byte(&bytes[0])) return 1;
72
        bytes[1] = c;
73
        *size = 2;
74
    }else{
75
        bytes[1] = 0;
76
        bytes[0] = c;
77
        *size = 1;
78
    }
79
    return 0;
80
}
81

    
82
int (kbd_read_byte)(uint8_t *value){
83
    uint8_t stat;
84
    while(1){
85
        if(util_sys_inb(STATUS_REG, &stat)) return 1;
86
        if((stat&OUT_BUF_FUL) && ((stat&AUX_MOUSE)^AUX_MOUSE)){
87
            if(stat & (PARITY_ERROR | TIME_OUT_REC)) return 1;
88
            else return util_sys_inb(OUTPUT_BUF, value);
89
        }
90
        tickdelay(micros_to_ticks(DELAY));
91
    }
92
}