Project

General

Profile

Statistics
| Revision:

root / proj / src / interrupts_func.c @ 273

History | View | Annotate | Download (6.47 KB)

1
#include <lcom/lcf.h>
2

    
3
#include "interrupts_func.h"
4
#include "timer.h"
5
#include "i8254.h"
6
#include "keyboard.h"
7
#include "mouse.h"
8
#include "uart.h"
9
#include "utils.h"
10
#include "errors.h"
11

    
12
static int timer_subscribed = 0, timer_id;
13

    
14
static int keyboard_subscribed = 0, kbc_id;
15

    
16
static int mouse_subscribed = 0, mouse_id;
17

    
18
static int uart_subscribed = 0, uart_id;
19

    
20
static void (*const ih[])(void) =   {    timer_int_handler,
21
                                         kbc_ih,
22
                                         NULL,
23
                                         NULL,
24
                                         NULL,
25
                                         NULL,
26
                                         NULL,
27
                                         NULL,
28
                                         NULL,
29
                                         NULL,
30
                                         NULL,
31
                                         NULL,
32
                                         mouse_ih,
33
                                         NULL,
34
                                         NULL,
35
                                         NULL,
36
                                         NULL,
37
                                         NULL,
38
                                         NULL,
39
                                         NULL,
40
                                         NULL,
41
                                         NULL,
42
                                         NULL,
43
                                         NULL,
44
                                         NULL,
45
                                         NULL,
46
                                         NULL,
47
                                         NULL,
48
                                         NULL,
49
                                         NULL,
50
                                         NULL,
51
                                         NULL,
52
                                     };
53

    
54
int (subscribe_all)(void) {
55

    
56
    /// Timer interrupt handling
57
    timer_id = 0;
58
    if(subscribe_timer_interrupt(TIMER0_IRQ, &timer_id)) {
59
        printf("%s: failed to subscribe timer interrupts.\n", __func__);
60
        return SBCR_ERROR;
61
    }
62
    timer_subscribed = 1;
63

    
64
    /// Keyboard interrupt handling
65
    kbc_id = 0;
66
    if (subscribe_kbc_interrupt(KBC_IRQ, &kbc_id)) {
67
        printf("%s: failed to subscribe keyboard interrupts.\n", __func__);
68
        if (unsubscribe_all())
69
            printf("%s: failed to unsubcribe driver, unexpected behaviour is expected.\n");
70
        return SBCR_ERROR;
71
    }
72
    keyboard_subscribed = 1;
73

    
74
    /// Mouse interrupt handling
75
    mouse_id = 0;
76
    if (subscribe_mouse_interrupt(MOUSE_IRQ, &mouse_id)) { // subscribes mouse interrupts in exclusive mode
77
        printf("%s: failed to subscribe mouse interrupts.\n", __func__);
78
        if (unsubscribe_all())
79
            printf("%s: failed to unsubcribe driver, unexpected behaviour is expected.\n");
80
        return SBCR_ERROR;
81
    }
82
    mouse_subscribed = 1;
83
    if (sys_irqdisable(&mouse_id)) { // temporarily disables our interrupts notifications
84
        printf("%s: failed to disable mouse interrupts.\n", __func__);
85
        if (unsubscribe_all())
86
            printf("%s: failed to unsubcribe driver, unexpected behaviour is expected.\n");
87
        return SBCR_ERROR;
88
    }
89
    if (mouse_set_data_report(true)) { // enables mouse data reporting
90
        printf("%s: failed to enable mouse data reporting.\n", __func__);
91
        if (unsubscribe_all())
92
            printf("%s: failed to unsubcribe driver, unexpected behaviour is expected.\n");
93
        return SBCR_ERROR;
94
    }
95
    if (sys_irqenable(&mouse_id)) { // re-enables our interrupts notifications
96
        printf("%s: failed to enable mouse interrupts.\n", __func__);
97
        if (unsubscribe_all())
98
            printf("%s: failed to unsubcribe driver, unexpected behaviour is expected.\n");
99
        return SBCR_ERROR;
100
    }
101

    
102
    /// UART interrupt handling
103
    uart_id = 0;
104
    uart_set_bits_per_character(COM1_ADDR, 8);
105
    uart_set_stop_bits         (COM1_ADDR, 2);
106
    uart_set_parity            (COM1_ADDR, uart_parity_even);
107
    uart_set_bit_rate          (COM1_ADDR, 38400);
108
    uart_enable_int_rx (COM1_ADDR);
109
    uart_disable_int_tx(COM1_ADDR);
110
    if(subscribe_uart_interrupt(COM1_IRQ, &uart_id)) {
111
        printf("%s: failed to subscribe UART interrupts.\n", __func__);
112
        return SBCR_ERROR;
113
    }
114
    uart_subscribed = 1;
115

    
116
    return SUCCESS;
117
}
118

    
119
int (unsubscribe_all)(void) {
120
    int r = SUCCESS;
121

    
122
    // Unsubscribe Timer Interrupts
123
    if (timer_subscribed) {
124
        if (unsubscribe_interrupt(&timer_id)) {
125
            printf("%s: failed to unsubcribe timer interrupts.\n");
126
            r = UNSBCR_ERROR;
127
        }
128
        timer_subscribed = 0;
129
    }
130

    
131
    // Unsubscribe Keyboard interrupts
132
    if (keyboard_subscribed) {
133
        if (unsubscribe_interrupt(&kbc_id)) {
134
            printf("%s: failed to unsubcribe keyboard interrupts.\n");
135
            r = UNSBCR_ERROR;
136
        }
137
        keyboard_subscribed = 0;
138
    }
139

    
140
    // Unsubscribe Mouse Interrupts
141
    if (mouse_subscribed) {
142
        if (sys_irqdisable(&mouse_id)) { // temporarily disables our interrupts notifications
143
            printf("%s: failed to disable mouse interrupts.\n");
144
            r = UNSBCR_ERROR;
145
        }
146
        if (mouse_set_data_report(false)) { // disables mouse data reporting
147
            printf("%s: failed to disable mouse data reporting.\n");
148
            r = UNSBCR_ERROR;
149
        }
150
        if (sys_irqenable(&mouse_id)) { // re-enables our interrupts notifications
151
            printf("%s: failed to enable mouse interrupts.\n");
152
            r = UNSBCR_ERROR;
153
        }
154
        if (unsubscribe_interrupt(&mouse_id)) { // unsubscribes interrupts
155
            printf("%s: failed to unsubcribe mouse interrupts.\n");
156
            r = UNSBCR_ERROR;
157
        }
158
        mouse_subscribed = 0;
159
    }
160

    
161
    // Unsubscribe UART interrupts
162
    if (uart_subscribed) {
163
        if (unsubscribe_interrupt(&uart_id)) {
164
            printf("%s: failed to unsubcribe UART interrupts.\n");
165
            r = UNSBCR_ERROR;
166
        }
167
        uart_enable_int_rx(COM1_ADDR);
168
        uart_enable_int_tx(COM1_ADDR);
169
        uart_subscribed = 0;
170
    }
171

    
172
    return r;
173
}
174

    
175
void (interrupt_handler)(uint8_t handler) {
176
    if (handler >= 32)              return;
177
    if ((*ih[handler]) == NULL)     return;
178
    (*ih[handler])();
179
}
180

    
181
void (timer_manager)() {
182

    
183
}
184

    
185
void (mouse_manager)() {
186

    
187
}