diff --git a/src/kernel/memory/malloc.h b/src/kernel/memory/malloc.h index 730fcf0..a94e1a5 100644 --- a/src/kernel/memory/malloc.h +++ b/src/kernel/memory/malloc.h @@ -42,6 +42,9 @@ #define listAdd(list, data) _listAdd(malloc, list, data) +#else +extern void *malloc(uint32_t size); + #endif #endif diff --git a/src/kernel/memory/malloc.h b/src/kernel/memory/malloc.h index 730fcf0..a94e1a5 100644 --- a/src/kernel/memory/malloc.h +++ b/src/kernel/memory/malloc.h @@ -42,6 +42,9 @@ #define listAdd(list, data) _listAdd(malloc, list, data) +#else +extern void *malloc(uint32_t size); + #endif #endif diff --git a/src/userland/usb/include/usb.h b/src/userland/usb/include/usb.h index 39281a9..5950ade 100644 --- a/src/userland/usb/include/usb.h +++ b/src/userland/usb/include/usb.h @@ -5,6 +5,8 @@ #define OFFSET(ptr, off) (((void *)(ptr)) + (off)) +extern uint32_t serviceId; + typedef volatile struct { uint8_t capabilitiesLength; uint8_t reserved; diff --git a/src/kernel/memory/malloc.h b/src/kernel/memory/malloc.h index 730fcf0..a94e1a5 100644 --- a/src/kernel/memory/malloc.h +++ b/src/kernel/memory/malloc.h @@ -42,6 +42,9 @@ #define listAdd(list, data) _listAdd(malloc, list, data) +#else +extern void *malloc(uint32_t size); + #endif #endif diff --git a/src/userland/usb/include/usb.h b/src/userland/usb/include/usb.h index 39281a9..5950ade 100644 --- a/src/userland/usb/include/usb.h +++ b/src/userland/usb/include/usb.h @@ -5,6 +5,8 @@ #define OFFSET(ptr, off) (((void *)(ptr)) + (off)) +extern uint32_t serviceId; + typedef volatile struct { uint8_t capabilitiesLength; uint8_t reserved; diff --git a/src/userland/usb/main.c b/src/userland/usb/main.c index 90a3bc5..e6b04c0 100644 --- a/src/userland/usb/main.c +++ b/src/userland/usb/main.c @@ -2,6 +2,7 @@ #include #include "../hlib/include/syscalls.h" +#include "xhci/commands.h" #include "xhci/trbRing.h" #include @@ -9,20 +10,6 @@ XHCIController *controller; -CommandCompletionEvent *xhciCommand(XHCIController *controller, - uint32_t dataLow, uint32_t dataHigh, - uint32_t status, uint32_t control) { - XHCITRB trb = {0}; - trb.dataLow = dataLow; - trb.dataHigh = dataHigh; - trb.status = status; - trb.control = control; - uint32_t commandAddress = U32(enqueueCommand(&controller->commands, &trb)); - controller->doorbells[0] = 0; - uint32_t eventId = syscall(SYS_CREATE_EVENT, commandAddress, 0, 0, 0); - return PTR(await(serviceId, eventId)); -} - #define REQUEST(functionName, service, function) \ uint32_t functionName(uint32_t data1, uint32_t data2) { \ static uint32_t serviceId, functionId, initialized = false; \ @@ -170,62 +157,6 @@ return controller; } -void addressDevice(void *inputContext, uint32_t slotNumber, bool BSR) { - uint32_t control = - COMMAND_TYPE(11) | COMMAND_SLOT_ID(slotNumber) | COMMAND_BSR(BSR); - xhciCommand(controller, U32(inputContext), 0, 0, control); -} - -void configureEndpoint(void *inputContext, uint32_t slotNumber, - bool deconfigure) { - uint32_t control = COMMAND_TYPE(12) | COMMAND_SLOT_ID(slotNumber) | - COMMAND_BSR(deconfigure); - xhciCommand(controller, U32(inputContext), 0, 0, control); -} - -void evaluateContext(void *inputContext, uint32_t slotNumber) { - uint32_t control = COMMAND_TYPE(13) | COMMAND_SLOT_ID(slotNumber); - xhciCommand(controller, U32(inputContext), 0, 0, control); -} - -void *usbGetDeviceDescriptor(XHCIInputContext *inputContext, TrbRing *ring, - uint32_t slotIndex, uint32_t value, uint32_t index, - void *buffer) { - XHCISetupStageTRB setup = {0}; - setup.requestType = 0x80; - setup.request = 6; - setup.value = value; - setup.index = index; - setup.length = 4096; - setup.transferLength = 8; - setup.interruptOnCompletion = 0; - setup.interrupterTarget = 0; - setup.type = 2; - setup.transferType = 3; - setup.immediateData = 1; - - XHCIDataStageTRB data = {0}; - data.dataBuffer[0] = U32(getPhysicalAddress(buffer)); - data.inDirection = 1; - data.transferSize = 4096; - data.type = 3; - data.interrupterTarget = 0; - - XHCIStatusStageTRB status = {0}; - status.inDirection = 1; - status.evaluateNext = 0; - status.interruptOnCompletion = 1; - status.type = 4; - - enqueueCommand(ring, (void *)&setup); - enqueueCommand(ring, (void *)&data); - uint32_t commandAddress = U32(enqueueCommand(ring, (void *)&status)); - uint32_t eventId = syscall(SYS_CREATE_EVENT, commandAddress, 0, 0, 0); - controller->doorbells[slotIndex] = 1; - CommandCompletionEvent *completionEvent = PTR(await(serviceId, eventId)); - return buffer; -} - XHCIInputContext *createInputContext(XHCIController *controller, XHCIPort *port, uint32_t portIndex, uint32_t slotIndex) { XHCIInputContext *inputContext = requestMemory(1, 0, 0); @@ -247,23 +178,10 @@ return inputContext; } -TrbRing *createTRB(XHCIController *controller, XHCIInputContext *inputContext, - uint32_t slotIndex) { - TrbRing *ring = malloc(sizeof(TrbRing)); - setupTrbRing(ring, 256); - inputContext->deviceContext.endpoints[0].transferDequeuePointerLow = - U32(ring->physical) | 1; - inputContext->deviceContext.endpoints[0].transferDequeuePointerHigh = 0; - XHCIDevice *device = malloc(sizeof(XHCIDevice)); - controller->deviceContextBaseAddressArray[slotIndex] = - U32(getPhysicalAddress((void *)device)); - return ring; -} - -char *usbReadString(XHCIInputContext *inputContext, TrbRing *ring, - uint32_t slotIndex, uint32_t language, +char *usbReadString(XHCIController *controller, XHCIInputContext *inputContext, + TrbRing *ring, uint32_t slotIndex, uint32_t language, uint32_t stringDescriptor, void *buffer) { - usbGetDeviceDescriptor(inputContext, ring, slotIndex, + usbGetDeviceDescriptor(controller, inputContext, ring, slotIndex, 3 << 8 | stringDescriptor, language, buffer); uint32_t length = ((*(uint8_t *)buffer) - 2) / 2; char *string = malloc(length); @@ -283,36 +201,40 @@ return printf("port %i reset could not be done, aborting\n", portIndex - 1); } - uint32_t slotIndex = - xhciCommand(controller, 0, 0, 0, COMMAND_TYPE(9))->slotId; + uint32_t slotIndex = requestSlotIndex(controller); printf("port %i: reset done, now connecting to slot %i\n", portIndex - 1, slotIndex); XHCIInputContext *inputContext = createInputContext(controller, port, portIndex, slotIndex); TrbRing *ring = createTRB(controller, inputContext, slotIndex); printf("port %i: addressing slot\n", portIndex - 1); - addressDevice(getPhysicalAddress((void *)&inputContext->inputControl), + addressDevice(controller, + getPhysicalAddress((void *)&inputContext->inputControl), slotIndex, true); void *buffer = requestMemory(1, 0, 0); - UsbDeviceDescriptor *data = malloc(sizeof(UsbDeviceDescriptor)); - usbGetDeviceDescriptor(inputContext, ring, slotIndex, 1 << 8, 0, buffer); - memcpy(buffer, (void *)data, sizeof(UsbDeviceDescriptor)); - printf("port %i: type: %i, version %x\n", portIndex - 1, data->size, - data->descriptorType, data->usbVersion); + UsbDeviceDescriptor *descriptor = malloc(sizeof(UsbDeviceDescriptor)); + usbGetDeviceDescriptor(controller, inputContext, ring, slotIndex, 1 << 8, 0, + buffer); + memcpy(buffer, (void *)descriptor, sizeof(UsbDeviceDescriptor)); + printf("port %i: type: %i, version %x\n", portIndex - 1, descriptor->size, + descriptor->descriptorType, descriptor->usbVersion); printf("port %i: class: %i, subclass: %i, protocol %i, maxPacketSize: %i\n", - portIndex - 1, data->deviceClass, data->deviceSubclass, - data->deviceProtocol, data->maxPacketSize); + portIndex - 1, descriptor->deviceClass, descriptor->deviceSubclass, + descriptor->deviceProtocol, descriptor->maxPacketSize); inputContext->deviceContext.endpoints[0].maxPacketSize = - data->maxPacketSize == 9 ? 512 : data->maxPacketSize; - usbGetDeviceDescriptor(inputContext, ring, slotIndex, 3 << 8, 0, buffer); + descriptor->maxPacketSize == 9 ? 512 : descriptor->maxPacketSize; + usbGetDeviceDescriptor(controller, inputContext, ring, slotIndex, 3 << 8, 0, + buffer); uint32_t language = *((uint16_t *)(buffer + 2)); char *manufacturer = - usbReadString(inputContext, ring, slotIndex, language, - data->manufacturerStringDescriptor, buffer); - char *device = usbReadString(inputContext, ring, slotIndex, language, - data->deviceStringDescriptor, buffer); - char *serial = usbReadString(inputContext, ring, slotIndex, language, - data->serialNumberStringDescriptor, buffer); + usbReadString(controller, inputContext, ring, slotIndex, language, + descriptor->manufacturerStringDescriptor, buffer); + char *device = + usbReadString(controller, inputContext, ring, slotIndex, language, + descriptor->deviceStringDescriptor, buffer); + char *serial = + usbReadString(controller, inputContext, ring, slotIndex, language, + descriptor->serialNumberStringDescriptor, buffer); printf("manufacturer: %s, device: %s, serial: %s\n", manufacturer, device, serial); printf("--------\n"); diff --git a/src/kernel/memory/malloc.h b/src/kernel/memory/malloc.h index 730fcf0..a94e1a5 100644 --- a/src/kernel/memory/malloc.h +++ b/src/kernel/memory/malloc.h @@ -42,6 +42,9 @@ #define listAdd(list, data) _listAdd(malloc, list, data) +#else +extern void *malloc(uint32_t size); + #endif #endif diff --git a/src/userland/usb/include/usb.h b/src/userland/usb/include/usb.h index 39281a9..5950ade 100644 --- a/src/userland/usb/include/usb.h +++ b/src/userland/usb/include/usb.h @@ -5,6 +5,8 @@ #define OFFSET(ptr, off) (((void *)(ptr)) + (off)) +extern uint32_t serviceId; + typedef volatile struct { uint8_t capabilitiesLength; uint8_t reserved; diff --git a/src/userland/usb/main.c b/src/userland/usb/main.c index 90a3bc5..e6b04c0 100644 --- a/src/userland/usb/main.c +++ b/src/userland/usb/main.c @@ -2,6 +2,7 @@ #include #include "../hlib/include/syscalls.h" +#include "xhci/commands.h" #include "xhci/trbRing.h" #include @@ -9,20 +10,6 @@ XHCIController *controller; -CommandCompletionEvent *xhciCommand(XHCIController *controller, - uint32_t dataLow, uint32_t dataHigh, - uint32_t status, uint32_t control) { - XHCITRB trb = {0}; - trb.dataLow = dataLow; - trb.dataHigh = dataHigh; - trb.status = status; - trb.control = control; - uint32_t commandAddress = U32(enqueueCommand(&controller->commands, &trb)); - controller->doorbells[0] = 0; - uint32_t eventId = syscall(SYS_CREATE_EVENT, commandAddress, 0, 0, 0); - return PTR(await(serviceId, eventId)); -} - #define REQUEST(functionName, service, function) \ uint32_t functionName(uint32_t data1, uint32_t data2) { \ static uint32_t serviceId, functionId, initialized = false; \ @@ -170,62 +157,6 @@ return controller; } -void addressDevice(void *inputContext, uint32_t slotNumber, bool BSR) { - uint32_t control = - COMMAND_TYPE(11) | COMMAND_SLOT_ID(slotNumber) | COMMAND_BSR(BSR); - xhciCommand(controller, U32(inputContext), 0, 0, control); -} - -void configureEndpoint(void *inputContext, uint32_t slotNumber, - bool deconfigure) { - uint32_t control = COMMAND_TYPE(12) | COMMAND_SLOT_ID(slotNumber) | - COMMAND_BSR(deconfigure); - xhciCommand(controller, U32(inputContext), 0, 0, control); -} - -void evaluateContext(void *inputContext, uint32_t slotNumber) { - uint32_t control = COMMAND_TYPE(13) | COMMAND_SLOT_ID(slotNumber); - xhciCommand(controller, U32(inputContext), 0, 0, control); -} - -void *usbGetDeviceDescriptor(XHCIInputContext *inputContext, TrbRing *ring, - uint32_t slotIndex, uint32_t value, uint32_t index, - void *buffer) { - XHCISetupStageTRB setup = {0}; - setup.requestType = 0x80; - setup.request = 6; - setup.value = value; - setup.index = index; - setup.length = 4096; - setup.transferLength = 8; - setup.interruptOnCompletion = 0; - setup.interrupterTarget = 0; - setup.type = 2; - setup.transferType = 3; - setup.immediateData = 1; - - XHCIDataStageTRB data = {0}; - data.dataBuffer[0] = U32(getPhysicalAddress(buffer)); - data.inDirection = 1; - data.transferSize = 4096; - data.type = 3; - data.interrupterTarget = 0; - - XHCIStatusStageTRB status = {0}; - status.inDirection = 1; - status.evaluateNext = 0; - status.interruptOnCompletion = 1; - status.type = 4; - - enqueueCommand(ring, (void *)&setup); - enqueueCommand(ring, (void *)&data); - uint32_t commandAddress = U32(enqueueCommand(ring, (void *)&status)); - uint32_t eventId = syscall(SYS_CREATE_EVENT, commandAddress, 0, 0, 0); - controller->doorbells[slotIndex] = 1; - CommandCompletionEvent *completionEvent = PTR(await(serviceId, eventId)); - return buffer; -} - XHCIInputContext *createInputContext(XHCIController *controller, XHCIPort *port, uint32_t portIndex, uint32_t slotIndex) { XHCIInputContext *inputContext = requestMemory(1, 0, 0); @@ -247,23 +178,10 @@ return inputContext; } -TrbRing *createTRB(XHCIController *controller, XHCIInputContext *inputContext, - uint32_t slotIndex) { - TrbRing *ring = malloc(sizeof(TrbRing)); - setupTrbRing(ring, 256); - inputContext->deviceContext.endpoints[0].transferDequeuePointerLow = - U32(ring->physical) | 1; - inputContext->deviceContext.endpoints[0].transferDequeuePointerHigh = 0; - XHCIDevice *device = malloc(sizeof(XHCIDevice)); - controller->deviceContextBaseAddressArray[slotIndex] = - U32(getPhysicalAddress((void *)device)); - return ring; -} - -char *usbReadString(XHCIInputContext *inputContext, TrbRing *ring, - uint32_t slotIndex, uint32_t language, +char *usbReadString(XHCIController *controller, XHCIInputContext *inputContext, + TrbRing *ring, uint32_t slotIndex, uint32_t language, uint32_t stringDescriptor, void *buffer) { - usbGetDeviceDescriptor(inputContext, ring, slotIndex, + usbGetDeviceDescriptor(controller, inputContext, ring, slotIndex, 3 << 8 | stringDescriptor, language, buffer); uint32_t length = ((*(uint8_t *)buffer) - 2) / 2; char *string = malloc(length); @@ -283,36 +201,40 @@ return printf("port %i reset could not be done, aborting\n", portIndex - 1); } - uint32_t slotIndex = - xhciCommand(controller, 0, 0, 0, COMMAND_TYPE(9))->slotId; + uint32_t slotIndex = requestSlotIndex(controller); printf("port %i: reset done, now connecting to slot %i\n", portIndex - 1, slotIndex); XHCIInputContext *inputContext = createInputContext(controller, port, portIndex, slotIndex); TrbRing *ring = createTRB(controller, inputContext, slotIndex); printf("port %i: addressing slot\n", portIndex - 1); - addressDevice(getPhysicalAddress((void *)&inputContext->inputControl), + addressDevice(controller, + getPhysicalAddress((void *)&inputContext->inputControl), slotIndex, true); void *buffer = requestMemory(1, 0, 0); - UsbDeviceDescriptor *data = malloc(sizeof(UsbDeviceDescriptor)); - usbGetDeviceDescriptor(inputContext, ring, slotIndex, 1 << 8, 0, buffer); - memcpy(buffer, (void *)data, sizeof(UsbDeviceDescriptor)); - printf("port %i: type: %i, version %x\n", portIndex - 1, data->size, - data->descriptorType, data->usbVersion); + UsbDeviceDescriptor *descriptor = malloc(sizeof(UsbDeviceDescriptor)); + usbGetDeviceDescriptor(controller, inputContext, ring, slotIndex, 1 << 8, 0, + buffer); + memcpy(buffer, (void *)descriptor, sizeof(UsbDeviceDescriptor)); + printf("port %i: type: %i, version %x\n", portIndex - 1, descriptor->size, + descriptor->descriptorType, descriptor->usbVersion); printf("port %i: class: %i, subclass: %i, protocol %i, maxPacketSize: %i\n", - portIndex - 1, data->deviceClass, data->deviceSubclass, - data->deviceProtocol, data->maxPacketSize); + portIndex - 1, descriptor->deviceClass, descriptor->deviceSubclass, + descriptor->deviceProtocol, descriptor->maxPacketSize); inputContext->deviceContext.endpoints[0].maxPacketSize = - data->maxPacketSize == 9 ? 512 : data->maxPacketSize; - usbGetDeviceDescriptor(inputContext, ring, slotIndex, 3 << 8, 0, buffer); + descriptor->maxPacketSize == 9 ? 512 : descriptor->maxPacketSize; + usbGetDeviceDescriptor(controller, inputContext, ring, slotIndex, 3 << 8, 0, + buffer); uint32_t language = *((uint16_t *)(buffer + 2)); char *manufacturer = - usbReadString(inputContext, ring, slotIndex, language, - data->manufacturerStringDescriptor, buffer); - char *device = usbReadString(inputContext, ring, slotIndex, language, - data->deviceStringDescriptor, buffer); - char *serial = usbReadString(inputContext, ring, slotIndex, language, - data->serialNumberStringDescriptor, buffer); + usbReadString(controller, inputContext, ring, slotIndex, language, + descriptor->manufacturerStringDescriptor, buffer); + char *device = + usbReadString(controller, inputContext, ring, slotIndex, language, + descriptor->deviceStringDescriptor, buffer); + char *serial = + usbReadString(controller, inputContext, ring, slotIndex, language, + descriptor->serialNumberStringDescriptor, buffer); printf("manufacturer: %s, device: %s, serial: %s\n", manufacturer, device, serial); printf("--------\n"); diff --git a/src/userland/usb/xhci/commands.c b/src/userland/usb/xhci/commands.c new file mode 100644 index 0000000..8bdbf64 --- /dev/null +++ b/src/userland/usb/xhci/commands.c @@ -0,0 +1,82 @@ +#include "commands.h" +#include "../../hlib/include/syscalls.h" +#include "trbRing.h" +#include +#include + +CommandCompletionEvent *xhciCommand(XHCIController *controller, + uint32_t dataLow, uint32_t dataHigh, + uint32_t status, uint32_t control) { + XHCITRB trb = {0}; + trb.dataLow = dataLow; + trb.dataHigh = dataHigh; + trb.status = status; + trb.control = control; + uint32_t commandAddress = U32(enqueueCommand(&controller->commands, &trb)); + controller->doorbells[0] = 0; + uint32_t eventId = syscall(SYS_CREATE_EVENT, commandAddress, 0, 0, 0); + return PTR(await(serviceId, eventId)); +} + +void addressDevice(XHCIController *controller, void *inputContext, + uint32_t slotNumber, bool BSR) { + uint32_t control = + COMMAND_TYPE(11) | COMMAND_SLOT_ID(slotNumber) | COMMAND_BSR(BSR); + xhciCommand(controller, U32(inputContext), 0, 0, control); +} + +void configureEndpoint(XHCIController *controller, void *inputContext, + uint32_t slotNumber, bool deconfigure) { + uint32_t control = COMMAND_TYPE(12) | COMMAND_SLOT_ID(slotNumber) | + COMMAND_BSR(deconfigure); + xhciCommand(controller, U32(inputContext), 0, 0, control); +} + +void evaluateContext(XHCIController *controller, void *inputContext, + uint32_t slotNumber) { + uint32_t control = COMMAND_TYPE(13) | COMMAND_SLOT_ID(slotNumber); + xhciCommand(controller, U32(inputContext), 0, 0, control); +} + +uint32_t requestSlotIndex(XHCIController *controller) { + return xhciCommand(controller, 0, 0, 0, COMMAND_TYPE(9))->slotId; +} + +void *usbGetDeviceDescriptor(XHCIController *controller, + XHCIInputContext *inputContext, TrbRing *ring, + uint32_t slotIndex, uint32_t value, uint32_t index, + void *buffer) { + XHCISetupStageTRB setup = {0}; + setup.requestType = 0x80; + setup.request = 6; + setup.value = value; + setup.index = index; + setup.length = 4096; + setup.transferLength = 8; + setup.interruptOnCompletion = 0; + setup.interrupterTarget = 0; + setup.type = 2; + setup.transferType = 3; + setup.immediateData = 1; + + XHCIDataStageTRB data = {0}; + data.dataBuffer[0] = U32(getPhysicalAddress(buffer)); + data.inDirection = 1; + data.transferSize = 4096; + data.type = 3; + data.interrupterTarget = 0; + + XHCIStatusStageTRB status = {0}; + status.inDirection = 1; + status.evaluateNext = 0; + status.interruptOnCompletion = 1; + status.type = 4; + + enqueueCommand(ring, (void *)&setup); + enqueueCommand(ring, (void *)&data); + uint32_t commandAddress = U32(enqueueCommand(ring, (void *)&status)); + uint32_t eventId = syscall(SYS_CREATE_EVENT, commandAddress, 0, 0, 0); + controller->doorbells[slotIndex] = 1; + CommandCompletionEvent *completionEvent = PTR(await(serviceId, eventId)); + return buffer; +} diff --git a/src/kernel/memory/malloc.h b/src/kernel/memory/malloc.h index 730fcf0..a94e1a5 100644 --- a/src/kernel/memory/malloc.h +++ b/src/kernel/memory/malloc.h @@ -42,6 +42,9 @@ #define listAdd(list, data) _listAdd(malloc, list, data) +#else +extern void *malloc(uint32_t size); + #endif #endif diff --git a/src/userland/usb/include/usb.h b/src/userland/usb/include/usb.h index 39281a9..5950ade 100644 --- a/src/userland/usb/include/usb.h +++ b/src/userland/usb/include/usb.h @@ -5,6 +5,8 @@ #define OFFSET(ptr, off) (((void *)(ptr)) + (off)) +extern uint32_t serviceId; + typedef volatile struct { uint8_t capabilitiesLength; uint8_t reserved; diff --git a/src/userland/usb/main.c b/src/userland/usb/main.c index 90a3bc5..e6b04c0 100644 --- a/src/userland/usb/main.c +++ b/src/userland/usb/main.c @@ -2,6 +2,7 @@ #include #include "../hlib/include/syscalls.h" +#include "xhci/commands.h" #include "xhci/trbRing.h" #include @@ -9,20 +10,6 @@ XHCIController *controller; -CommandCompletionEvent *xhciCommand(XHCIController *controller, - uint32_t dataLow, uint32_t dataHigh, - uint32_t status, uint32_t control) { - XHCITRB trb = {0}; - trb.dataLow = dataLow; - trb.dataHigh = dataHigh; - trb.status = status; - trb.control = control; - uint32_t commandAddress = U32(enqueueCommand(&controller->commands, &trb)); - controller->doorbells[0] = 0; - uint32_t eventId = syscall(SYS_CREATE_EVENT, commandAddress, 0, 0, 0); - return PTR(await(serviceId, eventId)); -} - #define REQUEST(functionName, service, function) \ uint32_t functionName(uint32_t data1, uint32_t data2) { \ static uint32_t serviceId, functionId, initialized = false; \ @@ -170,62 +157,6 @@ return controller; } -void addressDevice(void *inputContext, uint32_t slotNumber, bool BSR) { - uint32_t control = - COMMAND_TYPE(11) | COMMAND_SLOT_ID(slotNumber) | COMMAND_BSR(BSR); - xhciCommand(controller, U32(inputContext), 0, 0, control); -} - -void configureEndpoint(void *inputContext, uint32_t slotNumber, - bool deconfigure) { - uint32_t control = COMMAND_TYPE(12) | COMMAND_SLOT_ID(slotNumber) | - COMMAND_BSR(deconfigure); - xhciCommand(controller, U32(inputContext), 0, 0, control); -} - -void evaluateContext(void *inputContext, uint32_t slotNumber) { - uint32_t control = COMMAND_TYPE(13) | COMMAND_SLOT_ID(slotNumber); - xhciCommand(controller, U32(inputContext), 0, 0, control); -} - -void *usbGetDeviceDescriptor(XHCIInputContext *inputContext, TrbRing *ring, - uint32_t slotIndex, uint32_t value, uint32_t index, - void *buffer) { - XHCISetupStageTRB setup = {0}; - setup.requestType = 0x80; - setup.request = 6; - setup.value = value; - setup.index = index; - setup.length = 4096; - setup.transferLength = 8; - setup.interruptOnCompletion = 0; - setup.interrupterTarget = 0; - setup.type = 2; - setup.transferType = 3; - setup.immediateData = 1; - - XHCIDataStageTRB data = {0}; - data.dataBuffer[0] = U32(getPhysicalAddress(buffer)); - data.inDirection = 1; - data.transferSize = 4096; - data.type = 3; - data.interrupterTarget = 0; - - XHCIStatusStageTRB status = {0}; - status.inDirection = 1; - status.evaluateNext = 0; - status.interruptOnCompletion = 1; - status.type = 4; - - enqueueCommand(ring, (void *)&setup); - enqueueCommand(ring, (void *)&data); - uint32_t commandAddress = U32(enqueueCommand(ring, (void *)&status)); - uint32_t eventId = syscall(SYS_CREATE_EVENT, commandAddress, 0, 0, 0); - controller->doorbells[slotIndex] = 1; - CommandCompletionEvent *completionEvent = PTR(await(serviceId, eventId)); - return buffer; -} - XHCIInputContext *createInputContext(XHCIController *controller, XHCIPort *port, uint32_t portIndex, uint32_t slotIndex) { XHCIInputContext *inputContext = requestMemory(1, 0, 0); @@ -247,23 +178,10 @@ return inputContext; } -TrbRing *createTRB(XHCIController *controller, XHCIInputContext *inputContext, - uint32_t slotIndex) { - TrbRing *ring = malloc(sizeof(TrbRing)); - setupTrbRing(ring, 256); - inputContext->deviceContext.endpoints[0].transferDequeuePointerLow = - U32(ring->physical) | 1; - inputContext->deviceContext.endpoints[0].transferDequeuePointerHigh = 0; - XHCIDevice *device = malloc(sizeof(XHCIDevice)); - controller->deviceContextBaseAddressArray[slotIndex] = - U32(getPhysicalAddress((void *)device)); - return ring; -} - -char *usbReadString(XHCIInputContext *inputContext, TrbRing *ring, - uint32_t slotIndex, uint32_t language, +char *usbReadString(XHCIController *controller, XHCIInputContext *inputContext, + TrbRing *ring, uint32_t slotIndex, uint32_t language, uint32_t stringDescriptor, void *buffer) { - usbGetDeviceDescriptor(inputContext, ring, slotIndex, + usbGetDeviceDescriptor(controller, inputContext, ring, slotIndex, 3 << 8 | stringDescriptor, language, buffer); uint32_t length = ((*(uint8_t *)buffer) - 2) / 2; char *string = malloc(length); @@ -283,36 +201,40 @@ return printf("port %i reset could not be done, aborting\n", portIndex - 1); } - uint32_t slotIndex = - xhciCommand(controller, 0, 0, 0, COMMAND_TYPE(9))->slotId; + uint32_t slotIndex = requestSlotIndex(controller); printf("port %i: reset done, now connecting to slot %i\n", portIndex - 1, slotIndex); XHCIInputContext *inputContext = createInputContext(controller, port, portIndex, slotIndex); TrbRing *ring = createTRB(controller, inputContext, slotIndex); printf("port %i: addressing slot\n", portIndex - 1); - addressDevice(getPhysicalAddress((void *)&inputContext->inputControl), + addressDevice(controller, + getPhysicalAddress((void *)&inputContext->inputControl), slotIndex, true); void *buffer = requestMemory(1, 0, 0); - UsbDeviceDescriptor *data = malloc(sizeof(UsbDeviceDescriptor)); - usbGetDeviceDescriptor(inputContext, ring, slotIndex, 1 << 8, 0, buffer); - memcpy(buffer, (void *)data, sizeof(UsbDeviceDescriptor)); - printf("port %i: type: %i, version %x\n", portIndex - 1, data->size, - data->descriptorType, data->usbVersion); + UsbDeviceDescriptor *descriptor = malloc(sizeof(UsbDeviceDescriptor)); + usbGetDeviceDescriptor(controller, inputContext, ring, slotIndex, 1 << 8, 0, + buffer); + memcpy(buffer, (void *)descriptor, sizeof(UsbDeviceDescriptor)); + printf("port %i: type: %i, version %x\n", portIndex - 1, descriptor->size, + descriptor->descriptorType, descriptor->usbVersion); printf("port %i: class: %i, subclass: %i, protocol %i, maxPacketSize: %i\n", - portIndex - 1, data->deviceClass, data->deviceSubclass, - data->deviceProtocol, data->maxPacketSize); + portIndex - 1, descriptor->deviceClass, descriptor->deviceSubclass, + descriptor->deviceProtocol, descriptor->maxPacketSize); inputContext->deviceContext.endpoints[0].maxPacketSize = - data->maxPacketSize == 9 ? 512 : data->maxPacketSize; - usbGetDeviceDescriptor(inputContext, ring, slotIndex, 3 << 8, 0, buffer); + descriptor->maxPacketSize == 9 ? 512 : descriptor->maxPacketSize; + usbGetDeviceDescriptor(controller, inputContext, ring, slotIndex, 3 << 8, 0, + buffer); uint32_t language = *((uint16_t *)(buffer + 2)); char *manufacturer = - usbReadString(inputContext, ring, slotIndex, language, - data->manufacturerStringDescriptor, buffer); - char *device = usbReadString(inputContext, ring, slotIndex, language, - data->deviceStringDescriptor, buffer); - char *serial = usbReadString(inputContext, ring, slotIndex, language, - data->serialNumberStringDescriptor, buffer); + usbReadString(controller, inputContext, ring, slotIndex, language, + descriptor->manufacturerStringDescriptor, buffer); + char *device = + usbReadString(controller, inputContext, ring, slotIndex, language, + descriptor->deviceStringDescriptor, buffer); + char *serial = + usbReadString(controller, inputContext, ring, slotIndex, language, + descriptor->serialNumberStringDescriptor, buffer); printf("manufacturer: %s, device: %s, serial: %s\n", manufacturer, device, serial); printf("--------\n"); diff --git a/src/userland/usb/xhci/commands.c b/src/userland/usb/xhci/commands.c new file mode 100644 index 0000000..8bdbf64 --- /dev/null +++ b/src/userland/usb/xhci/commands.c @@ -0,0 +1,82 @@ +#include "commands.h" +#include "../../hlib/include/syscalls.h" +#include "trbRing.h" +#include +#include + +CommandCompletionEvent *xhciCommand(XHCIController *controller, + uint32_t dataLow, uint32_t dataHigh, + uint32_t status, uint32_t control) { + XHCITRB trb = {0}; + trb.dataLow = dataLow; + trb.dataHigh = dataHigh; + trb.status = status; + trb.control = control; + uint32_t commandAddress = U32(enqueueCommand(&controller->commands, &trb)); + controller->doorbells[0] = 0; + uint32_t eventId = syscall(SYS_CREATE_EVENT, commandAddress, 0, 0, 0); + return PTR(await(serviceId, eventId)); +} + +void addressDevice(XHCIController *controller, void *inputContext, + uint32_t slotNumber, bool BSR) { + uint32_t control = + COMMAND_TYPE(11) | COMMAND_SLOT_ID(slotNumber) | COMMAND_BSR(BSR); + xhciCommand(controller, U32(inputContext), 0, 0, control); +} + +void configureEndpoint(XHCIController *controller, void *inputContext, + uint32_t slotNumber, bool deconfigure) { + uint32_t control = COMMAND_TYPE(12) | COMMAND_SLOT_ID(slotNumber) | + COMMAND_BSR(deconfigure); + xhciCommand(controller, U32(inputContext), 0, 0, control); +} + +void evaluateContext(XHCIController *controller, void *inputContext, + uint32_t slotNumber) { + uint32_t control = COMMAND_TYPE(13) | COMMAND_SLOT_ID(slotNumber); + xhciCommand(controller, U32(inputContext), 0, 0, control); +} + +uint32_t requestSlotIndex(XHCIController *controller) { + return xhciCommand(controller, 0, 0, 0, COMMAND_TYPE(9))->slotId; +} + +void *usbGetDeviceDescriptor(XHCIController *controller, + XHCIInputContext *inputContext, TrbRing *ring, + uint32_t slotIndex, uint32_t value, uint32_t index, + void *buffer) { + XHCISetupStageTRB setup = {0}; + setup.requestType = 0x80; + setup.request = 6; + setup.value = value; + setup.index = index; + setup.length = 4096; + setup.transferLength = 8; + setup.interruptOnCompletion = 0; + setup.interrupterTarget = 0; + setup.type = 2; + setup.transferType = 3; + setup.immediateData = 1; + + XHCIDataStageTRB data = {0}; + data.dataBuffer[0] = U32(getPhysicalAddress(buffer)); + data.inDirection = 1; + data.transferSize = 4096; + data.type = 3; + data.interrupterTarget = 0; + + XHCIStatusStageTRB status = {0}; + status.inDirection = 1; + status.evaluateNext = 0; + status.interruptOnCompletion = 1; + status.type = 4; + + enqueueCommand(ring, (void *)&setup); + enqueueCommand(ring, (void *)&data); + uint32_t commandAddress = U32(enqueueCommand(ring, (void *)&status)); + uint32_t eventId = syscall(SYS_CREATE_EVENT, commandAddress, 0, 0, 0); + controller->doorbells[slotIndex] = 1; + CommandCompletionEvent *completionEvent = PTR(await(serviceId, eventId)); + return buffer; +} diff --git a/src/userland/usb/xhci/commands.h b/src/userland/usb/xhci/commands.h new file mode 100644 index 0000000..00d497c --- /dev/null +++ b/src/userland/usb/xhci/commands.h @@ -0,0 +1,25 @@ +#ifndef COMMANDS_H +#define COMMANDS_H + +#include +#include + +extern void addressDevice(XHCIController *controller, void *inputContext, + uint32_t slotNumber, bool BSR); +extern void configureEndpoint(XHCIController *controller, void *inputContext, + uint32_t slotNumber, bool deconfigure); +extern void evaluateContext(XHCIController *controller, void *inputContext, + uint32_t slotNumber); + +extern uint32_t requestSlotIndex(XHCIController *controller); + +extern void *usbGetDeviceDescriptor(XHCIController *controller, + XHCIInputContext *inputContext, + TrbRing *ring, uint32_t slotIndex, + uint32_t value, uint32_t index, + void *buffer); + +extern TrbRing *createTRB(XHCIController *controller, + XHCIInputContext *inputContext, uint32_t slotIndex); + +#endif diff --git a/src/kernel/memory/malloc.h b/src/kernel/memory/malloc.h index 730fcf0..a94e1a5 100644 --- a/src/kernel/memory/malloc.h +++ b/src/kernel/memory/malloc.h @@ -42,6 +42,9 @@ #define listAdd(list, data) _listAdd(malloc, list, data) +#else +extern void *malloc(uint32_t size); + #endif #endif diff --git a/src/userland/usb/include/usb.h b/src/userland/usb/include/usb.h index 39281a9..5950ade 100644 --- a/src/userland/usb/include/usb.h +++ b/src/userland/usb/include/usb.h @@ -5,6 +5,8 @@ #define OFFSET(ptr, off) (((void *)(ptr)) + (off)) +extern uint32_t serviceId; + typedef volatile struct { uint8_t capabilitiesLength; uint8_t reserved; diff --git a/src/userland/usb/main.c b/src/userland/usb/main.c index 90a3bc5..e6b04c0 100644 --- a/src/userland/usb/main.c +++ b/src/userland/usb/main.c @@ -2,6 +2,7 @@ #include #include "../hlib/include/syscalls.h" +#include "xhci/commands.h" #include "xhci/trbRing.h" #include @@ -9,20 +10,6 @@ XHCIController *controller; -CommandCompletionEvent *xhciCommand(XHCIController *controller, - uint32_t dataLow, uint32_t dataHigh, - uint32_t status, uint32_t control) { - XHCITRB trb = {0}; - trb.dataLow = dataLow; - trb.dataHigh = dataHigh; - trb.status = status; - trb.control = control; - uint32_t commandAddress = U32(enqueueCommand(&controller->commands, &trb)); - controller->doorbells[0] = 0; - uint32_t eventId = syscall(SYS_CREATE_EVENT, commandAddress, 0, 0, 0); - return PTR(await(serviceId, eventId)); -} - #define REQUEST(functionName, service, function) \ uint32_t functionName(uint32_t data1, uint32_t data2) { \ static uint32_t serviceId, functionId, initialized = false; \ @@ -170,62 +157,6 @@ return controller; } -void addressDevice(void *inputContext, uint32_t slotNumber, bool BSR) { - uint32_t control = - COMMAND_TYPE(11) | COMMAND_SLOT_ID(slotNumber) | COMMAND_BSR(BSR); - xhciCommand(controller, U32(inputContext), 0, 0, control); -} - -void configureEndpoint(void *inputContext, uint32_t slotNumber, - bool deconfigure) { - uint32_t control = COMMAND_TYPE(12) | COMMAND_SLOT_ID(slotNumber) | - COMMAND_BSR(deconfigure); - xhciCommand(controller, U32(inputContext), 0, 0, control); -} - -void evaluateContext(void *inputContext, uint32_t slotNumber) { - uint32_t control = COMMAND_TYPE(13) | COMMAND_SLOT_ID(slotNumber); - xhciCommand(controller, U32(inputContext), 0, 0, control); -} - -void *usbGetDeviceDescriptor(XHCIInputContext *inputContext, TrbRing *ring, - uint32_t slotIndex, uint32_t value, uint32_t index, - void *buffer) { - XHCISetupStageTRB setup = {0}; - setup.requestType = 0x80; - setup.request = 6; - setup.value = value; - setup.index = index; - setup.length = 4096; - setup.transferLength = 8; - setup.interruptOnCompletion = 0; - setup.interrupterTarget = 0; - setup.type = 2; - setup.transferType = 3; - setup.immediateData = 1; - - XHCIDataStageTRB data = {0}; - data.dataBuffer[0] = U32(getPhysicalAddress(buffer)); - data.inDirection = 1; - data.transferSize = 4096; - data.type = 3; - data.interrupterTarget = 0; - - XHCIStatusStageTRB status = {0}; - status.inDirection = 1; - status.evaluateNext = 0; - status.interruptOnCompletion = 1; - status.type = 4; - - enqueueCommand(ring, (void *)&setup); - enqueueCommand(ring, (void *)&data); - uint32_t commandAddress = U32(enqueueCommand(ring, (void *)&status)); - uint32_t eventId = syscall(SYS_CREATE_EVENT, commandAddress, 0, 0, 0); - controller->doorbells[slotIndex] = 1; - CommandCompletionEvent *completionEvent = PTR(await(serviceId, eventId)); - return buffer; -} - XHCIInputContext *createInputContext(XHCIController *controller, XHCIPort *port, uint32_t portIndex, uint32_t slotIndex) { XHCIInputContext *inputContext = requestMemory(1, 0, 0); @@ -247,23 +178,10 @@ return inputContext; } -TrbRing *createTRB(XHCIController *controller, XHCIInputContext *inputContext, - uint32_t slotIndex) { - TrbRing *ring = malloc(sizeof(TrbRing)); - setupTrbRing(ring, 256); - inputContext->deviceContext.endpoints[0].transferDequeuePointerLow = - U32(ring->physical) | 1; - inputContext->deviceContext.endpoints[0].transferDequeuePointerHigh = 0; - XHCIDevice *device = malloc(sizeof(XHCIDevice)); - controller->deviceContextBaseAddressArray[slotIndex] = - U32(getPhysicalAddress((void *)device)); - return ring; -} - -char *usbReadString(XHCIInputContext *inputContext, TrbRing *ring, - uint32_t slotIndex, uint32_t language, +char *usbReadString(XHCIController *controller, XHCIInputContext *inputContext, + TrbRing *ring, uint32_t slotIndex, uint32_t language, uint32_t stringDescriptor, void *buffer) { - usbGetDeviceDescriptor(inputContext, ring, slotIndex, + usbGetDeviceDescriptor(controller, inputContext, ring, slotIndex, 3 << 8 | stringDescriptor, language, buffer); uint32_t length = ((*(uint8_t *)buffer) - 2) / 2; char *string = malloc(length); @@ -283,36 +201,40 @@ return printf("port %i reset could not be done, aborting\n", portIndex - 1); } - uint32_t slotIndex = - xhciCommand(controller, 0, 0, 0, COMMAND_TYPE(9))->slotId; + uint32_t slotIndex = requestSlotIndex(controller); printf("port %i: reset done, now connecting to slot %i\n", portIndex - 1, slotIndex); XHCIInputContext *inputContext = createInputContext(controller, port, portIndex, slotIndex); TrbRing *ring = createTRB(controller, inputContext, slotIndex); printf("port %i: addressing slot\n", portIndex - 1); - addressDevice(getPhysicalAddress((void *)&inputContext->inputControl), + addressDevice(controller, + getPhysicalAddress((void *)&inputContext->inputControl), slotIndex, true); void *buffer = requestMemory(1, 0, 0); - UsbDeviceDescriptor *data = malloc(sizeof(UsbDeviceDescriptor)); - usbGetDeviceDescriptor(inputContext, ring, slotIndex, 1 << 8, 0, buffer); - memcpy(buffer, (void *)data, sizeof(UsbDeviceDescriptor)); - printf("port %i: type: %i, version %x\n", portIndex - 1, data->size, - data->descriptorType, data->usbVersion); + UsbDeviceDescriptor *descriptor = malloc(sizeof(UsbDeviceDescriptor)); + usbGetDeviceDescriptor(controller, inputContext, ring, slotIndex, 1 << 8, 0, + buffer); + memcpy(buffer, (void *)descriptor, sizeof(UsbDeviceDescriptor)); + printf("port %i: type: %i, version %x\n", portIndex - 1, descriptor->size, + descriptor->descriptorType, descriptor->usbVersion); printf("port %i: class: %i, subclass: %i, protocol %i, maxPacketSize: %i\n", - portIndex - 1, data->deviceClass, data->deviceSubclass, - data->deviceProtocol, data->maxPacketSize); + portIndex - 1, descriptor->deviceClass, descriptor->deviceSubclass, + descriptor->deviceProtocol, descriptor->maxPacketSize); inputContext->deviceContext.endpoints[0].maxPacketSize = - data->maxPacketSize == 9 ? 512 : data->maxPacketSize; - usbGetDeviceDescriptor(inputContext, ring, slotIndex, 3 << 8, 0, buffer); + descriptor->maxPacketSize == 9 ? 512 : descriptor->maxPacketSize; + usbGetDeviceDescriptor(controller, inputContext, ring, slotIndex, 3 << 8, 0, + buffer); uint32_t language = *((uint16_t *)(buffer + 2)); char *manufacturer = - usbReadString(inputContext, ring, slotIndex, language, - data->manufacturerStringDescriptor, buffer); - char *device = usbReadString(inputContext, ring, slotIndex, language, - data->deviceStringDescriptor, buffer); - char *serial = usbReadString(inputContext, ring, slotIndex, language, - data->serialNumberStringDescriptor, buffer); + usbReadString(controller, inputContext, ring, slotIndex, language, + descriptor->manufacturerStringDescriptor, buffer); + char *device = + usbReadString(controller, inputContext, ring, slotIndex, language, + descriptor->deviceStringDescriptor, buffer); + char *serial = + usbReadString(controller, inputContext, ring, slotIndex, language, + descriptor->serialNumberStringDescriptor, buffer); printf("manufacturer: %s, device: %s, serial: %s\n", manufacturer, device, serial); printf("--------\n"); diff --git a/src/userland/usb/xhci/commands.c b/src/userland/usb/xhci/commands.c new file mode 100644 index 0000000..8bdbf64 --- /dev/null +++ b/src/userland/usb/xhci/commands.c @@ -0,0 +1,82 @@ +#include "commands.h" +#include "../../hlib/include/syscalls.h" +#include "trbRing.h" +#include +#include + +CommandCompletionEvent *xhciCommand(XHCIController *controller, + uint32_t dataLow, uint32_t dataHigh, + uint32_t status, uint32_t control) { + XHCITRB trb = {0}; + trb.dataLow = dataLow; + trb.dataHigh = dataHigh; + trb.status = status; + trb.control = control; + uint32_t commandAddress = U32(enqueueCommand(&controller->commands, &trb)); + controller->doorbells[0] = 0; + uint32_t eventId = syscall(SYS_CREATE_EVENT, commandAddress, 0, 0, 0); + return PTR(await(serviceId, eventId)); +} + +void addressDevice(XHCIController *controller, void *inputContext, + uint32_t slotNumber, bool BSR) { + uint32_t control = + COMMAND_TYPE(11) | COMMAND_SLOT_ID(slotNumber) | COMMAND_BSR(BSR); + xhciCommand(controller, U32(inputContext), 0, 0, control); +} + +void configureEndpoint(XHCIController *controller, void *inputContext, + uint32_t slotNumber, bool deconfigure) { + uint32_t control = COMMAND_TYPE(12) | COMMAND_SLOT_ID(slotNumber) | + COMMAND_BSR(deconfigure); + xhciCommand(controller, U32(inputContext), 0, 0, control); +} + +void evaluateContext(XHCIController *controller, void *inputContext, + uint32_t slotNumber) { + uint32_t control = COMMAND_TYPE(13) | COMMAND_SLOT_ID(slotNumber); + xhciCommand(controller, U32(inputContext), 0, 0, control); +} + +uint32_t requestSlotIndex(XHCIController *controller) { + return xhciCommand(controller, 0, 0, 0, COMMAND_TYPE(9))->slotId; +} + +void *usbGetDeviceDescriptor(XHCIController *controller, + XHCIInputContext *inputContext, TrbRing *ring, + uint32_t slotIndex, uint32_t value, uint32_t index, + void *buffer) { + XHCISetupStageTRB setup = {0}; + setup.requestType = 0x80; + setup.request = 6; + setup.value = value; + setup.index = index; + setup.length = 4096; + setup.transferLength = 8; + setup.interruptOnCompletion = 0; + setup.interrupterTarget = 0; + setup.type = 2; + setup.transferType = 3; + setup.immediateData = 1; + + XHCIDataStageTRB data = {0}; + data.dataBuffer[0] = U32(getPhysicalAddress(buffer)); + data.inDirection = 1; + data.transferSize = 4096; + data.type = 3; + data.interrupterTarget = 0; + + XHCIStatusStageTRB status = {0}; + status.inDirection = 1; + status.evaluateNext = 0; + status.interruptOnCompletion = 1; + status.type = 4; + + enqueueCommand(ring, (void *)&setup); + enqueueCommand(ring, (void *)&data); + uint32_t commandAddress = U32(enqueueCommand(ring, (void *)&status)); + uint32_t eventId = syscall(SYS_CREATE_EVENT, commandAddress, 0, 0, 0); + controller->doorbells[slotIndex] = 1; + CommandCompletionEvent *completionEvent = PTR(await(serviceId, eventId)); + return buffer; +} diff --git a/src/userland/usb/xhci/commands.h b/src/userland/usb/xhci/commands.h new file mode 100644 index 0000000..00d497c --- /dev/null +++ b/src/userland/usb/xhci/commands.h @@ -0,0 +1,25 @@ +#ifndef COMMANDS_H +#define COMMANDS_H + +#include +#include + +extern void addressDevice(XHCIController *controller, void *inputContext, + uint32_t slotNumber, bool BSR); +extern void configureEndpoint(XHCIController *controller, void *inputContext, + uint32_t slotNumber, bool deconfigure); +extern void evaluateContext(XHCIController *controller, void *inputContext, + uint32_t slotNumber); + +extern uint32_t requestSlotIndex(XHCIController *controller); + +extern void *usbGetDeviceDescriptor(XHCIController *controller, + XHCIInputContext *inputContext, + TrbRing *ring, uint32_t slotIndex, + uint32_t value, uint32_t index, + void *buffer); + +extern TrbRing *createTRB(XHCIController *controller, + XHCIInputContext *inputContext, uint32_t slotIndex); + +#endif diff --git a/src/userland/usb/xhci/trbRing.c b/src/userland/usb/xhci/trbRing.c index a31e28d..9276529 100644 --- a/src/userland/usb/xhci/trbRing.c +++ b/src/userland/usb/xhci/trbRing.c @@ -46,3 +46,16 @@ ring->trbs[ring->size - 1].dataLow = U32(ring->physical); ring->trbs[ring->size - 1].control |= 1 << 1 | COMMAND_TYPE(6); } + +TrbRing *createTRB(XHCIController *controller, XHCIInputContext *inputContext, + uint32_t slotIndex) { + TrbRing *ring = malloc(sizeof(TrbRing)); + setupTrbRing(ring, 256); + inputContext->deviceContext.endpoints[0].transferDequeuePointerLow = + U32(ring->physical) | 1; + inputContext->deviceContext.endpoints[0].transferDequeuePointerHigh = 0; + XHCIDevice *device = malloc(sizeof(XHCIDevice)); + controller->deviceContextBaseAddressArray[slotIndex] = + U32(getPhysicalAddress((void *)device)); + return ring; +}