#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)
#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);
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
static void
dequeue_key(struct bregs *regs, int incr, int extended)
{
+ yield();
u16 buffer_head;
u16 buffer_tail;
for (;;) {
regs->flags |= F_ZF;
return;
}
- cpu_relax();
+ wait_irq();
}
u8 ascii_code = GET_FARVAR(SEG_BDA, *(u8*)(buffer_head+0));
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)
static void
handle_1602(struct bregs *regs)
{
+ yield();
regs->al = GET_BDA(kbd_flag0);
}
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)));
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);
if (! CONFIG_KEYBOARD)
return;
- irq_enable();
-
+ // XXX - set_leds should be called from irq handler
set_leds();
switch (regs->ah) {
};
// 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");
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;
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;
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;
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)
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);
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);
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
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);
}