Handle tsc rollover.
[seabios.git] / src / ps2port.c
index b72def6cbfb1bfef28db72cf76d3e2e0377857e7..25d45446808573ec3b0f8f457104d75b824ebf20 100644 (file)
@@ -1,9 +1,9 @@
 // Support for handling the PS/2 mouse/keyboard ports.
 //
 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
-// Based on code Copyright (c) 1999-2004 Vojtech Pavlik
+// Several ideas taken from code Copyright (c) 1999-2004 Vojtech Pavlik
 //
-// 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.
 
 #include "ioport.h" // inb
 #include "util.h" // dprintf
 
 #define I8042_BUFFER_SIZE       16
 
-static void
-udelay(int usecs)
-{
-    // XXX - implement real udelay
-    outb(0x00, PORT_DIAG);
-}
-
 static int
 i8042_wait_read(void)
 {
+    dprintf(7, "i8042_wait_read\n");
     int i;
     for (i=0; i<I8042_CTL_TIMEOUT; i++) {
         u8 status = inb(PORT_PS2_STATUS);
@@ -44,6 +38,7 @@ i8042_wait_read(void)
 static int
 i8042_wait_write(void)
 {
+    dprintf(7, "i8042_wait_write\n");
     int i;
     for (i=0; i<I8042_CTL_TIMEOUT; i++) {
         u8 status = inb(PORT_PS2_STATUS);
@@ -58,6 +53,7 @@ i8042_wait_write(void)
 int
 i8042_flush(void)
 {
+    dprintf(7, "i8042_flush\n");
     unsigned long flags = irq_save();
 
     int i;
@@ -68,7 +64,8 @@ i8042_flush(void)
             return 0;
         }
         udelay(50);
-        inb(PORT_PS2_DATA);
+        u8 data = inb(PORT_PS2_DATA);
+        dprintf(7, "i8042 flushed %x (status=%x)\n", data, status);
     }
 
     irq_restore(flags);
@@ -103,6 +100,7 @@ __i8042_command(int command, u8 *param)
         if (ret)
             return ret;
         param[i] = inb(PORT_PS2_DATA);
+        dprintf(7, "i8042 param=%x\n", param[i]);
     }
 
     return 0;
@@ -111,6 +109,7 @@ __i8042_command(int command, u8 *param)
 int
 i8042_command(int command, u8 *param)
 {
+    dprintf(7, "i8042_command cmd=%x\n", command);
     unsigned long flags = irq_save();
     int ret = __i8042_command(command, param);
     irq_restore(flags);
@@ -122,6 +121,7 @@ i8042_command(int command, u8 *param)
 static int
 i8042_kbd_write(u8 c)
 {
+    dprintf(7, "i8042_kbd_write c=%d\n", c);
     unsigned long flags = irq_save();
 
     int ret = i8042_wait_write();
@@ -148,8 +148,41 @@ i8042_aux_write(u8 c)
 #define PS2_RET_NAK             0xfe
 
 static int
-ps2_sendbyte(int aux, u8 command)
+ps2_recvbyte(int aux, int needack, int timeout)
 {
+    u64 end = calc_future_tsc(timeout);
+    for (;;) {
+        if (check_time(end)) {
+            dprintf(1, "ps2_recvbyte timeout\n");
+            return -1;
+        }
+
+        u8 status = inb(PORT_PS2_STATUS);
+        if (! (status & I8042_STR_OBF))
+            continue;
+        u8 data = inb(PORT_PS2_DATA);
+        dprintf(7, "ps2 read %x\n", data);
+
+        if (!!(status & I8042_STR_AUXDATA) == aux) {
+            if (!needack)
+                return data;
+            if (data == PS2_RET_ACK)
+                return data;
+            if (data == PS2_RET_NAK) {
+                dprintf(1, "Got ps2 nak (status=%x); continuing\n", status);
+                return data;
+            }
+        }
+
+        // This data not for us - XXX - just discard it for now.
+        dprintf(1, "Discarding ps2 data %x (status=%x)\n", data, status);
+    }
+}
+
+static int
+ps2_sendbyte(int aux, u8 command, int timeout)
+{
+    dprintf(7, "ps2_sendbyte aux=%d cmd=%x\n", aux, command);
     int ret;
     if (aux)
         ret = i8042_aux_write(command);
@@ -159,14 +192,9 @@ ps2_sendbyte(int aux, u8 command)
         return ret;
 
     // Read ack.
-    ret = i8042_wait_read();
-    if (ret)
+    ret = ps2_recvbyte(aux, 1, timeout);
+    if (ret < 0)
         return ret;
-    u8 ack = inb(PORT_PS2_DATA);
-    if (ack != PS2_RET_ACK) {
-        dprintf(1, "Missing ack (got %x not %x)\n", ack, PS2_RET_ACK);
-        return -1;
-    }
 
     return 0;
 }
@@ -191,31 +219,49 @@ ps2_command(int aux, int command, u8 *param)
     if (ret)
         return ret;
 
-    // Send command.
-    ret = ps2_sendbyte(aux, command);
-    if (ret)
-        goto fail;
+    if (command == ATKBD_CMD_RESET_BAT) {
+        // Reset is special wrt timeouts.
 
-    // Send parameters (if any).
-    int i;
-    for (i = 0; i < send; i++) {
-        ret = ps2_sendbyte(aux, command);
+        // Send command.
+        ret = ps2_sendbyte(aux, command, 1000);
         if (ret)
             goto fail;
-    }
 
-    // Receive parameters (if any).
-    for (i = 0; i < receive; i++) {
-        ret = i8042_wait_read();
-        if (ret) {
-            // On a receive timeout, return the item number that the
-            // transfer failed on.
-            ret = i + 1;
+        // Receive parameters.
+        ret = ps2_recvbyte(aux, 0, 4000);
+        if (ret < 0)
             goto fail;
+        param[0] = ret;
+        ret = ps2_recvbyte(aux, 0, 100);
+        if (ret < 0)
+            // Some devices only respond with one byte on reset.
+            ret = 0;
+        param[1] = ret;
+    } else {
+        // Send command.
+        ret = ps2_sendbyte(aux, command, 200);
+        if (ret)
+            goto fail;
+
+        // Send parameters (if any).
+        int i;
+        for (i = 0; i < send; i++) {
+            ret = ps2_sendbyte(aux, param[i], 200);
+            if (ret)
+                goto fail;
+        }
+
+        // Receive parameters (if any).
+        for (i = 0; i < receive; i++) {
+            ret = ps2_recvbyte(aux, 0, 500);
+            if (ret < 0)
+                goto fail;
+            param[i] = ret;
         }
-        param[i] = inb(PORT_PS2_DATA);
     }
 
+    ret = 0;
+
 fail:
     // Restore interrupts and keyboard/mouse.
     ret2 = i8042_command(I8042_CMD_CTL_WCTR, &ps2ctr);
@@ -228,17 +274,19 @@ fail:
 int
 kbd_command(int command, u8 *param)
 {
+    dprintf(7, "kbd_command cmd=%x\n", command);
     int ret = ps2_command(0, command, param);
     if (ret)
-        dprintf(2, "keyboard command %x failed (ret=%d)\n", command, ret);
+        dprintf(2, "keyboard command %x failed\n", command);
     return ret;
 }
 
 int
 aux_command(int command, u8 *param)
 {
+    dprintf(7, "aux_command cmd=%x\n", command);
     int ret = ps2_command(1, command, param);
     if (ret)
-        dprintf(2, "mouse command %x failed (ret=%d)\n", command, ret);
+        dprintf(2, "mouse command %x failed\n", command);
     return ret;
 }