root / lab4 / kbc.c @ 66
History | View | Annotate | Download (1.4 KB)
1 | 59 | up20180642 | #include <lcom/lcf.h> |
---|---|---|---|
2 | |||
3 | #include "kbc.h" |
||
4 | |||
5 | #include "kbc_macros.h" |
||
6 | |||
7 | int (unsubscribe_interrupt)(int *interrupt_id) { |
||
8 | if (interrupt_id == NULL) return 1; |
||
9 | return sys_irqrmpolicy(interrupt_id);
|
||
10 | } |
||
11 | |||
12 | int (kbc_read_cmd)(uint8_t *cmd){
|
||
13 | if(kbc_issue_cmd(READ_KBC_CMD)) return 1; |
||
14 | if(kbc_read_byte(cmd)) return 1; |
||
15 | return 0; |
||
16 | } |
||
17 | |||
18 | int (kbc_change_cmd)(uint8_t cmd){
|
||
19 | if(kbc_issue_cmd(WRITE_KBC_CMD)) return 1; |
||
20 | if(sys_outb(KBC_CMD_ARG, cmd)) return 1; |
||
21 | return 0; |
||
22 | } |
||
23 | |||
24 | int (kbc_restore_kbd)(){
|
||
25 | uint8_t cmd = 0;
|
||
26 | if(kbc_read_cmd(&cmd)) return 1; |
||
27 | cmd = (cmd | INT_KBD) & (~DIS_KBD) ; |
||
28 | if(kbc_change_cmd(cmd)) return 1; |
||
29 | return 0; |
||
30 | } |
||
31 | |||
32 | int (kbc_issue_cmd)(uint8_t cmd){
|
||
33 | uint8_t stat; |
||
34 | for(int i = 0; i < KBC_NUM_TRIES; ++i){ |
||
35 | if(util_sys_inb(STATUS_REG, &stat)) return 1; |
||
36 | if((stat&IN_BUF_FULL) == 0){ |
||
37 | if(sys_outb(KBC_CMD, cmd)) return 1; |
||
38 | return 0; |
||
39 | } |
||
40 | tickdelay(micros_to_ticks(DELAY)); |
||
41 | } |
||
42 | return 1; |
||
43 | } |
||
44 | |||
45 | int (kbc_read_byte)(uint8_t *byte){
|
||
46 | uint8_t stat; |
||
47 | while(true){ |
||
48 | if(util_sys_inb(STATUS_REG, &stat)) return 1; |
||
49 | if((stat&OUT_BUF_FUL) && (stat&AUX_MOUSE)==0){ |
||
50 | if(stat & (PARITY_ERROR | TIME_OUT_REC)) return 1; |
||
51 | if(util_sys_inb(OUTPUT_BUF, byte)) return 1; |
||
52 | else return 0; |
||
53 | } |
||
54 | tickdelay(micros_to_ticks(DELAY)); |
||
55 | } |
||
56 | } |