summaryrefslogtreecommitdiff
path: root/arch/i386/kernel/pic.c
blob: 3cf1d9445d5f2513ed309fa9d4f213eb2a44babb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <kernel/pic.h>

static void (*irq_handlers[16])(struct regs *regs);

void pic_eoi(uint8_t irq) {
        if (irq >= 8)
                outb(PIC2_COMMAND, 0x20);
        outb(PIC1_COMMAND, 0x20);
}

void pic_remap(void) {
        unsigned char a1 = inb(PIC1_DATA);
        unsigned char a2 = inb(PIC2_DATA);

        outb(PIC1_COMMAND, ICW1_INIT | ICW1_ICW4);
        outb(PIC2_COMMAND, ICW1_INIT | ICW1_ICW4);
        outb(PIC1_DATA, PIC1);
        outb(PIC2_DATA, PIC2);
        outb(PIC1_DATA, 4);
        outb(PIC2_DATA, 2);
        outb(PIC1_DATA, ICW4_8086);
        outb(PIC2_DATA, ICW4_8086);
        outb(PIC1_DATA, a1);
        outb(PIC2_DATA, a2);
}

static uint16_t _pic_get_irq_reg(int ocw3) {
        outb(PIC1_COMMAND, ocw3);
        outb(PIC2_COMMAND, ocw3);
        return (inb(PIC2_COMMAND) << 8) | inb(PIC1_COMMAND);
}

uint16_t pic_get_irr(void) {
        return _pic_get_irq_reg(PIC_READ_IRR);
}

uint16_t pic_get_isr(void) {
        return _pic_get_irq_reg(PIC_READ_ISR);
}

void register_irq_handler(uint8_t irq, void (*handler)(struct regs *regs)) {
        irq_handlers[irq] = handler;
}

void irq_dispatch(struct regs *regs) {
        (*irq_handlers[regs->isr_vector-32])(regs);
        pic_eoi(regs->isr_vector-32);
        return;
}

void irq_set_mask(uint8_t irq) {
        uint16_t port;
        uint8_t data;

        if (irq < 8) {
                port = PIC1_DATA;
        } else {
                port = PIC2_DATA;
                irq -= 8;
        }

        data = inb(port) | (1 << irq);
        outb(port, data);
}

void irq_clear_mask(uint8_t irq) {
        uint16_t port;
        uint8_t data;

        if (irq < 8) {
                port = PIC1_DATA;
        } else {
                port = PIC2_DATA;
                irq -= 8;
        }

        data = inb(port) & ~(1 << irq);
        outb(port, data);
}