Merge remote-tracking branch 'Kernel/master'

This commit is contained in:
EnderIce2
2024-11-20 05:00:33 +02:00
468 changed files with 112800 additions and 1 deletions

View File

@ -0,0 +1,94 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "ehci.hpp"
#include <interface/aip.h>
#include <display.hpp>
#include <convert.h>
#include <printf.h>
#include <kcon.hpp>
#include <debug.h>
#include <smp.hpp>
#include <cpu.hpp>
#include <io.h>
#if defined(a64)
#include "../../../arch/amd64/cpu/gdt.hpp"
#elif defined(a32)
#elif defined(aa64)
#endif
#include "../../../kernel.h"
using namespace KernelConsole;
using namespace PCI;
#define ERROR_COLOR "\x1b[31m"
#define WARN_COLOR "\x1b[33m"
#define DEFAULT_COLOR "\x1b[0m"
extern void ExPrint(const char *Format, ...);
extern void ArrowInput(uint8_t key);
extern void UserInput(char *Input);
extern FontRenderer CrashFontRenderer;
nsa void CrashEHCIKeyboardDriver::OnInterruptReceived(CPU::TrapFrame *Frame)
{
}
nsa bool CrashEHCIKeyboardDriver::Initialize()
{
return false;
}
nsa CrashEHCIKeyboardDriver::CrashEHCIKeyboardDriver(PCI::PCIDevice dev)
: Interrupts::Handler(dev)
{
Header = (PCIDeviceHeader *)dev.Header;
switch (Header->HeaderType)
{
case 128:
{
ExPrint(ERROR_COLOR "Unknown header type %d! Guessing PCI Header 0\n" DEFAULT_COLOR,
Header->HeaderType);
[[fallthrough]];
}
case 0: /* PCI Header 0 */
{
PCI::PCIHeader0 *hdr = (PCI::PCIHeader0 *)Header;
UNUSED(hdr);
break;
}
case 1: /* PCI Header 1 (PCI-to-PCI Bridge) */
{
ExPrint(ERROR_COLOR "PCI-to-PCI Bridge not supported\n" DEFAULT_COLOR);
break;
}
case 2: /* PCI Header 2 (PCI-to-CardBus Bridge) */
{
ExPrint(ERROR_COLOR "PCI-to-CardBus Bridge not supported\n" DEFAULT_COLOR);
break;
}
default:
{
ExPrint(ERROR_COLOR "Invalid PCI header type\n" DEFAULT_COLOR);
break;
}
}
}

View File

@ -0,0 +1,31 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <ints.hpp>
class CrashEHCIKeyboardDriver : public Interrupts::Handler
{
public:
PCI::PCIDeviceHeader *Header = nullptr;
void OnInterruptReceived(CPU::TrapFrame *Frame);
bool Initialize();
CrashEHCIKeyboardDriver(PCI::PCIDevice dev);
~CrashEHCIKeyboardDriver() = default;
};

View File

@ -0,0 +1,101 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "keyboard.hpp"
#include "ps2.hpp"
#include "uhci.hpp"
#include "ehci.hpp"
#include "xhci.hpp"
#include <interface/aip.h>
#include <display.hpp>
#include <convert.h>
#include <printf.h>
#include <kcon.hpp>
#include <debug.h>
#include <smp.hpp>
#include <cpu.hpp>
#include <io.h>
#if defined(a64)
#include "../../../arch/amd64/cpu/gdt.hpp"
#elif defined(a32)
#elif defined(aa64)
#endif
#include "../../../kernel.h"
using namespace KernelConsole;
using namespace PCI;
nsa bool DetectUSBKeyboard()
{
if (!PCIManager)
return false;
std::list<PCIDevice> uhci = PCIManager->FindPCIDevice(0xC, 0x3, 0x00); /* UHCI */
debug("There are %lu UHCI devices", uhci.size());
std::list<PCIDevice> ohci = PCIManager->FindPCIDevice(0xC, 0x3, 0x10); /* OHCI */
debug("There are %lu OHCI devices", ohci.size());
std::list<PCIDevice> ehci = PCIManager->FindPCIDevice(0xC, 0x3, 0x20); /* EHCI */
debug("There are %lu EHCI devices", uhci.size());
std::list<PCIDevice> xhci = PCIManager->FindPCIDevice(0xC, 0x3, 0x30); /* XHCI */
debug("There are %lu XHCI devices", xhci.size());
debug("Initializing UHCI devices");
for (const auto &dev : uhci)
{
CrashUHCIKeyboardDriver *usb = new CrashUHCIKeyboardDriver(dev);
if (!usb->Initialize())
{
error("Failed to initialize UHCI keyboard driver");
uhci.pop_front();
}
}
debug("Initializing EHCI devices");
for (const auto &dev : ehci)
{
CrashEHCIKeyboardDriver *usb = new CrashEHCIKeyboardDriver(dev);
if (!usb->Initialize())
{
error("Failed to initialize UHCI keyboard driver");
ehci.pop_front();
}
}
debug("Initializing XHCI devices");
for (const auto &dev : xhci)
{
CrashXHCIKeyboardDriver *usb = new CrashXHCIKeyboardDriver(dev);
if (!usb->Initialize())
{
error("Failed to initialize XHCI keyboard driver");
xhci.pop_front();
}
}
return xhci.size() > 0 || uhci.size() > 0 || ohci.size() > 0;
}
nsa void InitializeKeyboards()
{
if (DetectUSBKeyboard() == false)
new CrashPS2KeyboardDriver;
}

View File

@ -0,0 +1,206 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include <ints.hpp>
#pragma once
enum Keys
{
KEY_INVALID = 0x0,
KEY_D_ESCAPE = 0x1,
KEY_D_1 = 0x2,
KEY_D_2 = 0x3,
KEY_D_3 = 0x4,
KEY_D_4 = 0x5,
KEY_D_5 = 0x6,
KEY_D_6 = 0x7,
KEY_D_7 = 0x8,
KEY_D_8 = 0x9,
KEY_D_9 = 0xa,
KEY_D_0 = 0xb,
KEY_D_MINUS = 0xc,
KEY_D_EQUALS = 0xd,
KEY_D_BACKSPACE = 0xe,
KEY_D_TAB = 0xf,
KEY_D_Q = 0x10,
KEY_D_W = 0x11,
KEY_D_E = 0x12,
KEY_D_R = 0x13,
KEY_D_T = 0x14,
KEY_D_Y = 0x15,
KEY_D_U = 0x16,
KEY_D_I = 0x17,
KEY_D_O = 0x18,
KEY_D_P = 0x19,
KEY_D_LBRACKET = 0x1a,
KEY_D_RBRACKET = 0x1b,
KEY_D_RETURN = 0x1c,
KEY_D_LCTRL = 0x1d,
KEY_D_A = 0x1e,
KEY_D_S = 0x1f,
KEY_D_D = 0x20,
KEY_D_F = 0x21,
KEY_D_G = 0x22,
KEY_D_H = 0x23,
KEY_D_J = 0x24,
KEY_D_K = 0x25,
KEY_D_L = 0x26,
KEY_D_SEMICOLON = 0x27,
KEY_D_APOSTROPHE = 0x28,
KEY_D_GRAVE = 0x29,
KEY_D_LSHIFT = 0x2a,
KEY_D_BACKSLASH = 0x2b,
KEY_D_Z = 0x2c,
KEY_D_X = 0x2d,
KEY_D_C = 0x2e,
KEY_D_V = 0x2f,
KEY_D_B = 0x30,
KEY_D_N = 0x31,
KEY_D_M = 0x32,
KEY_D_COMMA = 0x33,
KEY_D_PERIOD = 0x34,
KEY_D_SLASH = 0x35,
KEY_D_RSHIFT = 0x36,
KEY_D_PRTSC = 0x37,
KEY_D_LALT = 0x38,
KEY_D_SPACE = 0x39,
KEY_D_CAPSLOCK = 0x3a,
KEY_D_NUMLOCK = 0x45,
KEY_D_SCROLLLOCK = 0x46,
KEY_D_KP_MULTIPLY = 0x37,
KEY_D_KP_7 = 0x47,
KEY_D_KP_8 = 0x48,
KEY_D_KP_9 = 0x49,
KEY_D_KP_MINUS = 0x4a,
KEY_D_KP_4 = 0x4b,
KEY_D_KP_5 = 0x4c,
KEY_D_KP_6 = 0x4d,
KEY_D_KP_PLUS = 0x4e,
KEY_D_KP_1 = 0x4f,
KEY_D_KP_2 = 0x50,
KEY_D_KP_3 = 0x51,
KEY_D_KP_0 = 0x52,
KEY_D_KP_PERIOD = 0x53,
KEY_D_F1 = 0x3b,
KEY_D_F2 = 0x3c,
KEY_D_F3 = 0x3d,
KEY_D_F4 = 0x3e,
KEY_D_F5 = 0x3f,
KEY_D_F6 = 0x40,
KEY_D_F7 = 0x41,
KEY_D_F8 = 0x42,
KEY_D_F9 = 0x43,
KEY_D_F10 = 0x44,
KEY_D_F11 = 0x57,
KEY_D_F12 = 0x58,
KEY_D_UP = 0x48,
KEY_D_LEFT = 0x4b,
KEY_D_RIGHT = 0x4d,
KEY_D_DOWN = 0x50,
KEY_U_ESCAPE = 0x81,
KEY_U_1 = 0x82,
KEY_U_2 = 0x83,
KEY_U_3 = 0x84,
KEY_U_4 = 0x85,
KEY_U_5 = 0x86,
KEY_U_6 = 0x87,
KEY_U_7 = 0x88,
KEY_U_8 = 0x89,
KEY_U_9 = 0x8a,
KEY_U_0 = 0x8b,
KEY_U_MINUS = 0x8c,
KEY_U_EQUALS = 0x8d,
KEY_U_BACKSPACE = 0x8e,
KEY_U_TAB = 0x8f,
KEY_U_Q = 0x90,
KEY_U_W = 0x91,
KEY_U_E = 0x92,
KEY_U_R = 0x93,
KEY_U_T = 0x94,
KEY_U_Y = 0x95,
KEY_U_U = 0x96,
KEY_U_I = 0x97,
KEY_U_O = 0x98,
KEY_U_P = 0x99,
KEY_U_LBRACKET = 0x9a,
KEY_U_RBRACKET = 0x9b,
KEY_U_RETURN = 0x9c,
KEY_U_LCTRL = 0x9d,
KEY_U_A = 0x9e,
KEY_U_S = 0x9f,
KEY_U_D = 0xa0,
KEY_U_F = 0xa1,
KEY_U_G = 0xa2,
KEY_U_H = 0xa3,
KEY_U_J = 0xa4,
KEY_U_K = 0xa5,
KEY_U_L = 0xa6,
KEY_U_SEMICOLON = 0xa7,
KEY_U_APOSTROPHE = 0xa8,
KEY_U_GRAVE = 0xa9,
KEY_U_LSHIFT = 0xaa,
KEY_U_BACKSLASH = 0xab,
KEY_U_Z = 0xac,
KEY_U_X = 0xad,
KEY_U_C = 0xae,
KEY_U_V = 0xaf,
KEY_U_B = 0xb0,
KEY_U_N = 0xb1,
KEY_U_M = 0xb2,
KEY_U_COMMA = 0xb3,
KEY_U_PERIOD = 0xb4,
KEY_U_SLASH = 0xb5,
KEY_U_RSHIFT = 0xb6,
KEY_U_KP_MULTIPLY = 0xb7,
KEY_U_LALT = 0xb8,
KEY_U_SPACE = 0xb9,
KEY_U_CAPSLOCK = 0xba,
KEY_U_F1 = 0xbb,
KEY_U_F2 = 0xbc,
KEY_U_F3 = 0xbd,
KEY_U_F4 = 0xbe,
KEY_U_F5 = 0xbf,
KEY_U_F6 = 0xc0,
KEY_U_F7 = 0xc1,
KEY_U_F8 = 0xc2,
KEY_U_F9 = 0xc3,
KEY_U_F10 = 0xc4,
KEY_U_NUMLOCK = 0xc5,
KEY_U_SCROLLLOCK = 0xc6,
KEY_U_KP_7 = 0xc7,
KEY_U_KP_8 = 0xc8,
KEY_U_KP_9 = 0xc9,
KEY_U_KP_MINUS = 0xca,
KEY_U_KP_4 = 0xcb,
KEY_U_KP_5 = 0xcc,
KEY_U_KP_6 = 0xcd,
KEY_U_KP_PLUS = 0xce,
KEY_U_KP_1 = 0xcf,
KEY_U_KP_2 = 0xd0,
KEY_U_KP_3 = 0xd1,
KEY_U_KP_0 = 0xd2,
KEY_U_KP_PERIOD = 0xd3,
KEY_U_F11 = 0xd7,
KEY_U_F12 = 0xd8,
};
void InitializeKeyboards();

View File

@ -0,0 +1,416 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "ps2.hpp"
#include "keyboard.hpp"
#include <interface/aip.h>
#include <display.hpp>
#include <convert.h>
#include <printf.h>
#include <kcon.hpp>
#include <debug.h>
#include <smp.hpp>
#include <cpu.hpp>
#include <io.h>
#if defined(a64)
#include "../../../arch/amd64/cpu/gdt.hpp"
#elif defined(a32)
#elif defined(aa64)
#endif
#include "../../../kernel.h"
using namespace KernelConsole;
#define ERROR_COLOR "\x1b[31m"
#define WARN_COLOR "\x1b[33m"
#define DEFAULT_COLOR "\x1b[0m"
extern void ExPrint(const char *Format, ...);
extern void ArrowInput(uint8_t key);
extern void UserInput(char *Input);
extern FontRenderer CrashFontRenderer;
const char sc_ascii_low[] = {'?', '?', '1', '2', '3', '4', '5', '6',
'7', '8', '9', '0', '-', '=', '?', '?', 'q', 'w', 'e', 'r', 't', 'y',
'u', 'i', 'o', 'p', '[', ']', '?', '?', 'a', 's', 'd', 'f', 'g',
'h', 'j', 'k', 'l', ';', '\'', '`', '?', '\\', 'z', 'x', 'c', 'v',
'b', 'n', 'm', ',', '.', '/', '?', '?', '?', ' '};
const char sc_ascii_high[] = {'?', '?', '!', '@', '#', '$', '%', '^',
'&', '*', '(', ')', '_', '+', '?', '?', 'Q', 'W', 'E', 'R', 'T', 'Y',
'U', 'I', 'O', 'P', '{', '}', '?', '?', 'A', 'S', 'D', 'F', 'G',
'H', 'J', 'K', 'L', ';', '\"', '~', '?', '|', 'Z', 'X', 'C', 'V',
'B', 'N', 'M', '<', '>', '?', '?', '?', '?', ' '};
static int LowerCase = true;
nsa static inline int GetLetterFromScanCode(uint8_t ScanCode)
{
if (ScanCode & 0x80)
{
switch (ScanCode)
{
case KEY_U_LSHIFT:
LowerCase = true;
return KEY_INVALID;
case KEY_U_RSHIFT:
LowerCase = true;
return KEY_INVALID;
default:
return KEY_INVALID;
}
}
else
{
switch (ScanCode)
{
case KEY_D_RETURN:
return '\n';
case KEY_D_LSHIFT:
LowerCase = false;
return KEY_INVALID;
case KEY_D_RSHIFT:
LowerCase = false;
return KEY_INVALID;
case KEY_D_BACKSPACE:
return ScanCode;
default:
{
if (ScanCode > 0x39)
break;
if (LowerCase)
return sc_ascii_low[ScanCode];
else
return sc_ascii_high[ScanCode];
}
}
}
return KEY_INVALID;
}
nsa void CrashPS2KeyboardDriver::PS2Wait(bool Output)
{
#if defined(a86)
TimeoutCallNumber++;
int timeout = 100000;
PS2_STATUSES status = {.Raw = inb(PS2_STATUS)};
while (timeout--)
{
if (!Output)
{
if (status.OutputBufferFull == 0)
return;
}
else
{
if (status.InputBufferFull == 0)
return;
}
status.Raw = inb(PS2_STATUS);
}
ExPrint(WARN_COLOR "PS/2 controller timeout (%s;%d)\n" DEFAULT_COLOR,
Output ? "output" : "input", TimeoutCallNumber);
#endif // a86
}
/*
This simple driver relies on the PS/2 controller to handle the keyboard.
Maybe is not the most efficient way but most x86 devices out there
still has PS/2 support (emulated or not).
We even have to make sure IRQ1 is enabled in the PIC but on x64 we use
the I/O APIC... "outb(0x21, 0b11111101);" can be used but the EOI is
automatically sent to I/O APIC if enabled/supported which is bad.
FIXME: On some real devices, the PS/2 keyboard doesn't send interrupts.
*/
nsa CrashPS2KeyboardDriver::CrashPS2KeyboardDriver() : Interrupts::Handler(1) /* IRQ1 */
{
#define WaitRead PS2Wait(true)
#define WaitWrite PS2Wait(false)
#define SetMessageLocation \
ExPrint("\x1b[%d;%dH", (Display->GetWidth / CrashFontRenderer.CurrentFont->GetInfo().Width) - 1, 0);
CPU::Interrupts(CPU::Disable);
/* Dots will be printed at the bottom of the screen as a progress bar. */
ExPrint("\x1b[%d;%dH", (Display->GetWidth / CrashFontRenderer.CurrentFont->GetInfo().Width) - 2, 0);
#if defined(a86)
/* Disable port 1 & 2 */
{
/* Disable Port 1 */
WaitWrite;
outb(PS2_CMD, PS2_CMD_DISABLE_PORT_1);
/* Disable Port 2 */
WaitWrite;
outb(PS2_CMD, PS2_CMD_DISABLE_PORT_2);
}
ExPrint(".");
/* Flush */
{
PS2_STATUSES status;
int timeout = 0x500;
while (timeout--)
{
status.Raw = inb(PS2_STATUS);
if (status.OutputBufferFull == 0)
break;
inb(PS2_DATA);
}
if (timeout <= 0)
{
SetMessageLocation;
ExPrint(ERROR_COLOR
"PS/2 controller timeout (flush;0)\n" DEFAULT_COLOR);
CPU::Stop();
}
}
ExPrint(".");
/* Test controller */
{
/* Save config */
WaitWrite;
outb(PS2_CMD, PS2_CMD_READ_CONFIG);
WaitRead;
PS2_CONFIGURATION cfg = {.Raw = inb(PS2_DATA)};
cfg.Port1Interrupt = 1;
cfg.Port2Interrupt = 1;
cfg.Port1Translation = 1;
/* Update config */
WaitWrite;
outb(PS2_CMD, PS2_DATA);
WaitWrite;
outb(PS2_DATA, cfg.Raw);
/* Test PS/2 controller */
WaitWrite;
outb(PS2_CMD, PS2_CMD_TEST_CONTROLLER);
WaitRead;
uint8_t test = inb(PS2_DATA);
if (test != PS2_TEST_PASSED)
{
if (test == PS2_ACK)
{
trace("PS/2 controller sent ACK to test request.");
WaitRead;
test = inb(PS2_DATA);
}
if (test != PS2_TEST_PASSED)
{
SetMessageLocation;
ExPrint(ERROR_COLOR
"PS/2 controller self test failed (%#x)\n" DEFAULT_COLOR,
test);
CPU::Stop();
}
}
/* Restore config */
WaitWrite;
outb(PS2_CMD, PS2_DATA);
WaitWrite;
outb(PS2_DATA, cfg.Raw);
}
ExPrint(".");
/* Disable scanning; Enable port 1; Set default settings */
{
/* Disable scanning */
outb(PS2_DATA, PS2_KBD_CMD_DISABLE_SCANNING);
/* Enable Port 1 */
WaitWrite;
outb(PS2_CMD, PS2_CMD_ENABLE_PORT_1);
/* Set default settings */
outb(PS2_DATA, PS2_KBD_CMD_DEFAULTS);
}
ExPrint(".");
/* Test port 1 */
{
WaitWrite;
outb(PS2_CMD, PS2_CMD_TEST_PORT_1);
WaitRead;
uint8_t test = inb(PS2_DATA);
if (test != 0x00)
{
if (test == PS2_KBD_RESP_ACK)
{
trace("PS/2 keyboard sent ACK to test request.");
WaitRead;
test = inb(PS2_DATA);
}
if (test != 0x00)
{
SetMessageLocation;
ExPrint(ERROR_COLOR
"PS/2 keyboard self test failed (%#x)\n" DEFAULT_COLOR,
test);
CPU::Stop();
}
}
}
ExPrint(".");
/* Configure the controller */
{
// /* Read Controller Configuration */
// WaitWrite;
// outb(PS2_CMD, PS2_CMD_READ_CONFIG);
// WaitRead;
// uint8_t cfg = inb(PS2_DATA);
// /* Enable Port 1 & Port 1 translation */
// cfg |= 0b01000001;
// /* Write Controller Configuration */
// WaitWrite;
// outb(PS2_CMD, PS2_CMD_WRITE_CONFIG);
// WaitWrite;
// outb(PS2_DATA, cfg);
}
ExPrint(".");
/* Enable port 1; Set scan code; Enable scanning */
{
/* Enable Port 1 */
outb(PS2_CMD, PS2_CMD_ENABLE_PORT_1);
/* Set scan code set 1 */
WaitWrite;
outb(PS2_DATA, PS2_KBD_CMD_SCAN_CODE_SET);
WaitWrite;
outb(PS2_DATA, PS2_KBD_SCAN_CODE_SET_2);
/* Check if we have scan code set 1 */
WaitWrite;
outb(PS2_DATA, PS2_KBD_CMD_SCAN_CODE_SET);
WaitWrite;
outb(PS2_DATA, PS2_KBD_SCAN_CODE_GET_CURRENT);
/* Read scan code set */
WaitRead;
uint8_t scs = inb(PS2_DATA);
if (scs == PS2_KBD_RESP_ACK || scs == PS2_KBD_RESP_RESEND)
{
if (scs == PS2_KBD_RESP_ACK)
trace("PS/2 keyboard sent ACK to scan code set request.");
if (scs == PS2_KBD_RESP_RESEND)
trace("PS/2 keyboard sent RESEND to scan code set request.");
WaitRead;
scs = inb(PS2_DATA);
}
if (scs != PS2_KBD_SC_SET_2)
{
SetMessageLocation;
ExPrint(WARN_COLOR
"PS/2 keyboard scan code set 1 not supported (%#x)\n" DEFAULT_COLOR,
scs);
}
/* Enable scanning */
outb(PS2_DATA, PS2_KBD_CMD_ENABLE_SCANNING);
}
#ifdef DEBUG
WaitWrite;
outb(PS2_CMD, PS2_CMD_READ_CONFIG);
WaitRead;
PS2_CONFIGURATION cfg = {.Raw = inb(PS2_DATA)};
debug("PS2 CONFIG:\nPort1int: %d\nPort2int: %d\nSysFlg: %d\nZ: %d\nP1clk: %d\nP2clk: %d\nP1trans: %d\nz: %d",
cfg.Port1Interrupt, cfg.Port2Interrupt, cfg.SystemFlag, cfg.Zero0, cfg.Port1Clock, cfg.Port2Clock, cfg.Port1Translation, cfg.Zero1);
#endif
ExPrint(".");
#endif // defined(a86)
CPU::Interrupts(CPU::Enable);
}
nsa void CrashPS2KeyboardDriver::OnInterruptReceived(CPU::TrapFrame *)
{
#if defined(a86)
uint8_t scanCode = inb(PS2_DATA);
if (scanCode == KEY_D_TAB ||
scanCode == KEY_D_LCTRL ||
scanCode == KEY_D_LALT ||
scanCode == KEY_U_LCTRL ||
scanCode == KEY_U_LALT)
return;
switch (scanCode)
{
case KEY_D_UP:
case KEY_D_LEFT:
case KEY_D_RIGHT:
case KEY_D_DOWN:
ArrowInput(scanCode);
break;
default:
break;
}
int key = GetLetterFromScanCode(scanCode);
if (key != KEY_INVALID)
{
if (key == KEY_D_BACKSPACE)
{
if (BackSpaceLimit > 0)
{
char keyBuf[5] = {'\b', '\x1b', '[', 'K', '\0'};
ExPrint(keyBuf);
backspace(UserInputBuffer);
BackSpaceLimit--;
}
}
else if (key == '\n')
{
UserInput(UserInputBuffer);
BackSpaceLimit = 0;
UserInputBuffer[0] = '\0';
}
else
{
append(UserInputBuffer, s_cst(char, key));
char keyBuf[2] = {(char)key, '\0'};
ExPrint(keyBuf);
BackSpaceLimit++;
}
Display->UpdateBuffer(); /* Update as we type. */
}
#endif // a64 || a32
}

View File

@ -0,0 +1,35 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <ints.hpp>
class CrashPS2KeyboardDriver : public Interrupts::Handler
{
private:
void PS2Wait(bool Read);
void OnInterruptReceived(CPU::TrapFrame *Frame);
int BackSpaceLimit = 0;
char UserInputBuffer[256];
int TimeoutCallNumber = 0;
public:
CrashPS2KeyboardDriver();
~CrashPS2KeyboardDriver() = default;
};

View File

@ -0,0 +1,178 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "uhci.hpp"
#include <interface/aip.h>
#include <display.hpp>
#include <convert.h>
#include <printf.h>
#include <kcon.hpp>
#include <debug.h>
#include <smp.hpp>
#include <cpu.hpp>
#include <io.h>
#if defined(a64)
#include "../../../arch/amd64/cpu/gdt.hpp"
#elif defined(a32)
#elif defined(aa64)
#endif
#include "../../../kernel.h"
using namespace KernelConsole;
using namespace PCI;
#define ERROR_COLOR "\x1b[31m"
#define WARN_COLOR "\x1b[33m"
#define DEFAULT_COLOR "\x1b[0m"
extern void ExPrint(const char *Format, ...);
extern void ArrowInput(uint8_t key);
extern void UserInput(char *Input);
extern FontRenderer CrashFontRenderer;
nsa void CrashUHCIKeyboardDriver::OnInterruptReceived(CPU::TrapFrame *Frame)
{
}
__no_sanitize("undefined") nsa bool CrashUHCIKeyboardDriver::Initialize()
{
return false; /* FIXME: stub */
debug("Allocating resources");
this->FrameList = (uint32_t *)(uintptr_t)KernelAllocator.RequestPages(TO_PAGES(1024 * sizeof(FrameList[0])));
this->td = (TD *)KernelAllocator.RequestPages(TO_PAGES(sizeof(TD) * 32));
memset(td, 0, sizeof(TD) * 32);
this->qh = (QH *)KernelAllocator.RequestPages(TO_PAGES(sizeof(QH) * 8));
memset(qh, 0, sizeof(QH) * 8);
/* FIXME: stub */
debug("Initializing controller");
outw((uint16_t)((uintptr_t)io + 0xC0), 0x8F00); /* Disable Legacy Mode Support */
/* Disable All Interrupts */
io->USBINTR.TOCRC = 0;
io->USBINTR.RIE = 0;
io->USBINTR.IOCE = 0;
io->USBINTR.SPIE = 0;
/* Configure Frame List */
io->FRNUM.FN = 0;
io->FRBASEADD.BA = (uint32_t)(uintptr_t)FrameList;
io->SOFMOD.SOFTVAL = 0b1000000;
io->USBSTS.raw = 0xFFFF; /* Clear USBSTS */
if (io->USBSTS.HCH)
io->USBCMD.RS = 1;
else
{
debug("Controller not halted, what to do?");
}
return true;
}
nsa CrashUHCIKeyboardDriver::CrashUHCIKeyboardDriver(PCI::PCIDevice dev)
: Interrupts::Handler(dev)
{
Header = (PCIDeviceHeader *)dev.Header;
switch (Header->HeaderType)
{
case 128:
{
ExPrint(ERROR_COLOR "Unknown header type %d! Guessing PCI Header 0\n" DEFAULT_COLOR,
Header->HeaderType);
[[fallthrough]];
}
case 0: /* PCI Header 0 */
{
PCI::PCIHeader0 *hdr = (PCI::PCIHeader0 *)Header;
uint32_t BAR[6];
size_t BARsSize[6];
BAR[0] = hdr->BAR0;
BAR[1] = hdr->BAR1;
BAR[2] = hdr->BAR2;
BAR[3] = hdr->BAR3;
BAR[4] = hdr->BAR4;
BAR[5] = hdr->BAR5;
/* BARs Size */
for (short i = 0; i < 6; i++)
{
if (BAR[i] == 0)
continue;
size_t size;
if ((BAR[i] & 1) == 0) /* Memory Base */
{
hdr->BAR0 = 0xFFFFFFFF;
size = hdr->BAR0;
hdr->BAR0 = BAR[i];
BARsSize[i] = size & (~15);
BARsSize[i] = ~BARsSize[i] + 1;
BARsSize[i] = BARsSize[i] & 0xFFFFFFFF;
debug("MEM BAR%d %#lx size: %d",
i, BAR[i], BARsSize[i]);
}
else if ((BAR[i] & 1) == 1) /* I/O Base */
{
hdr->BAR1 = 0xFFFFFFFF;
size = hdr->BAR1;
hdr->BAR1 = BAR[i];
BARsSize[i] = size & (~3);
BARsSize[i] = ~BARsSize[i] + 1;
BARsSize[i] = BARsSize[i] & 0xFFFF;
debug("I/O BAR%d %#lx size: %d",
i, BAR[i], BARsSize[i]);
}
}
uintptr_t baseAddress = BAR[4];
assert(baseAddress & 0x1); /* must be I/O */
debug("baseAddress: %#lx size %#lx", baseAddress, BARsSize[4]);
Memory::Virtual vmm;
vmm.Map((void *)baseAddress, (void *)baseAddress, BARsSize[4], Memory::RW);
io = (IORegisters *)baseAddress;
break;
}
case 1: /* PCI Header 1 (PCI-to-PCI Bridge) */
{
ExPrint(ERROR_COLOR "PCI-to-PCI Bridge not supported\n" DEFAULT_COLOR);
break;
}
case 2: /* PCI Header 2 (PCI-to-CardBus Bridge) */
{
ExPrint(ERROR_COLOR "PCI-to-CardBus Bridge not supported\n" DEFAULT_COLOR);
break;
}
default:
{
ExPrint(ERROR_COLOR "Invalid PCI header type\n" DEFAULT_COLOR);
break;
}
}
}

View File

@ -0,0 +1,215 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <ints.hpp>
class CrashUHCIKeyboardDriver : public Interrupts::Handler
{
public:
struct IORegisters
{
union
{
struct
{
uint16_t RS : 1;
uint16_t HCRESET : 1;
uint16_t GRESET : 1;
uint16_t EGSM : 1;
uint16_t FGR : 1;
uint16_t SWDBG : 1;
uint16_t CF : 1;
uint16_t MAXP : 1;
uint16_t __reserved0 : 8;
} __packed;
uint16_t raw;
} USBCMD;
union
{
struct
{
uint16_t USBINT : 1;
uint16_t USBERRINT : 1;
uint16_t RD : 1;
uint16_t HSE : 1;
uint16_t HCPE : 1;
uint16_t HCH : 1;
uint16_t __reserved0 : 10;
} __packed;
uint16_t raw;
} USBSTS;
union
{
struct
{
uint16_t TOCRC : 1;
uint16_t RIE : 1;
uint16_t IOCE : 1;
uint16_t SPIE : 1;
uint16_t __reserved0 : 12;
} __packed;
uint16_t raw;
} USBINTR;
union
{
struct
{
uint16_t FN : 9;
uint16_t __reserved0 : 7;
} __packed;
uint16_t raw;
} FRNUM;
union
{
struct
{
uint32_t __reserved0 : 12;
uint32_t BA : 20;
} __packed;
uint32_t raw;
} FRBASEADD;
union
{
struct
{
uint8_t SOFTVAL : 7;
uint8_t __reserved0 : 1;
} __packed;
uint8_t raw;
} SOFMOD;
union
{
struct
{
uint16_t CCS : 1;
uint16_t CSC : 1;
uint16_t PE : 1;
uint16_t PEC : 1;
uint16_t LS : 1;
uint16_t RD : 1;
uint16_t __reserved0 : 1; // always 1
uint16_t LSDA : 1;
uint16_t PR : 1;
uint16_t __reserved1 : 2;
uint16_t SUS : 1;
uint16_t __reserved2 : 4;
} __packed;
uint16_t raw;
} PORTSC[2];
} __packed;
struct TD
{
union
{
struct
{
uint32_t T : 1;
uint32_t Q : 1;
uint32_t Vf : 1;
uint32_t __reserved0 : 1;
uint32_t LP : 28;
} __packed;
uint32_t raw;
} LINK;
union
{
struct
{
uint32_t ActLen : 11;
uint32_t __reserved0 : 5;
uint32_t STATUS : 8;
uint32_t IOC : 1;
uint32_t IOS : 1;
uint32_t LS : 1;
uint32_t unknown_0 : 2; /* missing in the spec @ 3.2.2 */
uint32_t SPD : 1;
uint32_t __reserved1 : 2;
} __packed;
uint32_t raw;
} CS;
union
{
struct
{
uint32_t PID : 8;
uint32_t DeviceAddress : 6;
uint32_t EndPt : 4;
uint32_t D : 1;
uint32_t __reserved0 : 1;
uint32_t MaxLen : 11;
} __packed;
uint32_t raw;
} TOKEN;
union
{
struct
{
uint32_t Addr : 32;
} __packed;
uint32_t raw;
} BUFFER;
/* The last 4 DWords of the
Transfer Descriptor are
reserved for use by software.
- UHCI Design Guide 1.1 @ 3.2.5
*/
uint32_t __padding[4];
} *td __aligned(16) __packed;
struct QH
{
union
{
struct
{
uint32_t T : 1;
uint32_t Q : 1;
uint32_t __reserved0 : 2; /* must be 0 */
uint32_t QHLP : 28;
} __packed;
uint32_t raw;
} HEAD;
union
{
struct
{
uint32_t T : 1;
uint32_t Q : 1;
uint32_t __reserved0 : 1;
uint32_t __reserved1 : 1; /* must be 0 */
uint32_t QELP : 28;
} __packed;
uint32_t raw;
} ELEMENT;
/* FIXME: It is the same with TD? */
} *qh __aligned(16) __packed;
PCI::PCIDeviceHeader *Header = nullptr;
IORegisters *io = nullptr;
uint32_t *FrameList = nullptr;
void OnInterruptReceived(CPU::TrapFrame *Frame);
bool Initialize();
CrashUHCIKeyboardDriver(PCI::PCIDevice dev);
~CrashUHCIKeyboardDriver() = default;
};

View File

@ -0,0 +1,204 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "xhci.hpp"
#include <interface/aip.h>
#include <display.hpp>
#include <convert.h>
#include <printf.h>
#include <kcon.hpp>
#include <debug.h>
#include <smp.hpp>
#include <cpu.hpp>
#include <io.h>
#if defined(a64)
#include "../../../arch/amd64/cpu/gdt.hpp"
#elif defined(a32)
#elif defined(aa64)
#endif
#include "../../../kernel.h"
using namespace KernelConsole;
using namespace PCI;
#define ERROR_COLOR "\x1b[31m"
#define WARN_COLOR "\x1b[33m"
#define DEFAULT_COLOR "\x1b[0m"
extern void ExPrint(const char *Format, ...);
extern void ArrowInput(uint8_t key);
extern void UserInput(char *Input);
extern FontRenderer CrashFontRenderer;
nsa bool CrashXHCIKeyboardDriver::TakeOwnership()
{
HCExtCap *exCap = (HCExtCap *)(uintptr_t)this->ExtendedCaps;
if (exCap->USBLEGSUP.CapID != 1)
return true;
if (exCap->USBLEGSUP.BIOSOwnsHC == 0)
return true;
exCap->USBLEGSUP.OSOwnsHC = 1;
TimeManager->Sleep(200, Time::Milliseconds);
if (exCap->USBLEGSUP.BIOSOwnsHC == 0)
return true;
ExPrint(ERROR_COLOR "BIOS owns the USB controller\n" DEFAULT_COLOR);
return false;
}
nsa bool CrashXHCIKeyboardDriver::Initialize()
{
int timeout = 10;
if (!TakeOwnership())
{
ExPrint(ERROR_COLOR "Failed to take ownership\n" DEFAULT_COLOR);
return false;
}
stub;
return false; /* FIXME: stub */
}
nsa CrashXHCIKeyboardDriver::CrashXHCIKeyboardDriver(PCIDevice xhci)
: Interrupts::Handler(xhci)
{
Header = (PCIDeviceHeader *)xhci.Header;
switch (Header->HeaderType)
{
case 128:
{
ExPrint(ERROR_COLOR "Unknown header type %d! Guessing PCI Header 0\n" DEFAULT_COLOR,
Header->HeaderType);
[[fallthrough]];
}
case 0: /* PCI Header 0 */
{
PCI::PCIHeader0 *hdr = (PCI::PCIHeader0 *)Header;
uint32_t BAR[6];
size_t BARsSize[6];
BAR[0] = hdr->BAR0;
BAR[1] = hdr->BAR1;
BAR[2] = hdr->BAR2;
BAR[3] = hdr->BAR3;
BAR[4] = hdr->BAR4;
BAR[5] = hdr->BAR5;
/* BARs Size */
for (short i = 0; i < 6; i++)
{
if (BAR[i] == 0)
continue;
size_t size;
if ((BAR[i] & 1) == 0) /* Memory Base */
{
hdr->BAR0 = 0xFFFFFFFF;
size = hdr->BAR0;
hdr->BAR0 = BAR[i];
BARsSize[i] = size & (~15);
BARsSize[i] = ~BARsSize[i] + 1;
BARsSize[i] = BARsSize[i] & 0xFFFFFFFF;
debug("MEM BAR%d %#lx size: %d",
i, BAR[i], BARsSize[i]);
}
else if ((BAR[i] & 1) == 1) /* I/O Base */
{
hdr->BAR1 = 0xFFFFFFFF;
size = hdr->BAR1;
hdr->BAR1 = BAR[i];
BARsSize[i] = size & (~3);
BARsSize[i] = ~BARsSize[i] + 1;
BARsSize[i] = BARsSize[i] & 0xFFFF;
debug("IO BAR%d %#lx size: %d",
i, BAR[i], BARsSize[i]);
}
}
debug("IO %d 64-BIT %d", BAR[0] & 0x1, BAR[0] & 0x4);
uintptr_t baseAddress = BAR[0];
if (BAR[0] & 0x4)
baseAddress |= (uintptr_t)BAR[1] << 32;
if (baseAddress & 0x1)
baseAddress &= 0xFFFFFFFFFFFFFFFC;
else
baseAddress &= 0xFFFFFFFFFFFFFFF0;
debug("baseAddress: %#lx", baseAddress);
Memory::Virtual vmm;
vmm.Map((void *)baseAddress, (void *)baseAddress, BARsSize[0], Memory::RW);
caps = (XHCIcap *)baseAddress;
ops = (XHCIop *)(baseAddress + caps->CAPLENGTH);
port = (XHCIport *)(baseAddress + caps->CAPLENGTH + 0x400);
runtime = (XHCIruntime *)(baseAddress + (caps->RTSOFF & ~0x1F));
doorbell = (XHCIdoorbell *)(baseAddress + (caps->DBOFF & ~0x3));
uint16_t exCapOffset = caps->HCCPARAMS1.xHCIExtendedCapacitiesPointer << 2;
ExtendedCaps = (uintptr_t)caps + exCapOffset;
debug("ExtendedCaps: %#lx (%#lx + %#lx)", ExtendedCaps, caps, exCapOffset);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
Interrupter = &runtime->Interrupter[0];
#pragma GCC diagnostic pop
break;
}
case 1: /* PCI Header 1 (PCI-to-PCI Bridge) */
{
ExPrint(ERROR_COLOR "PCI-to-PCI Bridge not supported\n" DEFAULT_COLOR);
break;
}
case 2: /* PCI Header 2 (PCI-to-CardBus Bridge) */
{
ExPrint(ERROR_COLOR "PCI-to-CardBus Bridge not supported\n" DEFAULT_COLOR);
break;
}
default:
{
ExPrint(ERROR_COLOR "Invalid PCI header type\n" DEFAULT_COLOR);
break;
}
}
}
nsa void CrashXHCIKeyboardDriver::OnInterruptReceived(CPU::TrapFrame *Frame)
{
debug("Interrupt received");
Interrupter->IMAN.IP = 1;
if (!ops->USBSTS.EINT)
debug("!USBSTS.EINT");
// return;
ops->USBSTS.EINT = 1;
stub;
Interrupter->IMAN.IP = 0;
ops->USBSTS.EINT = 0;
}

View File

@ -0,0 +1,257 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <ints.hpp>
class CrashXHCIKeyboardDriver : public Interrupts::Handler
{
private:
struct XHCIcap
{
uint8_t CAPLENGTH;
uint8_t __reserved0;
uint16_t HCIVERSION;
uint32_t HCSPARAMS1;
uint32_t HCSPARAMS2;
uint32_t HCSPARAMS3;
union
{
struct
{
uint32_t AC64 : 1;
uint32_t BNC : 1;
uint32_t CSZ : 1;
uint32_t PPC : 1;
uint32_t PIND : 1;
uint32_t LHRC : 1;
uint32_t LTC : 1;
uint32_t NSS : 1;
uint32_t PAE : 1;
uint32_t SPC : 1;
uint32_t SEC : 1;
uint32_t CFC : 1;
uint32_t MaxPSASize : 4;
uint32_t xHCIExtendedCapacitiesPointer : 16;
} __packed;
uint32_t raw;
} HCCPARAMS1;
uint32_t DBOFF;
uint32_t RTSOFF;
uint32_t HCCPARAMS2;
} *caps __packed;
struct XHCIop
{
union
{
struct
{
uint32_t RS : 1;
uint32_t HCRST : 1;
uint32_t INTE : 1;
uint32_t HSEE : 1;
uint32_t __reserved0 : 3;
uint32_t LHCRST : 1;
uint32_t CSS : 1;
uint32_t CRS : 1;
uint32_t EWE : 1;
uint32_t EU3S : 1;
uint32_t __reserved1 : 1;
uint32_t CME : 1;
uint32_t ETE : 1;
uint32_t TSCEN : 1;
uint32_t VTIOEN : 1;
uint32_t __reserved2 : 15;
} __packed;
uint32_t raw;
} USBCMD;
union
{
struct
{
uint32_t HCH : 1;
uint32_t __reserved0 : 1;
uint32_t HSE : 1;
uint32_t EINT : 1;
uint32_t PCB : 1;
uint32_t __reserved1 : 3;
uint32_t SSS : 1;
uint32_t RSS : 1;
uint32_t SRE : 1;
uint32_t CNR : 1;
uint32_t HCE : 1;
uint32_t __reserved2 : 18;
} __packed;
uint32_t raw;
} USBSTS;
uint32_t PAGESIZE;
uint8_t __reserved0[8];
uint32_t DNCTRL;
union
{
struct
{
uint64_t RCS : 1;
uint64_t CS : 1;
uint64_t CA : 1;
uint64_t CRR : 1;
uint64_t __reserved0 : 2;
uint64_t CRP : 58;
} __packed;
uint64_t raw;
} CRCR;
uint8_t __reserved1[16];
uint64_t DCBAAP;
uint32_t CONFIG;
} *ops __packed;
struct XHCIport
{
uint32_t PORTSC;
uint32_t PORTMSC;
uint32_t PORTLI;
uint32_t PORTHLPMC;
} *port __packed;
struct XHCIruntime
{
uint32_t MFINDEX;
uint32_t __reserved0[7];
struct XHCIinterrupter
{
union
{
struct
{
uint32_t IP : 1;
uint32_t IE : 1;
uint32_t __reserved0 : 30;
} __packed;
uint32_t raw;
} IMAN;
union
{
struct
{
uint32_t IMODI : 16;
uint32_t IMODC : 16;
} __packed;
uint32_t raw;
} IMOD;
union
{
struct
{
uint32_t ERSTSZ : 16;
uint32_t __reserved0 : 16;
} __packed;
uint32_t raw;
} ERSTSZ;
uint32_t __reserved;
union
{
struct
{
uint64_t __reserved0 : 6;
uint64_t ERSTBAR : 58;
} __packed;
uint64_t raw;
} ERSTBA;
union
{
struct
{
uint64_t DESI : 3;
uint64_t EHB : 1;
uint64_t ERDP : 60;
} __packed;
uint64_t raw;
} ERDP;
} Interrupter[] __packed;
} *runtime __packed;
union XHCIdoorbell
{
struct
{
uint32_t DBTarget : 8;
uint32_t __reserved : 8;
uint32_t DBTaskID : 16;
} __packed;
uint32_t raw;
} *doorbell;
struct HCExtCap
{
union
{
struct
{
uint32_t CapID : 8;
uint32_t NextCapPtr : 8;
uint32_t BIOSOwnsHC : 1;
uint32_t __reserved0 : 7;
uint32_t OSOwnsHC : 1;
uint32_t __reserved1 : 7;
} __packed;
uint32_t raw;
} USBLEGSUP;
union
{
struct
{
uint32_t unknown : 32;
} __packed;
uint32_t raw;
} USBLEGCTLSTS;
} __packed;
struct XHCIprotocol
{
uint8_t CAPID;
uint8_t NextCapPtr;
uint8_t RevisionMinor;
uint8_t RevisionMajor;
uint8_t Name[4];
uint8_t CompPortOffset;
uint8_t CompPortCount;
uint16_t ProtocolDefined : 12;
uint8_t PSIC : 4;
uint8_t ProtocolSlotType : 4;
uint32_t __reserved0 : 28;
} __packed;
PCI::PCIDeviceHeader *Header = nullptr;
uintptr_t ExtendedCaps = 0;
void *baseAddrArray = nullptr;
std::vector<XHCIprotocol *> Protocols = {};
XHCIruntime::XHCIinterrupter *Interrupter = nullptr;
void OnInterruptReceived(CPU::TrapFrame *Frame);
bool TakeOwnership();
public:
bool Initialize();
CrashXHCIKeyboardDriver(PCI::PCIDevice dev);
~CrashXHCIKeyboardDriver() {}
};