diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 97006d4..0a02f5a 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -88,8 +88,8 @@ outb(0xA0, 0x11); outb(0xA1, 32); outb(0x21, 40); - outb(0xA1, 0x04); - outb(0x21, 0x02); + outb(0xA1, 0x02); + outb(0x21, 0x04); outb(0x21, 0x1); outb(0xA1, 0x1); outb(0x21, 0xFF); diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 97006d4..0a02f5a 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -88,8 +88,8 @@ outb(0xA0, 0x11); outb(0xA1, 32); outb(0x21, 40); - outb(0xA1, 0x04); - outb(0x21, 0x02); + outb(0xA1, 0x02); + outb(0x21, 0x04); outb(0x21, 0x1); outb(0xA1, 0x1); outb(0x21, 0xFF); diff --git a/src/userland/pic/main.c b/src/userland/pic/main.c index 73309f2..a9b2dc5 100644 --- a/src/userland/pic/main.c +++ b/src/userland/pic/main.c @@ -24,8 +24,10 @@ #define PIC_READ_ISR 0x0B uint16_t getISR() { ioOut(PIC1, PIC_READ_ISR, 1); + uint16_t first = ioIn(PIC1, 1); ioOut(PIC2, PIC_READ_ISR, 1); - return (ioIn(PIC2, 1) << 8) | ioIn(PIC1, 1); + uint16_t second = ioIn(PIC2, 1) << 8; + return first | second; } void irqMaster(uint32_t intNo) { diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 97006d4..0a02f5a 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -88,8 +88,8 @@ outb(0xA0, 0x11); outb(0xA1, 32); outb(0x21, 40); - outb(0xA1, 0x04); - outb(0x21, 0x02); + outb(0xA1, 0x02); + outb(0x21, 0x04); outb(0x21, 0x1); outb(0xA1, 0x1); outb(0x21, 0xFF); diff --git a/src/userland/pic/main.c b/src/userland/pic/main.c index 73309f2..a9b2dc5 100644 --- a/src/userland/pic/main.c +++ b/src/userland/pic/main.c @@ -24,8 +24,10 @@ #define PIC_READ_ISR 0x0B uint16_t getISR() { ioOut(PIC1, PIC_READ_ISR, 1); + uint16_t first = ioIn(PIC1, 1); ioOut(PIC2, PIC_READ_ISR, 1); - return (ioIn(PIC2, 1) << 8) | ioIn(PIC1, 1); + uint16_t second = ioIn(PIC2, 1) << 8; + return first | second; } void irqMaster(uint32_t intNo) { diff --git a/src/userland/usb/main.c b/src/userland/usb/main.c index b9e6d76..64dd69f 100644 --- a/src/userland/usb/main.c +++ b/src/userland/usb/main.c @@ -3,8 +3,8 @@ #include "usb.h" -void xhci_command(XHCIController *controller, uint32_t p1, uint32_t p2, - uint32_t status, uint32_t control) { +void xhciCommand(XHCIController *controller, uint32_t p1, uint32_t p2, + uint32_t status, uint32_t control) { control &= ~1; control |= controller->commands.cycle; @@ -57,8 +57,8 @@ } XHCITRB *trbRingFetch(TrbRing *ring) { - while ((ring->trbs[ring->dequeue].control & 1) != ring->cycle) { - syscall(-1, 0, 0, 0, 0); + if ((ring->trbs[ring->dequeue].control & 1) != ring->cycle) { + return NULL; } XHCITRB *result = &ring->trbs[ring->dequeue]; ring->dequeue++; @@ -86,8 +86,6 @@ void setupRuntime(XHCIController *controller) { controller->runtime = OFFSET(controller->capabilities, controller->capabilities->runtimeOffset); - controller->runtime->interrupters[0].enabled = true; - controller->runtime->interrupters[0].moderationCounter = 500; controller->runtime->interrupters[0].eventRingSegmentTableSize = 1; controller->runtime->interrupters[0].eventRingSegmentTableAddress[0] = U32(controller->eventRingSegmentTablePhysical); @@ -96,6 +94,9 @@ U32(&controller->events.physical[controller->events.dequeue]) | (1 << 3); controller->runtime->interrupters[0].eventRingDequeuePointer[1] = 0; + + controller->runtime->interrupters[0].management |= 3; + sleep(100); } void setupEventRingSegmentTable(XHCIController *controller) { @@ -192,33 +193,15 @@ slotInfo >> 24); return controller; } +XHCIController *controller; -static void initializeUSB(uint32_t deviceId) { - XHCIController *controller = initializeController(deviceId); - restartXHCIController(controller); - - setupTrbRing(&controller->commands, 256); - setupTrbRing(&controller->events, 256); - readExtendedCapabilities(controller); - setupOperationalRegisters(controller); - setupEventRingSegmentTable(controller); - setupRuntime(controller); - setupScratchpadBuffers(controller); - - controller->operational->command |= (1 << 0) | (1 << 2); - sleep(100); - - if (controller->operational->status & (1 << 2)) - return printf("critical XHCI problem\n"); - - // just testing for now... - xhci_command(controller, 0, 0, 0, (9 << 10)); - xhci_command(controller, 0, 0, 0, (9 << 10)); - xhci_command(controller, 0, 0, 0, (9 << 10)); - xhci_command(controller, 0, 0, 0, (9 << 10)); - - while (1) { - XHCITRB *trb = trbRingFetch(&controller->events); +void xhciInterrupt() { + if (!(controller->runtime->interrupters[0].management & 1)) { + printf("no interrupt pending\n"); + return; + } + XHCITRB *trb; + while ((trb = trbRingFetch(&controller->events))) { uint32_t type = (trb->control >> 10) & 0x3F; printf("event %i [%x %x %x %x] type: %i, slotId: %i\n", @@ -231,6 +214,35 @@ } } +void initializeUSB(uint32_t deviceId) { + controller = initializeController(deviceId); + restartXHCIController(controller); + + setupTrbRing(&controller->commands, 256); + setupTrbRing(&controller->events, 256); + readExtendedCapabilities(controller); + setupOperationalRegisters(controller); + setupEventRingSegmentTable(controller); + setupRuntime(controller); + setupScratchpadBuffers(controller); + + controller->operational->status |= (1 << 3); + controller->operational->command |= (1 << 0) | (1 << 2); + printf("using irq no. %i\n", getPCIInterrupt(deviceId, 0)); + int pic = getService("pic"); + subscribeEvent(pic, getEvent(pic, "irq11"), xhciInterrupt); + sleep(100); + + if (controller->operational->status & (1 << 2)) + return printf("critical XHCI problem\n"); + + // just testing for now... + xhciCommand(controller, 0, 0, 0, (9 << 10)); + xhciCommand(controller, 0, 0, 0, (9 << 10)); + xhciCommand(controller, 0, 0, 0, (9 << 10)); + xhciCommand(controller, 0, 0, 0, (9 << 10)); +} + int32_t main() { uint32_t i = 0, class = 0; while ((class = getDeviceClass(i, 0))) { diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 97006d4..0a02f5a 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -88,8 +88,8 @@ outb(0xA0, 0x11); outb(0xA1, 32); outb(0x21, 40); - outb(0xA1, 0x04); - outb(0x21, 0x02); + outb(0xA1, 0x02); + outb(0x21, 0x04); outb(0x21, 0x1); outb(0xA1, 0x1); outb(0x21, 0xFF); diff --git a/src/userland/pic/main.c b/src/userland/pic/main.c index 73309f2..a9b2dc5 100644 --- a/src/userland/pic/main.c +++ b/src/userland/pic/main.c @@ -24,8 +24,10 @@ #define PIC_READ_ISR 0x0B uint16_t getISR() { ioOut(PIC1, PIC_READ_ISR, 1); + uint16_t first = ioIn(PIC1, 1); ioOut(PIC2, PIC_READ_ISR, 1); - return (ioIn(PIC2, 1) << 8) | ioIn(PIC1, 1); + uint16_t second = ioIn(PIC2, 1) << 8; + return first | second; } void irqMaster(uint32_t intNo) { diff --git a/src/userland/usb/main.c b/src/userland/usb/main.c index b9e6d76..64dd69f 100644 --- a/src/userland/usb/main.c +++ b/src/userland/usb/main.c @@ -3,8 +3,8 @@ #include "usb.h" -void xhci_command(XHCIController *controller, uint32_t p1, uint32_t p2, - uint32_t status, uint32_t control) { +void xhciCommand(XHCIController *controller, uint32_t p1, uint32_t p2, + uint32_t status, uint32_t control) { control &= ~1; control |= controller->commands.cycle; @@ -57,8 +57,8 @@ } XHCITRB *trbRingFetch(TrbRing *ring) { - while ((ring->trbs[ring->dequeue].control & 1) != ring->cycle) { - syscall(-1, 0, 0, 0, 0); + if ((ring->trbs[ring->dequeue].control & 1) != ring->cycle) { + return NULL; } XHCITRB *result = &ring->trbs[ring->dequeue]; ring->dequeue++; @@ -86,8 +86,6 @@ void setupRuntime(XHCIController *controller) { controller->runtime = OFFSET(controller->capabilities, controller->capabilities->runtimeOffset); - controller->runtime->interrupters[0].enabled = true; - controller->runtime->interrupters[0].moderationCounter = 500; controller->runtime->interrupters[0].eventRingSegmentTableSize = 1; controller->runtime->interrupters[0].eventRingSegmentTableAddress[0] = U32(controller->eventRingSegmentTablePhysical); @@ -96,6 +94,9 @@ U32(&controller->events.physical[controller->events.dequeue]) | (1 << 3); controller->runtime->interrupters[0].eventRingDequeuePointer[1] = 0; + + controller->runtime->interrupters[0].management |= 3; + sleep(100); } void setupEventRingSegmentTable(XHCIController *controller) { @@ -192,33 +193,15 @@ slotInfo >> 24); return controller; } +XHCIController *controller; -static void initializeUSB(uint32_t deviceId) { - XHCIController *controller = initializeController(deviceId); - restartXHCIController(controller); - - setupTrbRing(&controller->commands, 256); - setupTrbRing(&controller->events, 256); - readExtendedCapabilities(controller); - setupOperationalRegisters(controller); - setupEventRingSegmentTable(controller); - setupRuntime(controller); - setupScratchpadBuffers(controller); - - controller->operational->command |= (1 << 0) | (1 << 2); - sleep(100); - - if (controller->operational->status & (1 << 2)) - return printf("critical XHCI problem\n"); - - // just testing for now... - xhci_command(controller, 0, 0, 0, (9 << 10)); - xhci_command(controller, 0, 0, 0, (9 << 10)); - xhci_command(controller, 0, 0, 0, (9 << 10)); - xhci_command(controller, 0, 0, 0, (9 << 10)); - - while (1) { - XHCITRB *trb = trbRingFetch(&controller->events); +void xhciInterrupt() { + if (!(controller->runtime->interrupters[0].management & 1)) { + printf("no interrupt pending\n"); + return; + } + XHCITRB *trb; + while ((trb = trbRingFetch(&controller->events))) { uint32_t type = (trb->control >> 10) & 0x3F; printf("event %i [%x %x %x %x] type: %i, slotId: %i\n", @@ -231,6 +214,35 @@ } } +void initializeUSB(uint32_t deviceId) { + controller = initializeController(deviceId); + restartXHCIController(controller); + + setupTrbRing(&controller->commands, 256); + setupTrbRing(&controller->events, 256); + readExtendedCapabilities(controller); + setupOperationalRegisters(controller); + setupEventRingSegmentTable(controller); + setupRuntime(controller); + setupScratchpadBuffers(controller); + + controller->operational->status |= (1 << 3); + controller->operational->command |= (1 << 0) | (1 << 2); + printf("using irq no. %i\n", getPCIInterrupt(deviceId, 0)); + int pic = getService("pic"); + subscribeEvent(pic, getEvent(pic, "irq11"), xhciInterrupt); + sleep(100); + + if (controller->operational->status & (1 << 2)) + return printf("critical XHCI problem\n"); + + // just testing for now... + xhciCommand(controller, 0, 0, 0, (9 << 10)); + xhciCommand(controller, 0, 0, 0, (9 << 10)); + xhciCommand(controller, 0, 0, 0, (9 << 10)); + xhciCommand(controller, 0, 0, 0, (9 << 10)); +} + int32_t main() { uint32_t i = 0, class = 0; while ((class = getDeviceClass(i, 0))) { diff --git a/src/userland/usb/usb.h b/src/userland/usb/usb.h index f7550c8..bad390b 100644 --- a/src/userland/usb/usb.h +++ b/src/userland/usb/usb.h @@ -28,7 +28,7 @@ uint32_t status; uint32_t pageSize; uint32_t reserved[2]; - uint32_t op_dnctrl; + uint32_t notification; uint32_t commandRingControl[2]; uint32_t reserved1[4]; uint32_t deviceContextBaseAddressArray[2]; @@ -45,7 +45,7 @@ } __attribute__((packed)) XHCITRB; typedef volatile struct { - uint32_t pending : 1, enabled : 1, reserved : 30; + uint32_t management; uint32_t moderationCounter : 16, moderationInterval : 16; uint32_t eventRingSegmentTableSize; // MAX 16 bit uint32_t reserved2;