root / lab3 / keyboard.c @ 15
History | View | Annotate | Download (2.46 KB)
1 | 10 | up20180645 | #include "keyboard.h" |
---|---|---|---|
2 | 9 | up20180645 | |
3 | 10 | up20180645 | uint8_t scan_code=0; // make code or break code |
4 | 9 | up20180645 | int keyboard_id = KEYBOARD_IRQ; // KEYBOARD_IRQ is defined in interrupt.h in .minix-src folder |
5 | uint32_t cnt = 0; // counter of sys_inb calls |
||
6 | |||
7 | int (util_sys_inb)(int port, uint8_t *value) { //transform 8 bit into 32 bit |
||
8 | |||
9 | uint32_t new_val; //initializing 32 bit variable
|
||
10 | |||
11 | if(sys_inb(port,&new_val)!=0){ //verifies if there is an error |
||
12 | printf("Error in util_sys_inb\n");
|
||
13 | return 1; |
||
14 | } |
||
15 | *value=new_val & 0xFF; //dereferencing "value" |
||
16 | #ifdef LAB3
|
||
17 | cnt++; |
||
18 | #endif
|
||
19 | |||
20 | return 0; |
||
21 | } |
||
22 | |||
23 | void (kbc_ih)(void){ |
||
24 | 10 | up20180645 | uint8_t status_reg; |
25 | 9 | up20180645 | |
26 | 10 | up20180645 | if(util_sys_inb(STATUS_REG,&status_reg)!=0){ //checks if there is an error |
27 | 12 | up20180645 | printf("Error reading status register in the keyboard interruption\n");
|
28 | 9 | up20180645 | return;
|
29 | } |
||
30 | 11 | up20180645 | if(((status_reg & STAT_REG_OBF)==0) ||((status_reg&(STAT_REG_PAR|STAT_REG_TIMEOUT))!=0)|| ((status_reg&STAT_REG_AUX)!=0)){ //checks if there is a parity or timeout error (mask -> 0xC0, bit 7 and 6 set), checks if output buffer is empty and checks if there is data coming from the mouse |
31 | 12 | up20180645 | printf("Parity/Timeout error or output buffer is empty or data coming from the mouse\n");
|
32 | 9 | up20180645 | return;
|
33 | } |
||
34 | 12 | up20180645 | if(util_sys_inb(OUTPUT_BUF,&scan_code)!=0){//checks if there is an error |
35 | printf("Error reading output buffer in the keyboard interruption\n");
|
||
36 | return;
|
||
37 | } |
||
38 | 9 | up20180645 | |
39 | } |
||
40 | |||
41 | 11 | up20180645 | |
42 | 9 | up20180645 | int (kbc_subscribe_int)(uint8_t *bit_no) { //similar function to that of timer_subscribe_int |
43 | *bit_no = BIT(keyboard_id); |
||
44 | 12 | up20180645 | if(sys_irqsetpolicy(KEYBOARD_IRQ,(IRQ_REENABLE|IRQ_EXCLUSIVE),&keyboard_id)==1){ //operation to subscribe int |
45 | 9 | up20180645 | printf("Error subscribing int\n");
|
46 | return 1; |
||
47 | } |
||
48 | return 0; |
||
49 | } |
||
50 | |||
51 | int (kbc_unsubscribe_int)() { //similar function to that of timer_unsubscribe_int |
||
52 | if(sys_irqrmpolicy(&keyboard_id)==1){ |
||
53 | printf("Error unsubscribing int\n");
|
||
54 | return 1; |
||
55 | } |
||
56 | return 0; |
||
57 | } |
||
58 | 11 | up20180645 | |
59 | 12 | up20180645 | int (reenable_interrupt)()
|
60 | 11 | up20180645 | { |
61 | uint8_t command; |
||
62 | |||
63 | if(sys_outb(STATUS_REG,READ_COMMAND)!=0){ |
||
64 | printf("Error writing read-command\n");
|
||
65 | return 1; |
||
66 | } |
||
67 | |||
68 | if(util_sys_inb(OUTPUT_BUF, &command)!=0){ |
||
69 | 12 | up20180645 | printf("Error reading output buffer in enable_interrupt\n");
|
70 | 11 | up20180645 | return 1; |
71 | } |
||
72 | |||
73 | command = command | INT_ENABLE; |
||
74 | |||
75 | if(sys_outb(STATUS_REG,OUTPUT_BUF)!=0){ |
||
76 | printf("Error writing output buffer\n");
|
||
77 | return 1; |
||
78 | } |
||
79 | |||
80 | if(sys_outb(OUTPUT_BUF,command)!=0){ |
||
81 | printf("Error writing new buffer status\n");
|
||
82 | return 1; |
||
83 | } |
||
84 | |||
85 | return 0; |
||
86 | } |