Change license from GPLv3 to LGPLv3.
[seabios.git] / src / kbd.c
index f4a1c95f757fad1437eb7b6bcb9284d84e69d645..e976350bf09c71d3dde1edc5dceaf4a03f30e4cb 100644 (file)
--- a/src/kbd.c
+++ b/src/kbd.c
@@ -3,7 +3,7 @@
 // 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.
 
 #include "biosvar.h" // GET_BDA
 #include "util.h" // debug_enter
@@ -30,7 +30,7 @@ keyboard_init()
     if (ret)
         return;
     if (param[0] != 0x55) {
-        dprintf(1, "i8042 self test failed (got %x not 0x55\n", param[0]);
+        dprintf(1, "i8042 self test failed (got %x not 0x55)\n", param[0]);
         return;
     }
 
@@ -39,7 +39,7 @@ keyboard_init()
     if (ret)
         return;
     if (param[0] != 0x00) {
-        dprintf(1, "i8042 keyboard test failed (got %x not 0x00\n", param[0]);
+        dprintf(1, "i8042 keyboard test failed (got %x not 0x00)\n", param[0]);
         return;
     }
 
@@ -55,10 +55,10 @@ keyboard_init()
     /* ------------------- keyboard side ------------------------*/
     /* reset keyboard and self test  (keyboard side) */
     ret = kbd_command(ATKBD_CMD_RESET_BAT, param);
-    if (ret)
+    if (ret != 0 && ret != 2)
         return;
     if (param[0] != 0xaa) {
-        dprintf(1, "keyboard self test failed (got %x not 0xaa\n", param[0]);
+        dprintf(1, "keyboard self test failed (got %x not 0xaa)\n", param[0]);
         return;
     }
 
@@ -83,7 +83,7 @@ void
 kbd_setup()
 {
     dprintf(3, "init keyboard\n");
-    u16 x = offsetof(struct bios_data_area_s, kbd_buf) - 0x400;
+    u16 x = offsetof(struct bios_data_area_s, kbd_buf);
     SET_BDA(kbd_mode, 0x10);
     SET_BDA(kbd_buf_head, x);
     SET_BDA(kbd_buf_tail, x);
@@ -97,8 +97,7 @@ kbd_setup()
 
     keyboard_init();
 
-    // Enable IRQ1 (handle_09)
-    unmask_pic1(PIC1_IRQ1);
+    enable_hwirq(1, entry_09);
 }
 
 static u8
@@ -118,14 +117,14 @@ enqueue_key(u8 scan_code, u8 ascii_code)
     if (buffer_tail == buffer_head)
         return 0;
 
-    SET_FARVAR(SEG_BDA, *(u8*)(temp_tail+0x400+0), ascii_code);
-    SET_FARVAR(SEG_BDA, *(u8*)(temp_tail+0x400+1), scan_code);
+    SET_FARVAR(SEG_BDA, *(u8*)(temp_tail+0), ascii_code);
+    SET_FARVAR(SEG_BDA, *(u8*)(temp_tail+1), scan_code);
     SET_BDA(kbd_buf_tail, buffer_tail);
     return 1;
 }
 
-static u8
-dequeue_key(u8 *scan_code, u8 *ascii_code, u8 incr)
+static void
+dequeue_key(struct bregs *regs, int incr, int extended)
 {
     u16 buffer_head;
     u16 buffer_tail;
@@ -135,54 +134,45 @@ dequeue_key(u8 *scan_code, u8 *ascii_code, u8 incr)
 
         if (buffer_head != buffer_tail)
             break;
-        if (!incr)
-            return 0;
+        if (!incr) {
+            regs->flags |= F_ZF;
+            return;
+        }
         cpu_relax();
     }
 
-    *ascii_code = GET_FARVAR(SEG_BDA, *(u8*)(buffer_head+0x400+0));
-    *scan_code  = GET_FARVAR(SEG_BDA, *(u8*)(buffer_head+0x400+1));
-
-    if (incr) {
-        u16 buffer_start = GET_BDA(kbd_buf_start_offset);
-        u16 buffer_end   = GET_BDA(kbd_buf_end_offset);
+    u8 ascii_code = GET_FARVAR(SEG_BDA, *(u8*)(buffer_head+0));
+    u8 scan_code  = GET_FARVAR(SEG_BDA, *(u8*)(buffer_head+1));
+    if ((ascii_code == 0xF0 && scan_code != 0)
+        || (ascii_code == 0xE0 && !extended))
+        ascii_code = 0;
+    regs->ax = (scan_code << 8) | ascii_code;
 
-        buffer_head += 2;
-        if (buffer_head >= buffer_end)
-            buffer_head = buffer_start;
-        SET_BDA(kbd_buf_head, buffer_head);
+    if (!incr) {
+        regs->flags &= ~F_ZF;
+        return;
     }
-    return 1;
+    u16 buffer_start = GET_BDA(kbd_buf_start_offset);
+    u16 buffer_end   = GET_BDA(kbd_buf_end_offset);
+
+    buffer_head += 2;
+    if (buffer_head >= buffer_end)
+        buffer_head = buffer_start;
+    SET_BDA(kbd_buf_head, buffer_head);
 }
 
 // read keyboard input
 static void
 handle_1600(struct bregs *regs)
 {
-    u8 scan_code, ascii_code;
-    dequeue_key(&scan_code, &ascii_code, 1);
-    if (scan_code != 0 && ascii_code == 0xF0)
-        ascii_code = 0;
-    else if (ascii_code == 0xE0)
-        ascii_code = 0;
-    regs->ax = (scan_code << 8) | ascii_code;
+    dequeue_key(regs, 1, 0);
 }
 
 // check keyboard status
 static void
 handle_1601(struct bregs *regs)
 {
-    u8 scan_code, ascii_code;
-    if (!dequeue_key(&scan_code, &ascii_code, 0)) {
-        regs->flags |= F_ZF;
-        return;
-    }
-    if (scan_code != 0 && ascii_code == 0xF0)
-        ascii_code = 0;
-    else if (ascii_code == 0xE0)
-        ascii_code = 0;
-    regs->ax = (scan_code << 8) | ascii_code;
-    regs->flags &= ~F_ZF;
+    dequeue_key(regs, 0, 0);
 }
 
 // get shift flag status
@@ -233,26 +223,14 @@ handle_160a(struct bregs *regs)
 static void
 handle_1610(struct bregs *regs)
 {
-    u8 scan_code, ascii_code;
-    dequeue_key(&scan_code, &ascii_code, 1);
-    if (scan_code != 0 && ascii_code == 0xF0)
-        ascii_code = 0;
-    regs->ax = (scan_code << 8) | ascii_code;
+    dequeue_key(regs, 1, 1);
 }
 
 // check MF-II keyboard status
 static void
 handle_1611(struct bregs *regs)
 {
-    u8 scan_code, ascii_code;
-    if (!dequeue_key(&scan_code, &ascii_code, 0)) {
-        regs->flags |= F_ZF;
-        return;
-    }
-    if (scan_code != 0 && ascii_code == 0xF0)
-        ascii_code = 0;
-    regs->ax = (scan_code << 8) | ascii_code;
-    regs->flags &= ~F_ZF;
+    dequeue_key(regs, 0, 1);
 }
 
 // get extended keyboard status
@@ -287,6 +265,12 @@ handle_16a2(struct bregs *regs)
     // don't change AH : function int16 ah=0x20-0x22 NOT supported
 }
 
+static void
+handle_16XX(struct bregs *regs)
+{
+    debug_stub(regs);
+}
+
 static void
 set_leds()
 {
@@ -329,6 +313,7 @@ handle_16(struct bregs *regs)
     case 0x92: handle_1692(regs); break;
     case 0xa2: handle_16a2(regs); break;
     case 0x6f: handle_166f(regs); break;
+    default:   handle_16XX(regs); break;
     }
 }
 
@@ -341,7 +326,7 @@ static struct scaninfo {
     u16 control;
     u16 alt;
     u8 lock_flags;
-} scan_to_scanascii[MAX_SCAN_CODE + 1] = {
+} scan_to_scanascii[MAX_SCAN_CODE + 1] VAR16 = {
     {   none,   none,   none,   none, none },
     { 0x011b, 0x011b, 0x011b, 0x0100, none }, /* escape */
     { 0x0231, 0x0221,   none, 0x7800, none }, /* 1! */
@@ -429,11 +414,14 @@ static struct scaninfo {
     {   none,   none,   none,   none, none },
     {   none,   none,   none,   none, none },
     { 0x565c, 0x567c,   none,   none, none }, /* \| */
-    { 0x5700, 0x5700,   none,   none, none }, /* F11 */
-    { 0x5800, 0x5800,   none,   none, none }  /* F12 */
+    { 0x8500, 0x8700, 0x8900, 0x8b00, none }, /* F11 */
+    { 0x8600, 0x8800, 0x8a00, 0x8c00, none }, /* F12 */
 };
 
-static void
+// 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
+// have the overhead of this function's stack.
+static void noinline
 process_key(u8 scancode)
 {
     u8 shift_flags = GET_BDA(kbd_flag0);
@@ -560,9 +548,12 @@ process_key(u8 scancode)
         return;
 
     default:
-        if (scancode & 0x80) {
-            break; /* toss key releases ... */
-        }
+        if (scancode & 0x80)
+            // toss key releases
+            break;
+        if (scancode == 0x53 && (shift_flags & 0x0c) == 0x0c)
+            // Ctrl+alt+del - reset machine.
+            reset_vector();
         if (scancode > MAX_SCAN_CODE) {
             dprintf(1, "KBD: int09h_handler(): unknown scancode read: 0x%02x!\n"
                     , scancode);
@@ -571,35 +562,35 @@ process_key(u8 scancode)
         u8 asciicode;
         struct scaninfo *info = &scan_to_scanascii[scancode];
         if (shift_flags & 0x08) { /* ALT */
-            asciicode = GET_VAR(CS, info->alt);
-            scancode = GET_VAR(CS, info->alt) >> 8;
+            asciicode = GET_GLOBAL(info->alt);
+            scancode = GET_GLOBAL(info->alt) >> 8;
         } else if (shift_flags & 0x04) { /* CONTROL */
-            asciicode = GET_VAR(CS, info->control);
-            scancode = GET_VAR(CS, info->control) >> 8;
+            asciicode = GET_GLOBAL(info->control);
+            scancode = GET_GLOBAL(info->control) >> 8;
         } else if ((mf2_state & 0x02) > 0
                    && scancode >= 0x47 && scancode <= 0x53) {
             /* extended keys handling */
             asciicode = 0xe0;
-            scancode = GET_VAR(CS, info->normal) >> 8;
+            scancode = GET_GLOBAL(info->normal) >> 8;
         } else if (shift_flags & 0x03) { /* LSHIFT + RSHIFT */
             /* check if lock state should be ignored
              * because a SHIFT key are pressed */
 
-            if (shift_flags & GET_VAR(CS, info->lock_flags)) {
-                asciicode = GET_VAR(CS, info->normal);
-                scancode = GET_VAR(CS, info->normal) >> 8;
+            if (shift_flags & GET_GLOBAL(info->lock_flags)) {
+                asciicode = GET_GLOBAL(info->normal);
+                scancode = GET_GLOBAL(info->normal) >> 8;
             } else {
-                asciicode = GET_VAR(CS, info->shift);
-                scancode = GET_VAR(CS, info->shift) >> 8;
+                asciicode = GET_GLOBAL(info->shift);
+                scancode = GET_GLOBAL(info->shift) >> 8;
             }
         } else {
             /* check if lock is on */
-            if (shift_flags & GET_VAR(CS, info->lock_flags)) {
-                asciicode = GET_VAR(CS, info->shift);
-                scancode = GET_VAR(CS, info->shift) >> 8;
+            if (shift_flags & GET_GLOBAL(info->lock_flags)) {
+                asciicode = GET_GLOBAL(info->shift);
+                scancode = GET_GLOBAL(info->shift) >> 8;
             } else {
-                asciicode = GET_VAR(CS, info->normal);
-                scancode = GET_VAR(CS, info->normal) >> 8;
+                asciicode = GET_GLOBAL(info->normal);
+                scancode = GET_GLOBAL(info->normal) >> 8;
             }
         }
         if (scancode==0 && asciicode==0) {
@@ -626,29 +617,23 @@ handle_09()
 
     // read key from keyboard controller
     u8 v = inb(PORT_PS2_STATUS);
-    if ((v & 0x21) != 0x01) {
-        dprintf(1, "int09 but no keyboard data.\n");
+    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);
 
-    irq_enable();
     if (CONFIG_KBD_CALL_INT15_4F) {
         // allow for keyboard intercept
-        struct bregs tr;
-        memset(&tr, 0, sizeof(tr));
-        tr.al = key;
-        tr.ah = 0x4f;
-        tr.flags = F_CF;
-        call16_int(0x15, &tr);
-        if (!(tr.flags & F_CF))
+        u32 eax = (0x4f << 8) | key;
+        u32 flags;
+        call16_simpint(0x15, &eax, &flags);
+        if (!(flags & F_CF))
             goto done;
-        key = tr.al;
+        key = eax;
     }
     process_key(key);
 
-    irq_disable();
-
 done:
     eoi_pic1();
 }