Project

General

Profile

Statistics
| Revision:

root / lab3 / kbc_func.c @ 48

History | View | Annotate | Download (2.73 KB)

1 47 up20180642
#include "kbc_func.h"
2
#include "kbc.h"
3
4
#include <lcom/lcf.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_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 got_error = 0;
19 48 up20180655
int done = 0;
20
int sz = 1;
21 47 up20180642
22
void (kbc_ih)(void) {
23
    uint8_t status = 0;
24
    got_error = 0;
25
26
    if (util_sys_inb(STATUS_REG, &status)) {
27
        got_error = 1;
28
        return;
29
    }
30
31
    if (status & (TIME_OUT_REC | PARITY_ERROR)) {
32
        got_error = 1;
33
        return;
34
    }
35
36
    uint8_t byte = 0;
37
38
    if (util_sys_inb(OUTPUT_BUF, &byte)) {
39
        got_error = 1;
40
        return;
41
    }
42
43 48 up20180655
    if (sz == 2 && done == 0) {
44 47 up20180642
        scancode[1] = byte;
45 48 up20180655
        done = 1;
46 47 up20180642
    } else {
47
        scancode[0] = byte;
48 48 up20180655
        if (byte == TWO_BYTE_CODE) {
49
            sz = 2;
50
            done = 0;
51
        } else {
52
            sz = 1;
53
            done = 1;
54
        }
55 47 up20180642
    }
56
57
}
58
59
int (kbd_poll)(uint8_t bytes[], uint8_t *size){
60
    if(bytes == NULL || size == NULL) return 1;
61
    uint8_t c;
62
    if(kbc_read_byte(&c)) return 1;
63
    if(c == TWO_BYTE_CODE){
64
        if(kbc_read_byte(&bytes[1])) return 1;
65
        bytes[0] = c;
66
        *size = 2;
67
    }else{
68
        bytes[1] = 0;
69
        bytes[0] = c;
70
        *size = 1;
71
    }
72
    return 0;
73
}
74
75
int (kbc_read_cmd)(uint8_t *cmd){
76
    if(kbc_issue_cmd(READ_KBC_CMD)) return 1;
77
    if(kbc_read_byte(cmd)) return 1;
78
    return 0;
79
}
80
81
int (kbc_change_cmd)(uint8_t cmd){
82
    if(kbc_issue_cmd(WRITE_KBC_CMD)) return 1;
83
    if(sys_outb(KBC_CMD_ARG, cmd)) return 1;
84
    return 0;
85
}
86
87
int (kbc_restore_kbd)(){
88
    uint8_t cmd = 0;
89
    if(kbc_read_cmd(&cmd)) return 1;
90
    cmd = (cmd | INT_KBD) & (~DIS_KBD);
91
    if(kbc_change_cmd(cmd)) return 1;
92
    return 0;
93
}
94
95
int (kbc_issue_cmd)(uint8_t cmd){
96
    uint8_t stat;
97
    for(int i = 0; i < KBC_NUM_TRIES; ++i){
98
        if(util_sys_inb(STATUS_REG, &stat)) return 1;
99
        if((stat&IN_BUF_FULL) == 0){
100
            if(sys_outb(KBC_CMD, cmd)) return 1;
101
            return 0;
102
        }
103
        tickdelay(micros_to_ticks(DELAY));
104
    }
105
    return 1;
106
}
107
108
int (kbc_read_byte)(uint8_t *byte){
109
    uint8_t stat;
110
    while(true){
111
        if(util_sys_inb(STATUS_REG, &stat)) return 1;
112
        if((stat&OUT_BUF_FUL) && (stat&AUX_MOUSE)==0){
113
            if(stat & (PARITY_ERROR | TIME_OUT_REC)) return 1;
114
            if(util_sys_inb(OUTPUT_BUF, byte)) return 1;
115
            else return 0;
116
        }
117
        tickdelay(micros_to_ticks(DELAY));
118
    }
119
}