--- /dev/null
+ #ifndef X86_INPUT_PS2_H
+ #define X86_INPUT_PS2_H
+
+ /* port constants */
+ #define PS2_DATA_PORT 0x60
+ #define PS2_CMD_STS_PORT 0x64
+
+ /* command constants */
+ #define PS2_CMD_PORT_2_DISABLE 0xA7
+ #define PS2_CMD_PORT_1_DISABLE 0xAD
+ #define PS2_CMD_PORT_2_ENABLE 0xA8
+ #define PS2_CMD_PORT_1_ENABLE 0xAE
+ #define PS2_CMD_READ_CCB 0x20
+ #define PS2_CMD_WRITE_CCB 0x60
+ #define PS2_CMD_CONTROLLER_TEST 0xAA
+ #define PS2_CMD_PORT_1_TEST 0xAB
+ #define PS2_CMD_PORT_2_TEST 0xA9
+ #define PS2_CMD_PORT_2_WRITE 0xD4
+
+ #define PS2_DEV_RESET 0xFF
+
+ #define PS2_CONTROLLER_GOOD 0x55
+ #define PS2_CONTROLLER_BAD 0xFC
+
+ enum PS2_CCB {
+ PORT_1_IRQ = 0,
+ PORT_2_IRQ,
+ SYSTEM_FLAG,
+ ZERO,
+ PORT_1_CLK,
+ PORT_2_CLK,
+ PORT_1_TRANSL,
+ ZERO2
+ };
+
+ enum PS2_STATUS {
+ OUT_STATUS = 0,
+ IN_STATUS,
+ STS_SYSTEM_FLAG,
+ CMD_DATA,
+ UNKNOWN,
+ UNKNOWN2,
+ TIMEOUT_ERROR,
+ PARITY_ERROR
+ };
+
+ enum PS2_SPECIAL {
+ SELF_TEST_GOOD = 0xAA,
+ SELF_TEST_BAD = 0xFC,
+ SELF_TEST_BAD_2 = 0xFD,
+ ACK = 0xFA,
+ RESEND = 0xFE
+ };
+
+ static const char scancodes[] = {
+ /* letters */
+ [0x15] = 'q',
+ [0x1d] = 'w',
+ [0x24] = 'e',
+ [0x2d] = 'r',
+ [0x2c] = 't',
+ [0x35] = 'y',
+ [0x3c] = 'u',
+ [0x43] = 'i',
+ [0x44] = 'o',
+ [0x4d] = 'p',
+ [0x1c] = 'a',
+ [0x1b] = 's',
+ [0x23] = 'd',
+ [0x2b] = 'f',
+ [0x34] = 'g',
+ [0x33] = 'h',
+ [0x3b] = 'j',
+ [0x42] = 'k',
+ [0x4b] = 'l',
+ [0x1a] = 'z',
+ [0x22] = 'x',
+ [0x21] = 'c',
+ [0x2a] = 'v',
+ [0x32] = 'b',
+ [0x31] = 'n',
+ [0x3a] = 'm',
+
+ /* numbers */
+ [0x45] = '0',
+ [0x16] = '1',
+ [0x1e] = '2',
+ [0x26] = '3',
+ [0x25] = '4',
+ [0x2e] = '5',
+ [0x36] = '6',
+ [0x3d] = '7',
+ [0x3e] = '8',
+ [0x46] = '9',
+
+ /* special */
+ [0x29] = ' '
+ };
+
+ extern int ps2_initialize();
+ extern void ps2_input_wait();
+ extern void ps2_output_wait();
+
+ #endif
--- /dev/null
+#include <kprint.h>
+#include <io.h>
+#include <irq/idt.h>
+#include <irq/interrupt.h>
+#include <irq/i8259a.h>
++#include <input/ps2.h>
+#include <stdint.h>
+#include <mm/paging.h>
+#include <panic.h>
+
+struct idt_descriptor idt[256] __attribute__((aligned(0x10)));
+struct idtr idtr __attribute__((aligned(0x10)));
+extern void (*_int_handler_table[48])(void);
+
+struct e820_map {
+ uint64_t base;
+ uint64_t length;
+ uint32_t type;
+ uint32_t attrib;
+};
+
+void print_e820(struct e820_map *mmap, int mmap_size)
+{
+ kprint(" Base | Length | Type | Attrib |\n", VGA_LIGHT_GRAY, VGA_BLACK);
+ kprint("------------------------------------------------------------\n", VGA_LIGHT_GRAY, VGA_BLACK);
+ for (int i=0; i<mmap_size; i++) {
+ kprintq(mmap[i].base, 1);
+ kprintc('|', VGA_LIGHT_GRAY, VGA_BLACK);
+ kprintq(mmap[i].length, 1);
+ kprintc('|', VGA_LIGHT_GRAY, VGA_BLACK);
+ kprintd(mmap[i].type, 1);
+ kprintc('|', VGA_LIGHT_GRAY, VGA_BLACK);
+ kprintd(mmap[i].attrib, 1);
+ kprintc('|', VGA_LIGHT_GRAY, VGA_BLACK);
+ kprintc('\n', VGA_LIGHT_GRAY, VGA_BLACK);
+ }
+}
+
+/* Small handler for double faults. Will be put elsewhere eventually. */
+int double_fault_handler(struct interrupt_frame *frame)
+{
+ kprint("Double fault occurred!\n", VGA_BRIGHT_RED, VGA_BLACK);
+ kprint("EAX: ", VGA_LIGHT_GRAY, VGA_BLACK);
+ kprintd(frame->eax, 1);
+ kprintc(' ', VGA_LIGHT_GRAY, VGA_BLACK);
+ kprint("EBX: ", VGA_LIGHT_GRAY, VGA_BLACK);
+ kprintd(frame->ebx, 1);
+ kprintc(' ', VGA_LIGHT_GRAY, VGA_BLACK);
+ kprint("ECX: ", VGA_LIGHT_GRAY, VGA_BLACK);
+ kprintd(frame->ecx, 1);
+ kprintc(' ', VGA_LIGHT_GRAY, VGA_BLACK);
+ kprint("EDX: ", VGA_LIGHT_GRAY, VGA_BLACK);
+ kprintd(frame->edx, 1);
+ kprintc('\n', VGA_LIGHT_GRAY, VGA_BLACK);
+ kprint("EBP: ", VGA_LIGHT_GRAY, VGA_BLACK);
+ kprintd(frame->ebp, 1);
+ kprintc(' ', VGA_LIGHT_GRAY, VGA_BLACK);
+ kprint("ESP: ", VGA_LIGHT_GRAY, VGA_BLACK);
+ kprintd(frame->esp, 1);
+ kprintc('\n', VGA_LIGHT_GRAY, VGA_BLACK);
+ kprint("ESI: ", VGA_LIGHT_GRAY, VGA_BLACK);
+ kprintd(frame->esi, 1);
+ kprintc(' ', VGA_LIGHT_GRAY, VGA_BLACK);
+ kprint("EDI: ", VGA_LIGHT_GRAY, VGA_BLACK);
+ kprintd(frame->edi, 1);
+ kprintc('\n', VGA_LIGHT_GRAY, VGA_BLACK);
+ kprint("EIP: ", VGA_LIGHT_GRAY, VGA_BLACK);
+ kprintd(frame->eip, 1);
+ kprintc(' ', VGA_LIGHT_GRAY, VGA_BLACK);
+ kprint("EFLAGS: ", VGA_LIGHT_GRAY, VGA_BLACK);
+ kprintd(frame->eflags, 1);
+ kprintc('\n', VGA_LIGHT_GRAY, VGA_BLACK);
+ PANIC("double fault");
+}
+
+void kmain(struct e820_map *mmap, int mmap_size)
+{
+ vga_initialize();
+ kprint("Welcome to Nameless OS!\nRunning revision: ", VGA_LIGHT_GRAY, VGA_BLACK);
+ kprint(GIT_COMMIT, VGA_LIGHT_GRAY, VGA_BLACK);
+ kprint("\n", VGA_LIGHT_GRAY, VGA_BLACK);
+ kprint("BIOS memory map:\n", VGA_LIGHT_GRAY, VGA_BLACK);
+ print_e820(mmap, mmap_size);
+ kprint("Preparing IDT...\n", VGA_LIGHT_GRAY, VGA_BLACK);
+ for (int i=0; i<48; i++) {
+ idt_set_descriptor(idt, i, 0x8, _int_handler_table[i], IDT_INTERRUPT_GATE, 0);
+ }
+ kprint("IDT prepared, loading...\n", VGA_LIGHT_GRAY, VGA_BLACK);
+ populate_idtr(&idtr, idt);
+ load_idt(idtr);
+ kprint("Setting up interrupts...\n", VGA_LIGHT_GRAY, VGA_BLACK);
+ register_interrupt(8, &double_fault_handler);
+ pic_init(0x20, 0x28);
+ pic_mask_all();
+ asm volatile ("sti");
++ kprint("Initializing PS/2...\n", VGA_LIGHT_GRAY, VGA_BLACK);
++ int ret = ps2_initialize();
++ switch (ret) {
++ case -1:
++ kprint("No PS/2 devices found or something is faulty\n", VGA_LIGHT_GRAY, VGA_BLACK);
++ break;
++ case 0:
++ kprint("One PS/2 device found\n", VGA_LIGHT_GRAY, VGA_BLACK);
++ break;
++ case 1:
++ kprint("Two PS/2 devices found\n", VGA_LIGHT_GRAY, VGA_BLACK);
++ }
+ kprint("All done\n", VGA_LIGHT_GRAY, VGA_BLACK);
+ while(1);
+}