Project

General

Profile

kbc.c

Bruno Silva, 05/12/2011 19:17

Download (3.94 KB)

 
1
#include <minix/syslib.h>
2
#include <minix/drivers.h>
3

    
4
#include "kbc.h"
5

    
6
static int hook, t_hook;
7
unsigned char S_code;
8

    
9
int test_scan(void) {
10

    
11
        int ipc_estado;
12
        message msg;
13
        int i = 1;
14

    
15

    
16
        int irq_set = KBCsubscribe_int();
17

    
18

    
19
        while (i) {
20
                if (driver_receive(ANY, &msg, &ipc_estado) != 0) {
21
                        printf("A receção do driver falhou com: %d");
22
                        continue;
23
                }
24
                if (is_ipc_notify(ipc_estado)) {
25

    
26
                        switch (_ENDPOINT_P(msg.m_source)) {
27
                        case HARDWARE:
28
                                if (msg.NOTIFY_ARG & irq_set) {
29

    
30
                                        S_code = read_kbc();
31

    
32

    
33
                                        if (S_code == 0x81)
34
                                                i = 0;
35
                                        if (S_code >> 7 == 1)
36
                                                printf("Breakcode: 0x%2x", S_code);
37
                                        else
38
                                                printf("Makecode: 0x%2x", S_code);
39
                                        printf("\n");
40

    
41
                                }
42
                                break;
43
                        default:
44
                                break;
45
                        }
46
                } else {
47
                }
48
        }
49

    
50
        KBCunsubscribe_int();
51

    
52
}
53

    
54
int test_leds(unsigned short n, unsigned short *leds) {
55

    
56
        int ipc_estado;
57
        message msg;
58
        int i;
59
        unsigned long irq_set = timer_subscribe_int();
60
        int j;
61
        unsigned long data;
62
        unsigned long BitMask, t_BitMask;
63
        int led0 = 0, led1 = 0, led2 = 0;
64

    
65
        for (i = 0; i < n; i++) {
66
                BitMask = 0;
67
                write_kbc(DATA_REG, LEDS_SWITCH);
68
                sys_inb(DATA_REG, &data);
69

    
70
                if (data == 0xFE) {
71
                        i--;
72
                        printf("\n Erro no ultimo byte");
73
                } else if (data == 0xFC) {
74
                        printf("\n Erro na sequência");
75
                        i = 0;
76
                } else {
77

    
78
                        switch (leds[i]) {
79
                        case 0:
80
                                if (led0 == 0) {
81
                                        printf("\nScroll Lock ON");
82
                                        led0 = BIT(0);
83
                                } else {
84
                                        led0 = 0;
85
                                        printf("\nScroll Lock OFF");
86
                                }
87
                                break;
88
                        case 1:
89
                                if (led1 == 0) {
90
                                        led1 = BIT(1);
91
                                        printf("\nNum Lock-ON");
92
                                } else {
93
                                        led1 = 0;
94
                                        printf("\nNum Lock-OFF");
95
                                }
96
                                break;
97
                        case 2:
98
                                if (led2 == 0) {
99
                                        led2 = BIT(2);
100
                                        printf("\nCaps Lock-ON");
101
                                } else {
102
                                        led2 = 0;
103
                                        printf("\nCaps Lock-OFF");
104
                                }
105
                                break;
106
                        default:
107
                                printf("\nERRO NA POSICAO %d", i);
108
                                break;
109
                        }
110
                        BitMask = led0 | led1 | led2;
111
                        write_kbc(DATA_REG, BitMask);
112
                        sys_inb(DATA_REG, &data);
113
                        if (data == 0xfe)
114
                                i--;
115
                        else if (data == 0xFC)
116
                                i = 0;
117
                        else {
118

    
119
                                j = 0;
120
                                while (1) {
121
                                        if (driver_receive(ANY, &msg, &ipc_estado) != 0) {
122
                                                printf("\nFalha na recepcao da driver");
123
                                                continue;
124
                                        }
125

    
126
                                        if (is_ipc_notify(ipc_estado)) {
127

    
128
                                                switch (_ENDPOINT_P(msg.m_source)) {
129
                                                case HARDWARE:
130
                                                        if (msg.NOTIFY_ARG & irq_set) {
131
                                                                j++;
132
                                                        }
133
                                                        break;
134
                                                default:
135
                                                        break;
136
                                                }
137
                                        } else {
138

    
139
                                        }
140
                                        if (j >= 60)
141
                                                break;
142
                                }
143
                                timer_unsubscribe_int();
144

    
145
                        }
146
                }
147
        }
148

    
149
}
150

    
151

    
152
unsigned long KBCsubscribe_int(void) {
153

    
154
        int response;
155
        int* BitMask;
156
        BitMask = (int*)malloc(sizeof(int));
157
        *BitMask = 0;
158
        response = sys_irqsetpolicy(KB_IRQ, (IRQ_REENABLE | IRQ_EXCLUSIVE), BitMask);
159
        hook = *BitMask;
160
        if (response == OK && sys_irqenable(BitMask) == OK) {
161
                return 1;
162
        } else
163
                return 0;
164
}
165

    
166
int KBCunsubscribe_int() {
167

    
168
        if (sys_irqdisable(&hook) == OK && sys_irqrmpolicy(&hook) == OK)
169
                return 0;
170
        else
171
                return 1;
172
}
173

    
174

    
175

    
176

    
177

    
178
int write_kbc(unsigned long port, unsigned char byte) {
179
        unsigned long estado;
180

    
181
        while (1) {
182
                sys_inb(STAT_REG, &estado);
183

    
184
                if ((estado & IBF) == 0) {
185
                        sys_outb(port, byte);
186
                        return 0;
187
                }
188
                tickdelay(micros_to_ticks(DELAY_US));
189
        }
190
        return -1;
191
}
192

    
193
int read_kbc() {
194
        unsigned long estado, data;
195
        while (1) {
196
                sys_inb(STAT_REG, &estado);
197

    
198
                if ((estado & OBF) != 0) {
199
                        if (AUX == 1)
200
                                return -1;
201
                        else
202
                                sys_inb(DATA_REG, &data);
203

    
204
                        if ((estado & (PAR_ERR | TO_ERR)) == 0)
205
                                return data;
206
                        else if (data == 0xFE)
207
                                return data;
208
                        else if (data == 0xFC)
209
                                return data;
210
                        else
211
                                return -1;
212
                }
213
                tickdelay(micros_to_ticks(DELAY_US));
214
        }
215
        return -1;
216
}
217

    
218
unsigned long timer_subscribe_int(void) {
219
        int* t_BitMask;
220
        t_BitMask = (int*) malloc(sizeof(int));
221
        *t_BitMask = 2;
222
        t_hook =  *t_BitMask;
223
        if (sys_irqsetpolicy(TIMER0_IRQ, IRQ_REENABLE, t_BitMask) == OK
224
                        && sys_irqenable(t_BitMask) == OK) {
225
                return 4;
226
        } else
227
                return 0;
228
}
229

    
230
int timer_unsubscribe_int() {
231
        if (sys_irqrmpolicy(&t_hook) == OK && sys_irqdisable(&t_hook) == OK)
232
                return 0;
233
        else
234
                return 1;
235
}
236