grml...
[seabios.git] / src / kbd.c
index 65b8a94c05ed3a1762ec893225745c18adf086e4..1977c5d018c72ff87da3a8e90882090d97a3b356 100644 (file)
--- a/src/kbd.c
+++ b/src/kbd.c
@@ -8,9 +8,9 @@
 #include "biosvar.h" // GET_BDA
 #include "util.h" // debug_enter
 #include "config.h" // CONFIG_*
-#include "pic.h" // eoi_pic1
 #include "bregs.h" // struct bregs
-#include "ps2port.h" // i8042_flush
+#include "ps2port.h" // ps2_kbd_command
+#include "usb-hid.h" // usb_kbd_command
 
 // Bit definitions for BDA kbd_flag[012]
 #define KF0_RSHIFT       (1<<0)
@@ -23,6 +23,7 @@
 
 #define KF1_LCTRL        (1<<0)
 #define KF1_LALT         (1<<1)
+#define KF1_PAUSEACTIVE  (1<<3)
 #define KF1_SCROLL       (1<<4)
 #define KF1_NUM          (1<<5)
 #define KF1_CAPS         (1<<6)
 #define KF2_RALT       (1<<3)
 #define KF2_101KBD     (1<<4)
 
-static void
-keyboard_init()
-{
-    /* 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
-kbd_setup()
+kbd_setup(void)
 {
     dprintf(3, "init keyboard\n");
     u16 x = offsetof(struct bios_data_area_s, kbd_buf);
@@ -113,13 +46,6 @@ kbd_setup()
 
     SET_BDA(kbd_buf_end_offset
             , x + FIELD_SIZEOF(struct bios_data_area_s, kbd_buf));
-
-    if (! CONFIG_KEYBOARD)
-        return;
-
-    keyboard_init();
-
-    enable_hwirq(1, entry_09);
 }
 
 static u8
@@ -148,6 +74,7 @@ enqueue_key(u8 scan_code, u8 ascii_code)
 static void
 dequeue_key(struct bregs *regs, int incr, int extended)
 {
+    yield();
     u16 buffer_head;
     u16 buffer_tail;
     for (;;) {
@@ -160,7 +87,7 @@ dequeue_key(struct bregs *regs, int incr, int extended)
             regs->flags |= F_ZF;
             return;
         }
-        cpu_relax();
+        wait_irq();
     }
 
     u8 ascii_code = GET_FARVAR(SEG_BDA, *(u8*)(buffer_head+0));
@@ -183,6 +110,14 @@ dequeue_key(struct bregs *regs, int incr, int extended)
     SET_BDA(kbd_buf_head, buffer_head);
 }
 
+static inline int
+kbd_command(int command, u8 *param)
+{
+    if (usb_kbd_active())
+        return usb_kbd_command(command, param);
+    return ps2_kbd_command(command, param);
+}
+
 // read keyboard input
 static void
 handle_1600(struct bregs *regs)
@@ -201,6 +136,7 @@ handle_1601(struct bregs *regs)
 static void
 handle_1602(struct bregs *regs)
 {
+    yield();
     regs->al = GET_BDA(kbd_flag0);
 }
 
@@ -259,6 +195,7 @@ handle_1611(struct bregs *regs)
 static void
 handle_1612(struct bregs *regs)
 {
+    yield();
     regs->al = GET_BDA(kbd_flag0);
     regs->ah = ((GET_BDA(kbd_flag1) & ~(KF2_RCTRL|KF2_RALT))
                 | (GET_BDA(kbd_flag2) & (KF2_RCTRL|KF2_RALT)));
@@ -291,11 +228,11 @@ handle_16a2(struct bregs *regs)
 static void
 handle_16XX(struct bregs *regs)
 {
-    debug_stub(regs);
+    warn_unimplemented(regs);
 }
 
 static void
-set_leds()
+set_leds(void)
 {
     u8 shift_flags = (GET_BDA(kbd_flag0) >> 4) & 0x07;
     u8 kbd_led = GET_BDA(kbd_led);
@@ -319,8 +256,7 @@ handle_16(struct bregs *regs)
     if (! CONFIG_KEYBOARD)
         return;
 
-    irq_enable();
-
+    // XXX - set_leds should be called from irq handler
     set_leds();
 
     switch (regs->ah) {
@@ -443,15 +379,36 @@ static struct scaninfo {
 };
 
 // Handle a scancode read from the ps2 port.  Note that "noinline" is
-// used to make sure the call to call16_simpint in handle_09 doesn't
+// used to make sure the call to call16_simpint in process_key doesn't
 // have the overhead of this function's stack.
 static void noinline
-process_key(u8 scancode)
+__process_key(u8 scancode)
 {
     u8 flags0 = GET_BDA(kbd_flag0);
     u8 flags1 = GET_BDA(kbd_flag1);
     u8 flags2 = GET_BDA(kbd_flag2);
 
+    if (flags2 & KF2_LAST_E1) {
+        // Part of "pause" key (sequence is e1 1d 45 e1 9d c5)
+        if ((scancode & ~0x80) == 0x1d)
+            // Second key of sequence
+            return;
+        // Third key of sequence - clear flag.
+        flags2 &= ~KF2_LAST_E1;
+        SET_BDA(kbd_flag2, flags2);
+
+        if (scancode == 0xc5) {
+            // Final key in sequence.
+
+            // XXX - do actual pause.
+        }
+        return;
+    }
+
+    // XXX - PrtScr should cause int 0x05
+    // XXX - Ctrl+Break should cause int 0x1B
+    // XXX - SysReq should cause int 0x15/0x85
+
     switch (scancode) {
     case 0x00:
         dprintf(1, "KBD: int09 handler: AL=0\n");
@@ -480,9 +437,6 @@ process_key(u8 scancode)
         break;
 
     case 0x1d: /* Ctrl press */
-        if (flags2 & KF2_LAST_E1)
-            // Part of a "pause" key
-            break;
         flags0 |= KF0_CTRLACTIVE;
         if (flags2 & KF2_LAST_E0)
             flags2 |= KF2_RCTRL;
@@ -490,9 +444,6 @@ process_key(u8 scancode)
             flags1 |= KF1_LCTRL;
         break;
     case 0x9d: /* Ctrl release */
-        if (flags2 & KF2_LAST_E1)
-            // Part of a "pause" key
-            break;
         flags0 &= ~KF0_CTRLACTIVE;
         if (flags2 & KF2_LAST_E0)
             flags2 &= ~KF2_RCTRL;
@@ -516,16 +467,10 @@ process_key(u8 scancode)
         break;
 
     case 0x45: /* Num Lock press */
-        if (flags2 & (KF2_LAST_E1|KF2_LAST_E0))
-            // Part of a "pause" key?
-            break;
         flags1 |= KF1_NUM;
         flags0 ^= KF0_NUMACTIVE;
         break;
     case 0xc5: /* Num Lock release */
-        if (flags2 & (KF2_LAST_E1|KF2_LAST_E0))
-            // Part of a "pause" key?
-            break;
         flags1 &= ~KF1_NUM;
         break;
 
@@ -543,10 +488,9 @@ process_key(u8 scancode)
         SET_BDA(kbd_flag2, flags2);
         return;
     case 0xe1:
-        // Pause key
+        // Start of pause key sequence
         flags2 |= KF2_LAST_E1;
-        SET_BDA(kbd_flag2, flags2);
-        return;
+        break;
 
     default:
         if (scancode & 0x80)
@@ -578,8 +522,8 @@ process_key(u8 scancode)
             asciicode = 0xe0;
             scancode = GET_GLOBAL(info->normal) >> 8;
         } else if (flags0 & (KF0_RSHIFT|KF0_LSHIFT)) {
-            /* check if lock state should be ignored
-             * because a SHIFT key are pressed */
+            /* check if lock state should be ignored because a SHIFT
+             * key is pressed */
 
             if (flags0 & GET_GLOBAL(info->lock_flags)) {
                 asciicode = GET_GLOBAL(info->normal);
@@ -603,9 +547,6 @@ process_key(u8 scancode)
         enqueue_key(scancode, asciicode);
         break;
     }
-    if ((scancode & 0x7f) != 0x1d)
-        // Completed "pause" key.
-        flags2 &= ~KF2_LAST_E1;
     flags2 &= ~KF2_LAST_E0;
 
     SET_BDA(kbd_flag0, flags0);
@@ -613,21 +554,11 @@ process_key(u8 scancode)
     SET_BDA(kbd_flag2, flags2);
 }
 
-// INT09h : Keyboard Hardware Service Entry Point
-void VISIBLE16
-handle_09()
+void
+process_key(u8 key)
 {
-    debug_isr(DEBUG_ISR_09);
-    if (! CONFIG_KEYBOARD)
-        goto done;
-
-    // read key from keyboard controller
-    u8 v = inb(PORT_PS2_STATUS);
-    if ((v & (I8042_STR_OBF|I8042_STR_AUXDATA)) != I8042_STR_OBF) {
-        dprintf(1, "keyboard irq but no keyboard data.\n");
-        goto done;
-    }
-    u8 key = inb(PORT_PS2_DATA);
+    if (!CONFIG_KEYBOARD)
+        return;
 
     if (CONFIG_KBD_CALL_INT15_4F) {
         // allow for keyboard intercept
@@ -635,11 +566,8 @@ handle_09()
         u32 flags;
         call16_simpint(0x15, &eax, &flags);
         if (!(flags & F_CF))
-            goto done;
+            return;
         key = eax;
     }
-    process_key(key);
-
-done:
-    eoi_pic1();
+    __process_key(key);
 }