Project

General

Profile

Statistics
| Revision:

root / lab3 / kbc.c @ 228

History | View | Annotate | Download (2.13 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 67 up20180642
#include "errors.h"
8 30 up20180655
9 60 up20180642
int (unsubscribe_interrupt)(int *interrupt_id) {
10 67 up20180642
    if (interrupt_id == NULL) return NULL_PTR;
11
    if(sys_irqrmpolicy(interrupt_id)) return UNSBCR_ERROR;
12
    return SUCCESS;
13 30 up20180655
}
14
15 39 up20180642
int (kbc_read_cmd)(uint8_t *cmd){
16 67 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 39 up20180642
}
21
22
int (kbc_change_cmd)(uint8_t cmd){
23 67 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 39 up20180642
}
28
29 60 up20180642
int (kbc_restore_keyboard)(){
30 67 up20180642
    int ret = 0;
31 43 up20180642
    uint8_t cmd = 0;
32 67 up20180642
    if((ret = kbc_read_cmd(&cmd))) return ret;
33 60 up20180642
    cmd = (cmd | INT_KBD) & (~DIS_KBD) ;
34 67 up20180642
    if((ret = kbc_change_cmd(cmd))) return ret;
35
    return SUCCESS;
36 43 up20180642
}
37
38 39 up20180642
int (kbc_issue_cmd)(uint8_t cmd){
39 67 up20180642
    int ret = 0;
40 36 up20180642
    uint8_t stat;
41 40 up20180642
    for(int i = 0; i < KBC_NUM_TRIES; ++i){
42 67 up20180642
        if((ret = util_sys_inb(STATUS_REG, &stat))) return ret;
43 38 up20180642
        if((stat&IN_BUF_FULL) == 0){
44 67 up20180642
            if(sys_outb(KBC_CMD, cmd)) return WRITE_ERROR;
45
            return SUCCESS;
46 38 up20180642
        }
47
        tickdelay(micros_to_ticks(DELAY));
48
    }
49 67 up20180642
    return TIMEOUT_ERROR;
50 38 up20180642
}
51
52 61 up20180642
int (kbc_issue_arg)(uint8_t arg){
53 67 up20180642
    int ret = 0;
54 61 up20180642
    uint8_t stat;
55
    for(int i = 0; i < KBC_NUM_TRIES; ++i){
56 67 up20180642
        if((ret = util_sys_inb(STATUS_REG, &stat))) return ret;
57 61 up20180642
        if((stat&IN_BUF_FULL) == 0){
58 67 up20180642
            if(sys_outb(KBC_CMD_ARG, arg)) return WRITE_ERROR;
59
            return SUCCESS;
60 61 up20180642
        }
61
        tickdelay(micros_to_ticks(DELAY));
62
    }
63 67 up20180642
    return TIMEOUT_ERROR;
64 61 up20180642
}
65
66 39 up20180642
int (kbc_read_byte)(uint8_t *byte){
67 67 up20180642
    int ret = 0;
68 38 up20180642
    uint8_t stat;
69
    while(true){
70 67 up20180642
        if((ret = util_sys_inb(STATUS_REG, &stat))) return ret;
71 38 up20180642
        if((stat&OUT_BUF_FUL) && (stat&AUX_MOUSE)==0){
72 36 up20180642
            if(stat & (PARITY_ERROR | TIME_OUT_REC)) return 1;
73 67 up20180642
            if((ret = util_sys_inb(OUTPUT_BUF, byte))) return ret;
74
            else return SUCCESS;
75 36 up20180642
        }
76
        tickdelay(micros_to_ticks(DELAY));
77
    }
78 67 up20180642
    return TIMEOUT_ERROR;
79 36 up20180642
}