Revision 68
working decently
kbc.c | ||
---|---|---|
3 | 3 |
#include "kbc.h" |
4 | 4 |
|
5 | 5 |
#include "kbc_macros.h" |
6 |
#include "utils.h" |
|
7 |
#include "errors.h" |
|
6 | 8 |
|
7 | 9 |
int (unsubscribe_interrupt)(int *interrupt_id) { |
8 |
if (interrupt_id == NULL) return 1; |
|
9 |
return sys_irqrmpolicy(interrupt_id); |
|
10 |
if (interrupt_id == NULL) return NULL_PTR; |
|
11 |
if(sys_irqrmpolicy(interrupt_id)) return UNSBCR_ERROR; |
|
12 |
return SUCCESS; |
|
10 | 13 |
} |
11 | 14 |
|
12 | 15 |
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 |
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; |
|
16 | 20 |
} |
17 | 21 |
|
18 | 22 |
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; |
|
23 |
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; |
|
22 | 27 |
} |
23 | 28 |
|
24 | 29 |
int (kbc_restore_kbd)(){ |
30 |
int ret = 0; |
|
25 | 31 |
uint8_t cmd = 0; |
26 |
if(kbc_read_cmd(&cmd)) return 1;
|
|
32 |
if((ret = kbc_read_cmd(&cmd))) return ret;
|
|
27 | 33 |
cmd = (cmd | INT_KBD) & (~DIS_KBD) ; |
28 |
if(kbc_change_cmd(cmd)) return 1;
|
|
29 |
return 0;
|
|
34 |
if((ret = kbc_change_cmd(cmd))) return ret;
|
|
35 |
return SUCCESS;
|
|
30 | 36 |
} |
31 | 37 |
|
32 | 38 |
int (kbc_issue_cmd)(uint8_t cmd){ |
39 |
int ret = 0; |
|
33 | 40 |
uint8_t stat; |
34 | 41 |
for(int i = 0; i < KBC_NUM_TRIES; ++i){ |
35 |
if(util_sys_inb(STATUS_REG, &stat)) return 1;
|
|
42 |
if((ret = util_sys_inb(STATUS_REG, &stat))) return ret;
|
|
36 | 43 |
if((stat&IN_BUF_FULL) == 0){ |
37 |
if(sys_outb(KBC_CMD, cmd)) return 1;
|
|
38 |
return 0;
|
|
44 |
if(sys_outb(KBC_CMD, cmd)) return WRITE_ERROR;
|
|
45 |
return SUCCESS;
|
|
39 | 46 |
} |
40 | 47 |
tickdelay(micros_to_ticks(DELAY)); |
41 | 48 |
} |
42 |
return 1;
|
|
49 |
return TIMEOUT_ERROR;
|
|
43 | 50 |
} |
44 | 51 |
|
52 |
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 |
|
|
45 | 66 |
int (kbc_read_byte)(uint8_t *byte){ |
67 |
int ret = 0; |
|
46 | 68 |
uint8_t stat; |
47 |
while(true){
|
|
48 |
if(util_sys_inb(STATUS_REG, &stat)) return 1;
|
|
69 |
for(int i = 0; i < KBC_NUM_TRIES; ++i){
|
|
70 |
if((ret = util_sys_inb(STATUS_REG, &stat))) return ret;
|
|
49 | 71 |
if((stat&OUT_BUF_FUL) && (stat&AUX_MOUSE)==0){ |
50 | 72 |
if(stat & (PARITY_ERROR | TIME_OUT_REC)) return 1; |
51 |
if(util_sys_inb(OUTPUT_BUF, byte)) return 1;
|
|
52 |
else return 0;
|
|
73 |
if((ret = util_sys_inb(OUTPUT_BUF, byte))) return ret;
|
|
74 |
else return SUCCESS;
|
|
53 | 75 |
} |
54 | 76 |
tickdelay(micros_to_ticks(DELAY)); |
55 | 77 |
} |
78 |
return TIMEOUT_ERROR; |
|
56 | 79 |
} |
Also available in: Unified diff