root / proj / kbc.c @ 147
History | View | Annotate | Download (2.01 KB)
1 | 145 | 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 (kbc_read_cmd)(uint8_t *cmd){
|
||
10 | int ret = 0; |
||
11 | if((ret = kbc_issue_cmd(READ_KBC_CMD))) return ret; |
||
12 | if((ret = kbc_read_byte(cmd))) return ret; |
||
13 | return SUCCESS;
|
||
14 | } |
||
15 | |||
16 | int (kbc_change_cmd)(uint8_t cmd){
|
||
17 | int ret = 0; |
||
18 | if((ret = kbc_issue_cmd(WRITE_KBC_CMD))) return ret; |
||
19 | if((ret = kbc_issue_arg(cmd))) return ret; |
||
20 | return SUCCESS;
|
||
21 | } |
||
22 | |||
23 | int (kbc_restore_kbd)(){
|
||
24 | int ret = 0; |
||
25 | uint8_t cmd = 0;
|
||
26 | if((ret = kbc_read_cmd(&cmd))) return ret; |
||
27 | cmd = (cmd | INT_KBD) & (~DIS_KBD); |
||
28 | if((ret = kbc_change_cmd(cmd))) return ret; |
||
29 | return SUCCESS;
|
||
30 | } |
||
31 | |||
32 | int (kbc_issue_cmd)(uint8_t cmd){
|
||
33 | int ret = 0; |
||
34 | uint8_t stat; |
||
35 | for(int i = 0; i < KBC_NUM_TRIES; ++i){ |
||
36 | if((ret = util_sys_inb(STATUS_REG, &stat))) return ret; |
||
37 | if((stat&IN_BUF_FULL) == 0){ |
||
38 | if(sys_outb(KBC_CMD, cmd)) return WRITE_ERROR; |
||
39 | return SUCCESS;
|
||
40 | } |
||
41 | tickdelay(micros_to_ticks(DELAY)); |
||
42 | } |
||
43 | return TIMEOUT_ERROR;
|
||
44 | } |
||
45 | |||
46 | int (kbc_issue_arg)(uint8_t arg){
|
||
47 | int ret = 0; |
||
48 | uint8_t stat; |
||
49 | for(int i = 0; i < KBC_NUM_TRIES; ++i){ |
||
50 | if((ret = util_sys_inb(STATUS_REG, &stat))) return ret; |
||
51 | if((stat&IN_BUF_FULL) == 0){ |
||
52 | if(sys_outb(KBC_CMD_ARG, arg)) return WRITE_ERROR; |
||
53 | return SUCCESS;
|
||
54 | } |
||
55 | tickdelay(micros_to_ticks(DELAY)); |
||
56 | } |
||
57 | return TIMEOUT_ERROR;
|
||
58 | } |
||
59 | |||
60 | int (kbc_read_byte)(uint8_t *byte){
|
||
61 | int ret = 0; |
||
62 | uint8_t stat; |
||
63 | for(int i = 0; i < KBC_NUM_TRIES; ++i){ |
||
64 | if((ret = util_sys_inb(STATUS_REG, &stat))) return ret; |
||
65 | if((stat&OUT_BUF_FUL) && (stat&AUX_MOUSE)==0){ |
||
66 | if(stat & (PARITY_ERROR | TIME_OUT_REC)) return OTHER_ERROR; |
||
67 | if((ret = util_sys_inb(OUTPUT_BUF, byte))) return ret; |
||
68 | else return SUCCESS; |
||
69 | } |
||
70 | tickdelay(micros_to_ticks(DELAY)); |
||
71 | } |
||
72 | printf("Timing out\n");
|
||
73 | return TIMEOUT_ERROR;
|
||
74 | } |