+waitpid()
[meinos.git] / kernel2 / pic.c
blob43ea06a1c073c4c7a399cccc7161ccce836f5bd6
1 /*
2 meinOS - A unix-like x86 microkernel operating system
3 Copyright (C) 2008 Janosch Gräf <janosch.graef@gmx.net>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <pic.h>
20 #include <ioport.h>
22 int pic_init() {
23 int offset1 = 0x20;
24 int offset2 = 0x28;
25 char a1,a2;
26 a1 = inb(PIC1_DATA); // save masks
27 a2 = inb(PIC2_DATA);
29 outb(PIC1_COMMAND,ICW1_INIT+ICW1_ICW4); // starts the initialization sequence
30 outb(PIC2_COMMAND,ICW1_INIT+ICW1_ICW4);
31 outb(PIC1_DATA,offset1); // define the PIC vectors
32 outb(PIC2_DATA,offset2);
33 outb(PIC1_DATA,4); // continue initialization sequence
34 outb(PIC2_DATA,2);
35 outb(PIC1_DATA,ICW4_8086);
36 outb(PIC2_DATA,ICW4_8086);
38 outb(PIC1_DATA,a1); // restore saved masks.
39 outb(PIC2_DATA,a2);
41 return 0;
44 /**
45 * Gets IRQ mask
46 * @param irq IRQ
47 * @return IRQ mask
49 unsigned int pic_getmask(int irq) {
50 unsigned int mask = inb(PIC1_DATA);
51 mask |= inb(PIC2_DATA)<<8;
52 return mask;
55 /**
56 * Sets IRQ mask
57 * @param irq IRQ
58 * @param mask IRQ mask
60 void pic_setmask(int irq,unsigned int mask) {
61 outb(PIC1_DATA,mask);
62 outb(PIC2_DATA,mask>>8);
65 /**
66 * Sends EOI to PIC
67 * @param irq IRQ
69 void pic_eoi(int irq) {
70 if (irq>=8) outb(PIC2_COMMAND,PIC_EOI);
71 outb(PIC1_COMMAND,PIC_EOI);
74 /**
75 * Sets interval of PIT
76 * @param interval Interval
77 * @param channel Channel
78 * @return 0=Success; -1=Failure
80 int pic_pit_setinterval(int channel,unsigned int interval) {
81 unsigned int val = PIT_FREQ*interval/1000;
82 if (channel<4) {
83 outb(PIT_CHANNELS+channel,val);
84 outb(PIT_CHANNELS+channel,(val>>8));
85 return 0;
87 else return -1;