root / lab3 / kbc.c @ 64
History | View | Annotate | Download (1.77 KB)
1 |
#include <lcom/lcf.h> |
---|---|
2 |
|
3 |
#include "kbc.h" |
4 |
|
5 |
#include "kbc_macros.h" |
6 |
#include "utils.h" |
7 |
|
8 |
int (unsubscribe_interrupt)(int *interrupt_id) { |
9 |
if (interrupt_id == NULL) return 1; |
10 |
return sys_irqrmpolicy(interrupt_id);
|
11 |
} |
12 |
|
13 |
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 |
int (kbc_restore_keyboard)(){
|
26 |
uint8_t cmd = 0;
|
27 |
if(kbc_read_cmd(&cmd)) return 1; |
28 |
cmd = (cmd | INT_KBD) & (~DIS_KBD) ; |
29 |
if(kbc_change_cmd(cmd)) return 1; |
30 |
return 0; |
31 |
} |
32 |
|
33 |
int (kbc_issue_cmd)(uint8_t cmd){
|
34 |
uint8_t stat; |
35 |
for(int i = 0; i < KBC_NUM_TRIES; ++i){ |
36 |
if(util_sys_inb(STATUS_REG, &stat)) return 1; |
37 |
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 |
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 |
int (kbc_read_byte)(uint8_t *byte){
|
60 |
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 |
if(stat & (PARITY_ERROR | TIME_OUT_REC)) return 1; |
65 |
if(util_sys_inb(OUTPUT_BUF, byte)) return 1; |
66 |
else return 0; |
67 |
} |
68 |
tickdelay(micros_to_ticks(DELAY)); |
69 |
} |
70 |
} |