grml...
[seabios.git] / src / serial.c
index a24f83f48d029204170774f9b1e4c6987e26aed4..21b4bd010586166bc4cfe8453a268147870403aa 100644 (file)
@@ -9,31 +9,6 @@
 #include "util.h" // debug_enter
 #include "bregs.h" // struct bregs
 
-// Timers based on 18.2Hz clock irq.
-struct tick_timer_s {
-    u16 last_tick, remaining;
-};
-
-struct tick_timer_s
-initTickTimer(u16 count)
-{
-    struct tick_timer_s tt = {GET_BDA(timer_counter), count};
-    return tt;
-}
-
-int
-checkTickTimer(struct tick_timer_s *tt)
-{
-    u16 timer = GET_BDA(timer_counter);
-    if (tt->last_tick != timer) {
-        tt->last_tick = timer;
-        tt->last_tick--;
-        if (!tt->last_tick)
-            return 1;
-    }
-    return 0;
-}
-
 
 /****************************************************************
  * COM ports
@@ -57,7 +32,7 @@ detect_serial(u16 port, u8 timeout, u8 count)
 }
 
 void
-serial_setup()
+serial_setup(void)
 {
     if (! CONFIG_SERIAL)
         return;
@@ -79,12 +54,12 @@ static u16
 getComAddr(struct bregs *regs)
 {
     if (regs->dx >= 4) {
-        set_fail(regs);
+        set_invalid(regs);
         return 0;
     }
     u16 addr = GET_BDA(port_com[regs->dx]);
     if (! addr)
-        set_fail(regs);
+        set_invalid(regs);
     return addr;
 }
 
@@ -117,8 +92,7 @@ handle_1401(struct bregs *regs)
     u16 addr = getComAddr(regs);
     if (!addr)
         return;
-    struct tick_timer_s tt = initTickTimer(GET_BDA(com_timeout[regs->dx]));
-    irq_enable();
+    u32 end = calc_future_timer_ticks(GET_BDA(com_timeout[regs->dx]));
     for (;;) {
         u8 lsr = inb(addr+SEROFF_LSR);
         if ((lsr & 0x60) == 0x60) {
@@ -128,13 +102,13 @@ handle_1401(struct bregs *regs)
             regs->ah = lsr;
             break;
         }
-        if (checkTickTimer(&tt)) {
+        if (check_timer(end)) {
             // Timed out - can't write data.
             regs->ah = lsr | 0x80;
             break;
         }
+        yield();
     }
-    irq_disable();
     set_success(regs);
 }
 
@@ -145,8 +119,7 @@ handle_1402(struct bregs *regs)
     u16 addr = getComAddr(regs);
     if (!addr)
         return;
-    struct tick_timer_s tt = initTickTimer(GET_BDA(com_timeout[regs->dx]));
-    irq_enable();
+    u32 end = calc_future_timer_ticks(GET_BDA(com_timeout[regs->dx]));
     for (;;) {
         u8 lsr = inb(addr+SEROFF_LSR);
         if (lsr & 0x01) {
@@ -155,13 +128,13 @@ handle_1402(struct bregs *regs)
             regs->ah = lsr;
             break;
         }
-        if (checkTickTimer(&tt)) {
+        if (check_timer(end)) {
             // Timed out - can't read data.
             regs->ah = lsr | 0x80;
             break;
         }
+        yield();
     }
-    irq_disable();
     set_success(regs);
 }
 
@@ -180,8 +153,7 @@ handle_1403(struct bregs *regs)
 static void
 handle_14XX(struct bregs *regs)
 {
-    // Unsupported
-    set_fail(regs);
+    set_unimplemented(regs);
 }
 
 // INT 14h Serial Communications Service Entry Point
@@ -227,7 +199,7 @@ detect_parport(u16 port, u8 timeout, u8 count)
 }
 
 void
-lpt_setup()
+lpt_setup(void)
 {
     if (! CONFIG_LPT)
         return;
@@ -247,12 +219,12 @@ static u16
 getLptAddr(struct bregs *regs)
 {
     if (regs->dx >= 3) {
-        set_fail(regs);
+        set_invalid(regs);
         return 0;
     }
     u16 addr = GET_BDA(port_lpt[regs->dx]);
     if (! addr)
-        set_fail(regs);
+        set_invalid(regs);
     return addr;
 }
 
@@ -264,8 +236,7 @@ handle_1700(struct bregs *regs)
     if (!addr)
         return;
 
-    struct tick_timer_s tt = initTickTimer(GET_BDA(lpt_timeout[regs->dx]));
-    irq_enable();
+    u32 end = calc_future_timer_ticks(GET_BDA(lpt_timeout[regs->dx]));
 
     outb(regs->al, addr);
     u8 val8 = inb(addr+2);
@@ -280,14 +251,14 @@ handle_1700(struct bregs *regs)
             regs->ah = v ^ 0x48;
             break;
         }
-        if (checkTickTimer(&tt)) {
+        if (check_timer(end)) {
             // Timeout
             regs->ah = (v ^ 0x48) | 0x01;
             break;
         }
+        yield();
     }
 
-    irq_disable();
     set_success(regs);
 }
 
@@ -322,8 +293,7 @@ handle_1702(struct bregs *regs)
 static void
 handle_17XX(struct bregs *regs)
 {
-    // Unsupported
-    set_fail(regs);
+    set_unimplemented(regs);
 }
 
 // INT17h : Printer Service Entry Point