Be sure to add "void" to all function prototypes that take no args.
[seabios.git] / src / ps2port.c
index 837f25a8265884fa701a9c018b8e7e2cd4e930ab..fb9d24ae092f89a44079d940a147e2a6505a0dcf 100644 (file)
@@ -9,6 +9,7 @@
 #include "util.h" // dprintf
 #include "biosvar.h" // GET_EBDA
 #include "ps2port.h" // kbd_command
+#include "pic.h" // eoi_pic1
 
 
 /****************************************************************
@@ -54,21 +55,16 @@ int
 i8042_flush(void)
 {
     dprintf(7, "i8042_flush\n");
-    unsigned long flags = irq_save();
-
     int i;
     for (i=0; i<I8042_BUFFER_SIZE; i++) {
         u8 status = inb(PORT_PS2_STATUS);
-        if (! (status & I8042_STR_OBF)) {
-            irq_restore(flags);
+        if (! (status & I8042_STR_OBF))
             return 0;
-        }
         udelay(50);
         u8 data = inb(PORT_PS2_DATA);
         dprintf(7, "i8042 flushed %x (status=%x)\n", data, status);
     }
 
-    irq_restore(flags);
     dprintf(1, "i8042 timeout on flush\n");
     return -1;
 }
@@ -110,9 +106,7 @@ 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);
     if (ret)
         dprintf(2, "i8042 command %x failed\n", command);
     return ret;
@@ -122,14 +116,9 @@ 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();
     if (! ret)
         outb(c, PORT_PS2_DATA);
-
-    irq_restore(flags);
-
     return ret;
 }
 
@@ -147,30 +136,51 @@ i8042_aux_write(u8 c)
 #define PS2_RET_ACK             0xfa
 #define PS2_RET_NAK             0xfe
 
+static void
+process_ps2byte(u8 status, u8 data)
+{
+    if (!MODE16) {
+        // Don't pull in all of keyboard/mouse code into 32bit code -
+        // just discard the data.
+        dprintf(1, "Discarding ps2 data %x (status=%x)\n", data, status);
+        return;
+    }
+    if (status & I8042_STR_AUXDATA)
+        process_mouse(data);
+    else
+        process_key(data);
+}
+
 static int
 ps2_recvbyte(int aux, int needack, int timeout)
 {
     u64 end = calc_future_tsc(timeout);
     for (;;) {
-        if (rdtscll() >= 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)
-            || (needack && data != PS2_RET_ACK)) {
-            // This data not for us - XXX - just discard it for now.
-            dprintf(1, "Discarding ps2 data %x (status=%x)\n", data, status);
-            continue;
+        if (status & I8042_STR_OBF) {
+            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)\n", status);
+                    return data;
+                }
+            }
+
+            // Data not part of this command.
+            process_ps2byte(status, data);
         }
 
-        return data;
+        if (check_time(end)) {
+            dprintf(1, "ps2_recvbyte timeout\n");
+            return -1;
+        }
+        yield();
     }
 }
 
@@ -190,6 +200,8 @@ ps2_sendbyte(int aux, u8 command, int timeout)
     ret = ps2_recvbyte(aux, 1, timeout);
     if (ret < 0)
         return ret;
+    if (ret != PS2_RET_ACK)
+        return -1;
 
     return 0;
 }
@@ -285,3 +297,131 @@ aux_command(int command, u8 *param)
         dprintf(2, "mouse command %x failed\n", command);
     return ret;
 }
+
+
+/****************************************************************
+ * IRQ handlers
+ ****************************************************************/
+
+static void
+process_ps2irq(void)
+{
+    u8 status = inb(PORT_PS2_STATUS);
+    if (!(status & I8042_STR_OBF)) {
+        dprintf(1, "ps2 irq but no data.\n");
+        return;
+    }
+    u8 data = inb(PORT_PS2_DATA);
+
+    process_ps2byte(status, data);
+}
+
+// INT74h : PS/2 mouse hardware interrupt
+void VISIBLE16
+handle_74(void)
+{
+    if (! CONFIG_PS2PORT)
+        return;
+
+    debug_isr(DEBUG_ISR_74);
+    process_ps2irq();
+    eoi_pic2();
+}
+
+// INT09h : Keyboard Hardware Service Entry Point
+void VISIBLE16
+handle_09(void)
+{
+    if (! CONFIG_PS2PORT)
+        return;
+
+    debug_isr(DEBUG_ISR_09);
+    process_ps2irq();
+    eoi_pic1();
+}
+
+
+/****************************************************************
+ * Setup
+ ****************************************************************/
+
+static void
+keyboard_init(void *data)
+{
+    /* flush incoming keys */
+    int ret = i8042_flush();
+    if (ret)
+        return;
+
+    // Controller self-test.
+    u8 param[2];
+    ret = i8042_command(I8042_CMD_CTL_TEST, param);
+    if (ret)
+        return;
+    if (param[0] != 0x55) {
+        dprintf(1, "i8042 self test failed (got %x not 0x55)\n", param[0]);
+        return;
+    }
+
+    // Controller keyboard test.
+    ret = i8042_command(I8042_CMD_KBD_TEST, param);
+    if (ret)
+        return;
+    if (param[0] != 0x00) {
+        dprintf(1, "i8042 keyboard test failed (got %x not 0x00)\n", param[0]);
+        return;
+    }
+
+    // Enable keyboard and mouse ports.
+    ret = i8042_command(I8042_CMD_KBD_ENABLE, NULL);
+    if (ret)
+        return;
+    ret = i8042_command(I8042_CMD_AUX_ENABLE, NULL);
+    if (ret)
+        return;
+
+
+    /* ------------------- keyboard side ------------------------*/
+    /* reset keyboard and self test  (keyboard side) */
+    ret = kbd_command(ATKBD_CMD_RESET_BAT, param);
+    if (ret)
+        return;
+    if (param[0] != 0xaa) {
+        dprintf(1, "keyboard self test failed (got %x not 0xaa)\n", param[0]);
+        return;
+    }
+
+    /* Disable keyboard */
+    ret = kbd_command(ATKBD_CMD_RESET_DIS, NULL);
+    if (ret)
+        return;
+
+    // Set scancode command (mode 2)
+    param[0] = 0x02;
+    ret = kbd_command(ATKBD_CMD_SSCANSET, param);
+    if (ret)
+        return;
+
+    // Keyboard Mode: scan code convert, disable mouse, enable IRQ 1
+    SET_EBDA(ps2ctr, I8042_CTR_AUXDIS | I8042_CTR_XLATE | I8042_CTR_KBDINT);
+
+    /* Enable keyboard */
+    ret = kbd_command(ATKBD_CMD_ENABLE, NULL);
+    if (ret)
+        return;
+
+    dprintf(1, "keyboard initialized\n");
+}
+
+void
+ps2port_setup(void)
+{
+    if (! CONFIG_PS2PORT)
+        return;
+    dprintf(3, "init ps2port\n");
+
+    enable_hwirq(1, entry_09);
+    enable_hwirq(12, entry_74);
+
+    run_thread(keyboard_init, NULL);
+}