Project

General

Profile

Statistics
| Revision:

root / lab3 / kbc.c @ 40

History | View | Annotate | Download (2.73 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(kbc_read_byte(&c)) return 1;
70
    if(c == TWO_BYTE_CODE){
71
        if(kbc_read_byte(&bytes[1])) return 1;
72
        bytes[0] = 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 (kbc_read_cmd)(uint8_t *cmd){
83
    if(kbc_issue_cmd(READ_KBC_CMD)) return 1;
84
    if(kbc_read_byte(cmd)) return 1;
85
    return 0;
86
}
87

    
88
int (kbc_change_cmd)(uint8_t cmd){
89
    if(kbc_issue_cmd(WRITE_KBC_CMD)) return 1;
90
    if(sys_outb(KBC_CMD_ARG, cmd)) return 1;
91
    return 0;
92
}
93

    
94
int (kbc_issue_cmd)(uint8_t cmd){
95
    uint8_t stat;
96
    for(int i = 0; i < KBC_NUM_TRIES; ++i){
97
        if(util_sys_inb(STATUS_REG, &stat)) return 1;
98
        if((stat&IN_BUF_FULL) == 0){
99
            if(sys_outb(KBC_CMD, cmd)) return 1;
100
            return 0;
101
        }
102
        tickdelay(micros_to_ticks(DELAY));
103
    }
104
    return 1;
105
}
106

    
107
int (kbc_read_byte)(uint8_t *byte){
108
    uint8_t stat;
109
    while(true){
110
        if(util_sys_inb(STATUS_REG, &stat)) return 1;
111
        if((stat&OUT_BUF_FUL) && (stat&AUX_MOUSE)==0){
112
            if(stat & (PARITY_ERROR | TIME_OUT_REC)) return 1;
113
            if(util_sys_inb(OUTPUT_BUF, byte)) return 1;
114
            else return 0;
115
        }
116
        tickdelay(micros_to_ticks(DELAY));
117
    }
118
}