feat(os): Airlys OS S1 production release with Linux Zen & KDE Plasma 6 profile
This commit is contained in:
1 parent
2b3e8f0bb6
commit
9bf424f8c2
27 files changed
+1030
-812
No files matched your search
+47
-27
@@ -15,15 +15,17 @@ static uint8_t s_mouse_cycle = 0;
|
||||
static uint8_t s_mouse_bytes[3];
|
||||
static bool s_left_button = false;
|
||||
static bool s_right_button = false;
|
||||
static bool s_mouse_present = false;
|
||||
|
||||
static inline void mouse_wait_input(void) {
|
||||
uint32_t timeout = 100000;
|
||||
uint32_t timeout = 50000;
|
||||
while ((inb(MOUSE_PORT_STATUS) & 2) && --timeout > 0) {}
|
||||
}
|
||||
|
||||
static inline void mouse_wait_output(void) {
|
||||
uint32_t timeout = 100000;
|
||||
static inline bool mouse_wait_output(void) {
|
||||
uint32_t timeout = 50000;
|
||||
while (!(inb(MOUSE_PORT_STATUS) & 1) && --timeout > 0) {}
|
||||
return timeout > 0;
|
||||
}
|
||||
|
||||
static void mouse_write(uint8_t write) {
|
||||
@@ -34,7 +36,7 @@ static void mouse_write(uint8_t write) {
|
||||
}
|
||||
|
||||
static uint8_t mouse_read(void) {
|
||||
mouse_wait_output();
|
||||
if (!mouse_wait_output()) return 0xFF;
|
||||
return inb(MOUSE_PORT_DATA);
|
||||
}
|
||||
|
||||
@@ -42,7 +44,7 @@ void mouse_move_relative(int32_t dx, int32_t dy) {
|
||||
s_mouse_x += dx;
|
||||
s_mouse_y += dy;
|
||||
|
||||
/* Clamp within display bounds */
|
||||
/* Clamp within physical display bounds */
|
||||
vbe_screen_t* scr = vbe_get_screen();
|
||||
if (scr && scr->width > 0 && scr->height > 0) {
|
||||
if (s_mouse_x < 0) s_mouse_x = 0;
|
||||
@@ -59,9 +61,12 @@ void mouse_set_buttons(bool left, bool right) {
|
||||
}
|
||||
|
||||
static void mouse_process_byte(uint8_t b) {
|
||||
/* Byte 0 alignment check: Bit 3 must always be 1 in standard PS/2 packet */
|
||||
if (s_mouse_cycle == 0 && !(b & 0x08)) {
|
||||
return;
|
||||
/* Strict PS/2 byte 0 validation to eliminate floating bus 0xFF/0x00 noise */
|
||||
if (s_mouse_cycle == 0) {
|
||||
/* Bit 3 MUST be 1; Bit 6 & 7 (overflow) MUST be 0; cannot be 0xFF or 0x00 */
|
||||
if ((b & 0x08) == 0 || (b & 0xC0) != 0 || b == 0xFF || b == 0x00) {
|
||||
return; /* Discard floating noise */
|
||||
}
|
||||
}
|
||||
|
||||
s_mouse_bytes[s_mouse_cycle++] = b;
|
||||
@@ -73,20 +78,23 @@ static void mouse_process_byte(uint8_t b) {
|
||||
int32_t dx = (int32_t)s_mouse_bytes[1];
|
||||
int32_t dy = (int32_t)s_mouse_bytes[2];
|
||||
|
||||
/* Sign extension for 9-bit deltas */
|
||||
/* Sanity checks on flags */
|
||||
if (flags == 0xFF || flags == 0x00) return;
|
||||
if ((flags & 0x08) == 0 || (flags & 0xC0) != 0) return;
|
||||
|
||||
/* Sign extension for 9-bit relative deltas */
|
||||
if (flags & 0x10) dx |= 0xFFFFFF00;
|
||||
if (flags & 0x20) dy |= 0xFFFFFF00;
|
||||
|
||||
/* Ignore overflow packets */
|
||||
if (flags & 0x40 || flags & 0x80) return;
|
||||
/* Ignore unrealistic jumps (noise suppression) */
|
||||
if (dx > 200 || dx < -200 || dy > 200 || dy < -200) return;
|
||||
|
||||
/* Invert dy because PS/2 Y is positive upwards, display Y is downwards */
|
||||
/* Move cursor (PS/2 dy is inverted) */
|
||||
mouse_move_relative(dx, -dy);
|
||||
|
||||
s_left_button = (flags & 0x01) != 0;
|
||||
s_right_button = (flags & 0x02) != 0;
|
||||
|
||||
/* Dispatch event to window compositor */
|
||||
compositor_handle_mouse(s_mouse_x, s_mouse_y, s_left_button, s_right_button);
|
||||
}
|
||||
}
|
||||
@@ -94,13 +102,12 @@ static void mouse_process_byte(uint8_t b) {
|
||||
static void mouse_callback(registers_t* regs) {
|
||||
(void)regs;
|
||||
uint8_t status = inb(MOUSE_PORT_STATUS);
|
||||
if (!(status & 0x01)) return; /* No data ready */
|
||||
if (!(status & 0x01)) return;
|
||||
|
||||
uint8_t b = inb(MOUSE_PORT_DATA);
|
||||
mouse_process_byte(b);
|
||||
}
|
||||
|
||||
/* Polling fallback for systems where IRQ 12 is intercepted by UEFI EC */
|
||||
void mouse_poll(void) {
|
||||
uint32_t guard = 16;
|
||||
while ((inb(MOUSE_PORT_STATUS) & 0x01) && guard-- > 0) {
|
||||
@@ -110,6 +117,19 @@ void mouse_poll(void) {
|
||||
}
|
||||
|
||||
void mouse_init(void) {
|
||||
/* Center cursor on 1080p display */
|
||||
vbe_screen_t* scr = vbe_get_screen();
|
||||
if (scr && scr->width > 0 && scr->height > 0) {
|
||||
s_mouse_x = (int32_t)scr->width / 2;
|
||||
s_mouse_y = (int32_t)scr->height / 2;
|
||||
} else {
|
||||
s_mouse_x = 960;
|
||||
s_mouse_y = 540;
|
||||
}
|
||||
s_mouse_cycle = 0;
|
||||
s_left_button = false;
|
||||
s_right_button = false;
|
||||
|
||||
/* Step 1: Enable auxiliary mouse device on PS/2 controller */
|
||||
mouse_wait_input();
|
||||
outb(MOUSE_PORT_STATUS, 0xA8);
|
||||
@@ -129,27 +149,27 @@ void mouse_init(void) {
|
||||
mouse_wait_input();
|
||||
outb(MOUSE_PORT_DATA, status);
|
||||
|
||||
/* Step 4: Set mouse to default sampling rate */
|
||||
mouse_write(0xF6);
|
||||
(void)mouse_read();
|
||||
/* Step 4: Reset & Self-test */
|
||||
mouse_write(0xFF);
|
||||
uint8_t ack = mouse_read();
|
||||
if (ack == 0xFA) {
|
||||
uint8_t test = mouse_read();
|
||||
if (test == 0xAA) {
|
||||
s_mouse_present = true;
|
||||
(void)mouse_read(); /* Drain device ID */
|
||||
}
|
||||
}
|
||||
|
||||
/* Step 5: Enable streaming / packet reporting */
|
||||
/* Step 5: Enable packet streaming */
|
||||
mouse_write(0xF4);
|
||||
(void)mouse_read();
|
||||
|
||||
/* Step 6: Register Vector 44 handler for IRQ 12 */
|
||||
register_interrupt_handler(44, mouse_callback);
|
||||
|
||||
/* Step 7: Unmask PIC IRQ 2 (Cascade) & IRQ 12 (Mouse) */
|
||||
/* Step 7: Unmask PIC IRQ 2 & IRQ 12 */
|
||||
pic_unmask_irq(2);
|
||||
pic_unmask_irq(12);
|
||||
|
||||
/* Center cursor */
|
||||
vbe_screen_t* scr = vbe_get_screen();
|
||||
if (scr && scr->width > 0 && scr->height > 0) {
|
||||
s_mouse_x = (int32_t)scr->width / 2;
|
||||
s_mouse_y = (int32_t)scr->height / 2;
|
||||
}
|
||||
}
|
||||
|
||||
int32_t mouse_get_x(void) {
|
||||
|
||||
+96
-4
@@ -73,12 +73,101 @@ static void pci_check_device(uint8_t bus, uint8_t slot) {
|
||||
}
|
||||
}
|
||||
|
||||
/* Enable hardware 5V VBUS power to all USB Root Hub ports (xHCI / EHCI) */
|
||||
void pci_enable_usb_port_power(void) {
|
||||
for (uint32_t i = 0; i < g_num_pci_devices; i++) {
|
||||
pci_device_t* dev = &g_pci_devices[i];
|
||||
if (dev->class_id != PCI_CLASS_SERIAL_BUS || dev->subclass_id != PCI_SUBCLASS_USB) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/* 1. Enable PCI Memory Space + Bus Master in Command Register (Offset 0x04) */
|
||||
uint16_t cmd = pci_read16(dev->bus, dev->slot, dev->func, 0x04);
|
||||
cmd |= (1 << 1) | (1 << 2); /* Memory Space Enable + Bus Master Enable */
|
||||
pci_write16(dev->bus, dev->slot, dev->func, 0x04, cmd);
|
||||
|
||||
/* 2. Retrieve 64-bit MMIO Base Address */
|
||||
uint32_t bar0_low = pci_read32(dev->bus, dev->slot, dev->func, 0x10);
|
||||
uint64_t bar0_phys = bar0_low & ~0xFU;
|
||||
if ((bar0_low & 0x06) == 0x04) {
|
||||
/* 64-bit BAR: read upper 32 bits from BAR1 */
|
||||
uint32_t bar0_hi = pci_read32(dev->bus, dev->slot, dev->func, 0x14);
|
||||
bar0_phys |= ((uint64_t)bar0_hi << 32);
|
||||
}
|
||||
|
||||
if (bar0_phys == 0 || bar0_phys == 0xFFFFFFF0) continue;
|
||||
|
||||
/* 3. Handle xHCI (Extensible Host Controller Interface - USB 3.x) */
|
||||
if (dev->prog_if == PCI_PROGIF_XHCI) {
|
||||
volatile uint8_t* xhci_base = (volatile uint8_t*)bar0_phys;
|
||||
uint8_t caplength = xhci_base[0];
|
||||
uint32_t hcsparams1 = *(volatile uint32_t*)(xhci_base + 0x04);
|
||||
uint32_t max_ports = (hcsparams1 >> 24) & 0xFF;
|
||||
if (max_ports == 0 || max_ports > 32) max_ports = 16;
|
||||
|
||||
volatile uint8_t* op_base = xhci_base + caplength;
|
||||
|
||||
/* Turn on Run/Stop (R/S) bit in USBCMD (Offset 0x00) */
|
||||
volatile uint32_t* usbcmd = (volatile uint32_t*)(op_base + 0x00);
|
||||
*usbcmd |= (1U << 0);
|
||||
|
||||
/* Turn on Port Power (PP = bit 9) on all root hub ports */
|
||||
volatile uint8_t* port_regs = op_base + 0x400;
|
||||
for (uint32_t p = 0; p < max_ports; p++) {
|
||||
volatile uint32_t* portsc = (volatile uint32_t*)(port_regs + (p * 0x10));
|
||||
uint32_t v = *portsc;
|
||||
/* Don't clear write-1-to-clear change bits */
|
||||
v &= ~((1U << 1) | (1U << 17) | (1U << 18) | (1U << 19) | (1U << 20) | (1U << 21) | (1U << 22));
|
||||
v |= (1U << 9); /* PP = 1: Turn ON 5V VBUS Power to USB port */
|
||||
*portsc = v;
|
||||
}
|
||||
|
||||
/* Take ownership from BIOS/SMI if USBLEGSUP is present */
|
||||
uint32_t hccparams1 = *(volatile uint32_t*)(xhci_base + 0x10);
|
||||
uint32_t xecp = (hccparams1 >> 16) & 0xFFFF;
|
||||
if (xecp > 0) {
|
||||
volatile uint8_t* ext_base = xhci_base + (xecp << 2);
|
||||
uint32_t legsup = *(volatile uint32_t*)ext_base;
|
||||
if ((legsup & 0xFF) == 1) { /* USB Legacy Support ID = 1 */
|
||||
*(volatile uint32_t*)ext_base |= (1U << 24); /* Claim OS Owned Semaphore */
|
||||
}
|
||||
}
|
||||
|
||||
printk(KERN_INFO "pci: xHCI USB 3.x Host Controller initialized (Bus %u, %u Ports Powered)\n",
|
||||
dev->bus, max_ports);
|
||||
}
|
||||
/* 4. Handle EHCI (Enhanced Host Controller Interface - USB 2.0) */
|
||||
else if (dev->prog_if == PCI_PROGIF_EHCI) {
|
||||
volatile uint8_t* ehci_base = (volatile uint8_t*)bar0_phys;
|
||||
uint8_t caplength = ehci_base[0];
|
||||
volatile uint8_t* op_base = ehci_base + caplength;
|
||||
|
||||
/* Route ports to EHCI via CONFIGFLAG (Offset 0x40) */
|
||||
volatile uint32_t* configflag = (volatile uint32_t*)(op_base + 0x40);
|
||||
*configflag = 1;
|
||||
|
||||
/* Turn on Port Power (PP = bit 12) */
|
||||
uint32_t hcsparams = *(volatile uint32_t*)(ehci_base + 0x04);
|
||||
uint32_t n_ports = hcsparams & 0x0F;
|
||||
if (n_ports == 0 || n_ports > 16) n_ports = 8;
|
||||
|
||||
for (uint32_t p = 0; p < n_ports; p++) {
|
||||
volatile uint32_t* portsc = (volatile uint32_t*)(op_base + 0x44 + (p * 4));
|
||||
*portsc |= (1U << 12); /* PP = 1 */
|
||||
}
|
||||
|
||||
printk(KERN_INFO "pci: EHCI USB 2.0 Host Controller initialized (Bus %u, %u Ports Powered)\n",
|
||||
dev->bus, n_ports);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void pci_init(void) {
|
||||
g_num_pci_devices = 0;
|
||||
kmemset(g_pci_devices, 0, sizeof(g_pci_devices));
|
||||
|
||||
/* Scan primary buses 0..4 */
|
||||
for (uint16_t bus = 0; bus < 4; bus++) {
|
||||
/* Scan primary buses 0..31 (encompassing AMD Zen 3+ Root Complex & Hubs) */
|
||||
for (uint16_t bus = 0; bus < 32; bus++) {
|
||||
for (uint8_t slot = 0; slot < 32; slot++) {
|
||||
pci_check_device((uint8_t)bus, slot);
|
||||
}
|
||||
@@ -89,7 +178,7 @@ void pci_init(void) {
|
||||
pci_device_t* net = &g_pci_devices[0];
|
||||
net->bus = 0; net->slot = 3; net->func = 0;
|
||||
net->vendor_id = PCI_VENDOR_INTEL;
|
||||
net->device_id = 0x100E; /* Intel 82540EM Gigabit Ethernet */
|
||||
net->device_id = 0x100E;
|
||||
net->class_id = PCI_CLASS_NETWORK;
|
||||
net->subclass_id = 0x00;
|
||||
net->bar0 = 0xF0000000;
|
||||
@@ -99,7 +188,7 @@ void pci_init(void) {
|
||||
pci_device_t* vga = &g_pci_devices[1];
|
||||
vga->bus = 0; vga->slot = 2; vga->func = 0;
|
||||
vga->vendor_id = PCI_VENDOR_QEMU;
|
||||
vga->device_id = 0x1111; /* QEMU Standard VGA */
|
||||
vga->device_id = 0x1111;
|
||||
vga->class_id = PCI_CLASS_DISPLAY;
|
||||
vga->bar0 = 0xFD000000;
|
||||
vga->valid = true;
|
||||
@@ -107,6 +196,9 @@ void pci_init(void) {
|
||||
g_num_pci_devices = 2;
|
||||
}
|
||||
|
||||
/* Turn on hardware power to all physical USB ports! */
|
||||
pci_enable_usb_port_power();
|
||||
|
||||
printk(KERN_INFO "pci: Probed PCI Bus controller (%u hardware devices enumerated)\n",
|
||||
g_num_pci_devices);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user