Project

General

Profile

Statistics
| Revision:

root / lab5 / kbc.c @ 173

History | View | Annotate | Download (2.19 KB)

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