Project

General

Profile

Statistics
| Revision:

root / proj / src / interrupts_func.c @ 202

History | View | Annotate | Download (5.51 KB)

1
#include <lcom/lcf.h>
2

    
3
#include "interrupts_func.h"
4
#include "timer.h"
5
#include "i8254.h"
6
#include "kbc_macros.h"
7
#include "keyboard.h"
8
#include "mouse.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 void (*const ih[])(void) =   {    timer_int_handler,
19
                                         kbc_ih,
20
                                         NULL,
21
                                         NULL,
22
                                         NULL,
23
                                         NULL,
24
                                         NULL,
25
                                         NULL,
26
                                         NULL,
27
                                         NULL,
28
                                         NULL,
29
                                         NULL,
30
                                         mouse_ih,
31
                                         NULL,
32
                                         NULL,
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
                                     };
51

    
52
int (subscribe_all)(void) {
53

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

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

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

    
100
    return SUCCESS;
101
}
102

    
103
int (unsubscribe_all)(void) {
104
    int r = SUCCESS;
105

    
106
    // Unsubscribe Timer Interrupts
107
    if (timer_subscribed) {
108
        if (unsubscribe_interrupt(&timer_id)) {
109
            printf("%s: failed to unsubcribe timer interrupts.\n");
110
            r = UNSBCR_ERROR;
111
        }
112
        timer_subscribed = 0;
113
    }
114

    
115
    // Unsubscribe Keyboard interrupts
116
    if (keyboard_subscribed) {
117
        if (unsubscribe_interrupt(&kbc_id)) {
118
            printf("%s: failed to unsubcribe keyboard interrupts.\n");
119
            r = UNSBCR_ERROR;
120
        }
121
        keyboard_subscribed = 0;
122
    }
123

    
124
    // Unsubscribe Mouse Interrupts
125
    if (mouse_subscribed) {
126
        if (sys_irqdisable(&mouse_id)) { // temporarily disables our interrupts notifications
127
            printf("%s: failed to disable mouse interrupts.\n");
128
            r = UNSBCR_ERROR;
129
        }
130
        if (mouse_set_data_report(false)) { // disables mouse data reporting
131
            printf("%s: failed to disable mouse data reporting.\n");
132
            r = UNSBCR_ERROR;
133
        }
134
        if (sys_irqenable(&mouse_id)) { // re-enables our interrupts notifications
135
            printf("%s: failed to enable mouse interrupts.\n");
136
            r = UNSBCR_ERROR;
137
        }
138
        if (unsubscribe_interrupt(&mouse_id)) { // unsubscribes interrupts
139
            printf("%s: failed to unsubcribe mouse interrupts.\n");
140
            r = UNSBCR_ERROR;
141
        }
142
        mouse_subscribed = 0;
143
    }
144

    
145
    return r;
146
}
147

    
148
void interrupt_handler(uint8_t handler) {
149
    if (handler >= 32)              return;
150
    if ((*ih[handler]) == NULL)     return;
151
    (*ih[handler])();
152
}