Project

General

Profile

Statistics
| Revision:

root / lab4 / kbc.c @ 142

History | View | Annotate | Download (2.19 KB)

1 59 up20180642
#include <lcom/lcf.h>
2
3
#include "kbc.h"
4
5
#include "kbc_macros.h"
6 68 up20180642
#include "utils.h"
7
#include "errors.h"
8 59 up20180642
9
int (unsubscribe_interrupt)(int *interrupt_id) {
10 68 up20180642
    if (interrupt_id == NULL) return NULL_PTR;
11
    if(sys_irqrmpolicy(interrupt_id)) return UNSBCR_ERROR;
12
    return SUCCESS;
13 59 up20180642
}
14
15
int (kbc_read_cmd)(uint8_t *cmd){
16 68 up20180642
    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 59 up20180642
}
21
22
int (kbc_change_cmd)(uint8_t cmd){
23 68 up20180642
    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 59 up20180642
}
28
29
int (kbc_restore_kbd)(){
30 68 up20180642
    int ret = 0;
31 59 up20180642
    uint8_t cmd = 0;
32 68 up20180642
    if((ret = kbc_read_cmd(&cmd))) return ret;
33 79 up20180655
    cmd = (cmd | INT_KBD) & (~DIS_KBD);
34 68 up20180642
    if((ret = kbc_change_cmd(cmd))) return ret;
35
    return SUCCESS;
36 59 up20180642
}
37
38
int (kbc_issue_cmd)(uint8_t cmd){
39 68 up20180642
    int ret = 0;
40 59 up20180642
    uint8_t stat;
41
    for(int i = 0; i < KBC_NUM_TRIES; ++i){
42 68 up20180642
        if((ret = util_sys_inb(STATUS_REG, &stat))) return ret;
43 59 up20180642
        if((stat&IN_BUF_FULL) == 0){
44 68 up20180642
            if(sys_outb(KBC_CMD, cmd)) return WRITE_ERROR;
45
            return SUCCESS;
46 59 up20180642
        }
47
        tickdelay(micros_to_ticks(DELAY));
48
    }
49 68 up20180642
    return TIMEOUT_ERROR;
50 59 up20180642
}
51
52 68 up20180642
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 59 up20180642
int (kbc_read_byte)(uint8_t *byte){
67 68 up20180642
    int ret = 0;
68 59 up20180642
    uint8_t stat;
69 68 up20180642
    for(int i = 0; i < KBC_NUM_TRIES; ++i){
70 78 up20180655
        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 68 up20180642
            if((ret = util_sys_inb(OUTPUT_BUF, byte))) return ret;
74
            else return SUCCESS;
75 59 up20180642
        }
76
        tickdelay(micros_to_ticks(DELAY));
77
    }
78 72 up20180642
    printf("Timing out\n");
79 68 up20180642
    return TIMEOUT_ERROR;
80 59 up20180642
}