Change license from GPLv3 to LGPLv3.
[seabios.git] / src / pic.h
index dc151a280dd1b8bee1f18aa79f20e34c152f690f..3056676ccbf9a3476daa1cc4f2424b08f71aa83f 100644 (file)
--- a/src/pic.h
+++ b/src/pic.h
@@ -3,12 +3,11 @@
 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
 // Copyright (C) 2002  MandrakeSoft S.A.
 //
-// This file may be distributed under the terms of the GNU GPLv3 license.
+// This file may be distributed under the terms of the GNU LGPLv3 license.
 #ifndef __PIC_H
 #define __PIC_H
 
 #include "ioport.h" // PORT_PIC*
-#include "util.h" // dprintf
 
 // PORT_PIC1 bitdefs
 #define PIC1_IRQ0  (1<<0)
@@ -49,6 +48,18 @@ unmask_pic2(u8 irq)
     outb(inb(PORT_PIC2_DATA) & ~irq, PORT_PIC2_DATA);
 }
 
+static inline void
+mask_pic1(u8 irq)
+{
+    outb(inb(PORT_PIC1_DATA) | irq, PORT_PIC1_DATA);
+}
+
+static inline void
+mask_pic2(u8 irq)
+{
+    outb(inb(PORT_PIC2_DATA) | irq, PORT_PIC2_DATA);
+}
+
 static inline u8
 get_pic1_isr()
 {
@@ -57,25 +68,36 @@ get_pic1_isr()
     return inb(PORT_PIC1_CMD);
 }
 
+static inline u8
+get_pic2_isr()
+{
+    // 0x0b == select OCW1 + read ISR
+    outb(0x0b, PORT_PIC2_CMD);
+    return inb(PORT_PIC2_CMD);
+}
+
+// post.c
+void __set_irq(int vector, void *loc);
+
 static inline void
-pic_setup()
+__enable_hwirq(int hwirq, void (*func)(void))
 {
-    dprintf(3, "init pic\n");
-    // Send ICW1 (select OCW1 + will send ICW4)
-    outb(0x11, PORT_PIC1_CMD);
-    outb(0x11, PORT_PIC2_CMD);
-    // Send ICW2 (base irqs: 0x08-0x0f for irq0-7, 0x70-0x78 for irq8-15)
-    outb(0x08, PORT_PIC1_DATA);
-    outb(0x70, PORT_PIC2_DATA);
-    // Send ICW3 (cascaded pic ids)
-    outb(0x04, PORT_PIC1_DATA);
-    outb(0x02, PORT_PIC2_DATA);
-    // Send ICW4 (enable 8086 mode)
-    outb(0x01, PORT_PIC1_DATA);
-    outb(0x01, PORT_PIC2_DATA);
-    // Mask all irqs (except cascaded PIC2 irq)
-    outb(~PIC1_IRQ2, PORT_PIC1_DATA);
-    outb(~0, PORT_PIC2_DATA);
+    int vector;
+    if (hwirq < 8) {
+        unmask_pic1(1 << hwirq);
+        vector = 0x08 + hwirq;
+    } else {
+        unmask_pic2(1 << (hwirq - 8));
+        vector = 0x70 + hwirq - 8;
+    }
+    __set_irq(vector, func);
 }
 
+#define enable_hwirq(irq, func) do {            \
+        extern void func (void);                \
+        __enable_hwirq(irq, func);              \
+    } while (0)
+
+void pic_setup();
+
 #endif // pic.h