Project

General

Profile

Statistics
| Revision:

root / lab3 / kbc.c @ 64

History | View | Annotate | Download (1.77 KB)

1 30 up20180655
#include <lcom/lcf.h>
2
3
#include "kbc.h"
4
5 60 up20180642
#include "kbc_macros.h"
6
#include "utils.h"
7 30 up20180655
8 60 up20180642
int (unsubscribe_interrupt)(int *interrupt_id) {
9 30 up20180655
    if (interrupt_id == NULL) return 1;
10
    return sys_irqrmpolicy(interrupt_id);
11
}
12
13 39 up20180642
int (kbc_read_cmd)(uint8_t *cmd){
14
    if(kbc_issue_cmd(READ_KBC_CMD)) return 1;
15
    if(kbc_read_byte(cmd)) return 1;
16
    return 0;
17
}
18
19
int (kbc_change_cmd)(uint8_t cmd){
20
    if(kbc_issue_cmd(WRITE_KBC_CMD)) return 1;
21
    if(sys_outb(KBC_CMD_ARG, cmd)) return 1;
22
    return 0;
23
}
24
25 60 up20180642
int (kbc_restore_keyboard)(){
26 43 up20180642
    uint8_t cmd = 0;
27
    if(kbc_read_cmd(&cmd)) return 1;
28 60 up20180642
    cmd = (cmd | INT_KBD) & (~DIS_KBD) ;
29 43 up20180642
    if(kbc_change_cmd(cmd)) return 1;
30
    return 0;
31
}
32
33 39 up20180642
int (kbc_issue_cmd)(uint8_t cmd){
34 36 up20180642
    uint8_t stat;
35 40 up20180642
    for(int i = 0; i < KBC_NUM_TRIES; ++i){
36 36 up20180642
        if(util_sys_inb(STATUS_REG, &stat)) return 1;
37 38 up20180642
        if((stat&IN_BUF_FULL) == 0){
38
            if(sys_outb(KBC_CMD, cmd)) return 1;
39
            return 0;
40
        }
41
        tickdelay(micros_to_ticks(DELAY));
42
    }
43
    return 1;
44
}
45
46 61 up20180642
int (kbc_issue_arg)(uint8_t arg){
47
    uint8_t stat;
48
    for(int i = 0; i < KBC_NUM_TRIES; ++i){
49
        if(util_sys_inb(STATUS_REG, &stat)) return 1;
50
        if((stat&IN_BUF_FULL) == 0){
51
            if(sys_outb(KBC_CMD_ARG, arg)) return 1;
52
            return 0;
53
        }
54
        tickdelay(micros_to_ticks(DELAY));
55
    }
56
    return 1;
57
}
58
59 39 up20180642
int (kbc_read_byte)(uint8_t *byte){
60 38 up20180642
    uint8_t stat;
61
    while(true){
62
        if(util_sys_inb(STATUS_REG, &stat)) return 1;
63
        if((stat&OUT_BUF_FUL) && (stat&AUX_MOUSE)==0){
64 36 up20180642
            if(stat & (PARITY_ERROR | TIME_OUT_REC)) return 1;
65 38 up20180642
            if(util_sys_inb(OUTPUT_BUF, byte)) return 1;
66
            else return 0;
67 36 up20180642
        }
68
        tickdelay(micros_to_ticks(DELAY));
69
    }
70
}