diff --git a/src/userland/usb/include/usb.h b/src/userland/usb/include/usb.h index 5950ade..66fc782 100644 --- a/src/userland/usb/include/usb.h +++ b/src/userland/usb/include/usb.h @@ -188,4 +188,12 @@ XHCIInputContext *inputContexts[32]; } XHCIController; +typedef struct { + XHCIController *controller; + uint32_t slotIndex, portIndex; + XHCIInputContext *inputContext; + XHCIPort *port; + TrbRing *controlRing; +} SlotXHCI; + #endif diff --git a/src/userland/usb/include/usb.h b/src/userland/usb/include/usb.h index 5950ade..66fc782 100644 --- a/src/userland/usb/include/usb.h +++ b/src/userland/usb/include/usb.h @@ -188,4 +188,12 @@ XHCIInputContext *inputContexts[32]; } XHCIController; +typedef struct { + XHCIController *controller; + uint32_t slotIndex, portIndex; + XHCIInputContext *inputContext; + XHCIPort *port; + TrbRing *controlRing; +} SlotXHCI; + #endif diff --git a/src/userland/usb/main.c b/src/userland/usb/main.c index e6b04c0..5875fee 100644 --- a/src/userland/usb/main.c +++ b/src/userland/usb/main.c @@ -157,16 +157,15 @@ return controller; } -XHCIInputContext *createInputContext(XHCIController *controller, XHCIPort *port, - uint32_t portIndex, uint32_t slotIndex) { +XHCIInputContext *createInputContext(SlotXHCI *slot) { XHCIInputContext *inputContext = requestMemory(1, 0, 0); inputContext->inputControl.addContextFlags = 3; - inputContext->deviceContext.slot.rootHubPort = portIndex; + inputContext->deviceContext.slot.rootHubPort = slot->portIndex; inputContext->deviceContext.slot.interrupterTarget = 0; inputContext->deviceContext.slot.multiTT = 0; inputContext->deviceContext.slot.deviceAddress = 0; inputContext->deviceContext.slot.contextEntryCount = 1; - inputContext->deviceContext.slot.speed = (port->status >> 10) & 3; + inputContext->deviceContext.slot.speed = (slot->port->status >> 10) & 3; inputContext->deviceContext.endpoints[0].endpointType = 4; inputContext->deviceContext.endpoints[0].maxPacketSize = 8; // TODO inputContext->deviceContext.endpoints[0].maxBurstSize = 0; @@ -178,11 +177,9 @@ return inputContext; } -char *usbReadString(XHCIController *controller, XHCIInputContext *inputContext, - TrbRing *ring, uint32_t slotIndex, uint32_t language, +char *usbReadString(SlotXHCI *slot, uint32_t language, uint32_t stringDescriptor, void *buffer) { - usbGetDeviceDescriptor(controller, inputContext, ring, slotIndex, - 3 << 8 | stringDescriptor, language, buffer); + usbGetDeviceDescriptor(slot, 3 << 8 | stringDescriptor, language, buffer); uint32_t length = ((*(uint8_t *)buffer) - 2) / 2; char *string = malloc(length); for (uint32_t i = 0; i < length; i++) { @@ -192,49 +189,47 @@ } void resetPort(XHCIController *controller, uint32_t portIndex) { - XHCIPort *port = &controller->operational->ports[portIndex - 1]; + SlotXHCI *slot = malloc(sizeof(SlotXHCI)); + slot->portIndex = portIndex; + slot->controller = controller; + slot->slotIndex = requestSlotIndex(controller); + slot->port = &controller->operational->ports[portIndex - 1]; + printf("port %i: connected, resetting and setting it up now ...\n", portIndex - 1); - port->status |= 1 << 4; - await(serviceId, syscall(SYS_CREATE_EVENT, portIndex << 24, 0, 0, 0)); - if (!(port->status & 1 << 1)) { + slot->port->status |= 1 << 4; + await(serviceId, syscall(SYS_CREATE_EVENT, slot->portIndex << 24, 0, 0, 0)); + if (!(slot->port->status & 1 << 1)) { return printf("port %i reset could not be done, aborting\n", portIndex - 1); } - 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); + slot->slotIndex); + slot->inputContext = createInputContext(slot); + slot->controlRing = createSlotTRB(slot); + slot->controller->deviceContextBaseAddressArray[slot->slotIndex] = + U32(getPhysicalAddress(malloc(sizeof(XHCIDevice)))); printf("port %i: addressing slot\n", portIndex - 1); - addressDevice(controller, - getPhysicalAddress((void *)&inputContext->inputControl), - slotIndex, true); + addressDevice(slot, true); void *buffer = requestMemory(1, 0, 0); UsbDeviceDescriptor *descriptor = malloc(sizeof(UsbDeviceDescriptor)); - usbGetDeviceDescriptor(controller, inputContext, ring, slotIndex, 1 << 8, 0, - buffer); + usbGetDeviceDescriptor(slot, 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, descriptor->deviceClass, descriptor->deviceSubclass, descriptor->deviceProtocol, descriptor->maxPacketSize); - inputContext->deviceContext.endpoints[0].maxPacketSize = + slot->inputContext->deviceContext.endpoints[0].maxPacketSize = descriptor->maxPacketSize == 9 ? 512 : descriptor->maxPacketSize; - usbGetDeviceDescriptor(controller, inputContext, ring, slotIndex, 3 << 8, 0, - buffer); + usbGetDeviceDescriptor(slot, 3 << 8, 0, buffer); uint32_t language = *((uint16_t *)(buffer + 2)); - char *manufacturer = - 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); + char *manufacturer = usbReadString( + slot, language, descriptor->manufacturerStringDescriptor, buffer); + char *device = usbReadString(slot, language, + descriptor->deviceStringDescriptor, buffer); + char *serial = usbReadString( + slot, language, descriptor->serialNumberStringDescriptor, buffer); printf("manufacturer: %s, device: %s, serial: %s\n", manufacturer, device, serial); printf("--------\n"); @@ -289,7 +284,6 @@ continue; } resetPort(controller, i + 1); - sleep(5000); } return; } diff --git a/src/userland/usb/include/usb.h b/src/userland/usb/include/usb.h index 5950ade..66fc782 100644 --- a/src/userland/usb/include/usb.h +++ b/src/userland/usb/include/usb.h @@ -188,4 +188,12 @@ XHCIInputContext *inputContexts[32]; } XHCIController; +typedef struct { + XHCIController *controller; + uint32_t slotIndex, portIndex; + XHCIInputContext *inputContext; + XHCIPort *port; + TrbRing *controlRing; +} SlotXHCI; + #endif diff --git a/src/userland/usb/main.c b/src/userland/usb/main.c index e6b04c0..5875fee 100644 --- a/src/userland/usb/main.c +++ b/src/userland/usb/main.c @@ -157,16 +157,15 @@ return controller; } -XHCIInputContext *createInputContext(XHCIController *controller, XHCIPort *port, - uint32_t portIndex, uint32_t slotIndex) { +XHCIInputContext *createInputContext(SlotXHCI *slot) { XHCIInputContext *inputContext = requestMemory(1, 0, 0); inputContext->inputControl.addContextFlags = 3; - inputContext->deviceContext.slot.rootHubPort = portIndex; + inputContext->deviceContext.slot.rootHubPort = slot->portIndex; inputContext->deviceContext.slot.interrupterTarget = 0; inputContext->deviceContext.slot.multiTT = 0; inputContext->deviceContext.slot.deviceAddress = 0; inputContext->deviceContext.slot.contextEntryCount = 1; - inputContext->deviceContext.slot.speed = (port->status >> 10) & 3; + inputContext->deviceContext.slot.speed = (slot->port->status >> 10) & 3; inputContext->deviceContext.endpoints[0].endpointType = 4; inputContext->deviceContext.endpoints[0].maxPacketSize = 8; // TODO inputContext->deviceContext.endpoints[0].maxBurstSize = 0; @@ -178,11 +177,9 @@ return inputContext; } -char *usbReadString(XHCIController *controller, XHCIInputContext *inputContext, - TrbRing *ring, uint32_t slotIndex, uint32_t language, +char *usbReadString(SlotXHCI *slot, uint32_t language, uint32_t stringDescriptor, void *buffer) { - usbGetDeviceDescriptor(controller, inputContext, ring, slotIndex, - 3 << 8 | stringDescriptor, language, buffer); + usbGetDeviceDescriptor(slot, 3 << 8 | stringDescriptor, language, buffer); uint32_t length = ((*(uint8_t *)buffer) - 2) / 2; char *string = malloc(length); for (uint32_t i = 0; i < length; i++) { @@ -192,49 +189,47 @@ } void resetPort(XHCIController *controller, uint32_t portIndex) { - XHCIPort *port = &controller->operational->ports[portIndex - 1]; + SlotXHCI *slot = malloc(sizeof(SlotXHCI)); + slot->portIndex = portIndex; + slot->controller = controller; + slot->slotIndex = requestSlotIndex(controller); + slot->port = &controller->operational->ports[portIndex - 1]; + printf("port %i: connected, resetting and setting it up now ...\n", portIndex - 1); - port->status |= 1 << 4; - await(serviceId, syscall(SYS_CREATE_EVENT, portIndex << 24, 0, 0, 0)); - if (!(port->status & 1 << 1)) { + slot->port->status |= 1 << 4; + await(serviceId, syscall(SYS_CREATE_EVENT, slot->portIndex << 24, 0, 0, 0)); + if (!(slot->port->status & 1 << 1)) { return printf("port %i reset could not be done, aborting\n", portIndex - 1); } - 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); + slot->slotIndex); + slot->inputContext = createInputContext(slot); + slot->controlRing = createSlotTRB(slot); + slot->controller->deviceContextBaseAddressArray[slot->slotIndex] = + U32(getPhysicalAddress(malloc(sizeof(XHCIDevice)))); printf("port %i: addressing slot\n", portIndex - 1); - addressDevice(controller, - getPhysicalAddress((void *)&inputContext->inputControl), - slotIndex, true); + addressDevice(slot, true); void *buffer = requestMemory(1, 0, 0); UsbDeviceDescriptor *descriptor = malloc(sizeof(UsbDeviceDescriptor)); - usbGetDeviceDescriptor(controller, inputContext, ring, slotIndex, 1 << 8, 0, - buffer); + usbGetDeviceDescriptor(slot, 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, descriptor->deviceClass, descriptor->deviceSubclass, descriptor->deviceProtocol, descriptor->maxPacketSize); - inputContext->deviceContext.endpoints[0].maxPacketSize = + slot->inputContext->deviceContext.endpoints[0].maxPacketSize = descriptor->maxPacketSize == 9 ? 512 : descriptor->maxPacketSize; - usbGetDeviceDescriptor(controller, inputContext, ring, slotIndex, 3 << 8, 0, - buffer); + usbGetDeviceDescriptor(slot, 3 << 8, 0, buffer); uint32_t language = *((uint16_t *)(buffer + 2)); - char *manufacturer = - 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); + char *manufacturer = usbReadString( + slot, language, descriptor->manufacturerStringDescriptor, buffer); + char *device = usbReadString(slot, language, + descriptor->deviceStringDescriptor, buffer); + char *serial = usbReadString( + slot, language, descriptor->serialNumberStringDescriptor, buffer); printf("manufacturer: %s, device: %s, serial: %s\n", manufacturer, device, serial); printf("--------\n"); @@ -289,7 +284,6 @@ continue; } resetPort(controller, i + 1); - sleep(5000); } return; } diff --git a/src/userland/usb/xhci/commands.c b/src/userland/usb/xhci/commands.c index 8bdbf64..62455b5 100644 --- a/src/userland/usb/xhci/commands.c +++ b/src/userland/usb/xhci/commands.c @@ -18,11 +18,13 @@ return PTR(await(serviceId, eventId)); } -void addressDevice(XHCIController *controller, void *inputContext, - uint32_t slotNumber, bool BSR) { +void addressDevice(SlotXHCI *slot, bool BSR) { uint32_t control = - COMMAND_TYPE(11) | COMMAND_SLOT_ID(slotNumber) | COMMAND_BSR(BSR); - xhciCommand(controller, U32(inputContext), 0, 0, control); + COMMAND_TYPE(11) | COMMAND_SLOT_ID(slot->slotIndex) | COMMAND_BSR(BSR); + xhciCommand( + slot->controller, + U32(getPhysicalAddress((void *)&slot->inputContext->inputControl)), 0, + 0, control); } void configureEndpoint(XHCIController *controller, void *inputContext, @@ -42,9 +44,7 @@ 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 *usbGetDeviceDescriptor(SlotXHCI *slot, uint32_t value, uint32_t index, void *buffer) { XHCISetupStageTRB setup = {0}; setup.requestType = 0x80; @@ -72,11 +72,12 @@ status.interruptOnCompletion = 1; status.type = 4; - enqueueCommand(ring, (void *)&setup); - enqueueCommand(ring, (void *)&data); - uint32_t commandAddress = U32(enqueueCommand(ring, (void *)&status)); + enqueueCommand(slot->controlRing, (void *)&setup); + enqueueCommand(slot->controlRing, (void *)&data); + uint32_t commandAddress = + U32(enqueueCommand(slot->controlRing, (void *)&status)); uint32_t eventId = syscall(SYS_CREATE_EVENT, commandAddress, 0, 0, 0); - controller->doorbells[slotIndex] = 1; + slot->controller->doorbells[slot->slotIndex] = 1; CommandCompletionEvent *completionEvent = PTR(await(serviceId, eventId)); return buffer; } diff --git a/src/userland/usb/include/usb.h b/src/userland/usb/include/usb.h index 5950ade..66fc782 100644 --- a/src/userland/usb/include/usb.h +++ b/src/userland/usb/include/usb.h @@ -188,4 +188,12 @@ XHCIInputContext *inputContexts[32]; } XHCIController; +typedef struct { + XHCIController *controller; + uint32_t slotIndex, portIndex; + XHCIInputContext *inputContext; + XHCIPort *port; + TrbRing *controlRing; +} SlotXHCI; + #endif diff --git a/src/userland/usb/main.c b/src/userland/usb/main.c index e6b04c0..5875fee 100644 --- a/src/userland/usb/main.c +++ b/src/userland/usb/main.c @@ -157,16 +157,15 @@ return controller; } -XHCIInputContext *createInputContext(XHCIController *controller, XHCIPort *port, - uint32_t portIndex, uint32_t slotIndex) { +XHCIInputContext *createInputContext(SlotXHCI *slot) { XHCIInputContext *inputContext = requestMemory(1, 0, 0); inputContext->inputControl.addContextFlags = 3; - inputContext->deviceContext.slot.rootHubPort = portIndex; + inputContext->deviceContext.slot.rootHubPort = slot->portIndex; inputContext->deviceContext.slot.interrupterTarget = 0; inputContext->deviceContext.slot.multiTT = 0; inputContext->deviceContext.slot.deviceAddress = 0; inputContext->deviceContext.slot.contextEntryCount = 1; - inputContext->deviceContext.slot.speed = (port->status >> 10) & 3; + inputContext->deviceContext.slot.speed = (slot->port->status >> 10) & 3; inputContext->deviceContext.endpoints[0].endpointType = 4; inputContext->deviceContext.endpoints[0].maxPacketSize = 8; // TODO inputContext->deviceContext.endpoints[0].maxBurstSize = 0; @@ -178,11 +177,9 @@ return inputContext; } -char *usbReadString(XHCIController *controller, XHCIInputContext *inputContext, - TrbRing *ring, uint32_t slotIndex, uint32_t language, +char *usbReadString(SlotXHCI *slot, uint32_t language, uint32_t stringDescriptor, void *buffer) { - usbGetDeviceDescriptor(controller, inputContext, ring, slotIndex, - 3 << 8 | stringDescriptor, language, buffer); + usbGetDeviceDescriptor(slot, 3 << 8 | stringDescriptor, language, buffer); uint32_t length = ((*(uint8_t *)buffer) - 2) / 2; char *string = malloc(length); for (uint32_t i = 0; i < length; i++) { @@ -192,49 +189,47 @@ } void resetPort(XHCIController *controller, uint32_t portIndex) { - XHCIPort *port = &controller->operational->ports[portIndex - 1]; + SlotXHCI *slot = malloc(sizeof(SlotXHCI)); + slot->portIndex = portIndex; + slot->controller = controller; + slot->slotIndex = requestSlotIndex(controller); + slot->port = &controller->operational->ports[portIndex - 1]; + printf("port %i: connected, resetting and setting it up now ...\n", portIndex - 1); - port->status |= 1 << 4; - await(serviceId, syscall(SYS_CREATE_EVENT, portIndex << 24, 0, 0, 0)); - if (!(port->status & 1 << 1)) { + slot->port->status |= 1 << 4; + await(serviceId, syscall(SYS_CREATE_EVENT, slot->portIndex << 24, 0, 0, 0)); + if (!(slot->port->status & 1 << 1)) { return printf("port %i reset could not be done, aborting\n", portIndex - 1); } - 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); + slot->slotIndex); + slot->inputContext = createInputContext(slot); + slot->controlRing = createSlotTRB(slot); + slot->controller->deviceContextBaseAddressArray[slot->slotIndex] = + U32(getPhysicalAddress(malloc(sizeof(XHCIDevice)))); printf("port %i: addressing slot\n", portIndex - 1); - addressDevice(controller, - getPhysicalAddress((void *)&inputContext->inputControl), - slotIndex, true); + addressDevice(slot, true); void *buffer = requestMemory(1, 0, 0); UsbDeviceDescriptor *descriptor = malloc(sizeof(UsbDeviceDescriptor)); - usbGetDeviceDescriptor(controller, inputContext, ring, slotIndex, 1 << 8, 0, - buffer); + usbGetDeviceDescriptor(slot, 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, descriptor->deviceClass, descriptor->deviceSubclass, descriptor->deviceProtocol, descriptor->maxPacketSize); - inputContext->deviceContext.endpoints[0].maxPacketSize = + slot->inputContext->deviceContext.endpoints[0].maxPacketSize = descriptor->maxPacketSize == 9 ? 512 : descriptor->maxPacketSize; - usbGetDeviceDescriptor(controller, inputContext, ring, slotIndex, 3 << 8, 0, - buffer); + usbGetDeviceDescriptor(slot, 3 << 8, 0, buffer); uint32_t language = *((uint16_t *)(buffer + 2)); - char *manufacturer = - 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); + char *manufacturer = usbReadString( + slot, language, descriptor->manufacturerStringDescriptor, buffer); + char *device = usbReadString(slot, language, + descriptor->deviceStringDescriptor, buffer); + char *serial = usbReadString( + slot, language, descriptor->serialNumberStringDescriptor, buffer); printf("manufacturer: %s, device: %s, serial: %s\n", manufacturer, device, serial); printf("--------\n"); @@ -289,7 +284,6 @@ continue; } resetPort(controller, i + 1); - sleep(5000); } return; } diff --git a/src/userland/usb/xhci/commands.c b/src/userland/usb/xhci/commands.c index 8bdbf64..62455b5 100644 --- a/src/userland/usb/xhci/commands.c +++ b/src/userland/usb/xhci/commands.c @@ -18,11 +18,13 @@ return PTR(await(serviceId, eventId)); } -void addressDevice(XHCIController *controller, void *inputContext, - uint32_t slotNumber, bool BSR) { +void addressDevice(SlotXHCI *slot, bool BSR) { uint32_t control = - COMMAND_TYPE(11) | COMMAND_SLOT_ID(slotNumber) | COMMAND_BSR(BSR); - xhciCommand(controller, U32(inputContext), 0, 0, control); + COMMAND_TYPE(11) | COMMAND_SLOT_ID(slot->slotIndex) | COMMAND_BSR(BSR); + xhciCommand( + slot->controller, + U32(getPhysicalAddress((void *)&slot->inputContext->inputControl)), 0, + 0, control); } void configureEndpoint(XHCIController *controller, void *inputContext, @@ -42,9 +44,7 @@ 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 *usbGetDeviceDescriptor(SlotXHCI *slot, uint32_t value, uint32_t index, void *buffer) { XHCISetupStageTRB setup = {0}; setup.requestType = 0x80; @@ -72,11 +72,12 @@ status.interruptOnCompletion = 1; status.type = 4; - enqueueCommand(ring, (void *)&setup); - enqueueCommand(ring, (void *)&data); - uint32_t commandAddress = U32(enqueueCommand(ring, (void *)&status)); + enqueueCommand(slot->controlRing, (void *)&setup); + enqueueCommand(slot->controlRing, (void *)&data); + uint32_t commandAddress = + U32(enqueueCommand(slot->controlRing, (void *)&status)); uint32_t eventId = syscall(SYS_CREATE_EVENT, commandAddress, 0, 0, 0); - controller->doorbells[slotIndex] = 1; + slot->controller->doorbells[slot->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 index 00d497c..6c8a694 100644 --- a/src/userland/usb/xhci/commands.h +++ b/src/userland/usb/xhci/commands.h @@ -4,8 +4,7 @@ #include #include -extern void addressDevice(XHCIController *controller, void *inputContext, - uint32_t slotNumber, bool BSR); +extern void addressDevice(SlotXHCI *slot, bool BSR); extern void configureEndpoint(XHCIController *controller, void *inputContext, uint32_t slotNumber, bool deconfigure); extern void evaluateContext(XHCIController *controller, void *inputContext, @@ -13,13 +12,9 @@ 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 void *usbGetDeviceDescriptor(SlotXHCI *slot, uint32_t value, + uint32_t index, void *buffer); -extern TrbRing *createTRB(XHCIController *controller, - XHCIInputContext *inputContext, uint32_t slotIndex); +extern TrbRing *createSlotTRB(SlotXHCI *slot); #endif diff --git a/src/userland/usb/include/usb.h b/src/userland/usb/include/usb.h index 5950ade..66fc782 100644 --- a/src/userland/usb/include/usb.h +++ b/src/userland/usb/include/usb.h @@ -188,4 +188,12 @@ XHCIInputContext *inputContexts[32]; } XHCIController; +typedef struct { + XHCIController *controller; + uint32_t slotIndex, portIndex; + XHCIInputContext *inputContext; + XHCIPort *port; + TrbRing *controlRing; +} SlotXHCI; + #endif diff --git a/src/userland/usb/main.c b/src/userland/usb/main.c index e6b04c0..5875fee 100644 --- a/src/userland/usb/main.c +++ b/src/userland/usb/main.c @@ -157,16 +157,15 @@ return controller; } -XHCIInputContext *createInputContext(XHCIController *controller, XHCIPort *port, - uint32_t portIndex, uint32_t slotIndex) { +XHCIInputContext *createInputContext(SlotXHCI *slot) { XHCIInputContext *inputContext = requestMemory(1, 0, 0); inputContext->inputControl.addContextFlags = 3; - inputContext->deviceContext.slot.rootHubPort = portIndex; + inputContext->deviceContext.slot.rootHubPort = slot->portIndex; inputContext->deviceContext.slot.interrupterTarget = 0; inputContext->deviceContext.slot.multiTT = 0; inputContext->deviceContext.slot.deviceAddress = 0; inputContext->deviceContext.slot.contextEntryCount = 1; - inputContext->deviceContext.slot.speed = (port->status >> 10) & 3; + inputContext->deviceContext.slot.speed = (slot->port->status >> 10) & 3; inputContext->deviceContext.endpoints[0].endpointType = 4; inputContext->deviceContext.endpoints[0].maxPacketSize = 8; // TODO inputContext->deviceContext.endpoints[0].maxBurstSize = 0; @@ -178,11 +177,9 @@ return inputContext; } -char *usbReadString(XHCIController *controller, XHCIInputContext *inputContext, - TrbRing *ring, uint32_t slotIndex, uint32_t language, +char *usbReadString(SlotXHCI *slot, uint32_t language, uint32_t stringDescriptor, void *buffer) { - usbGetDeviceDescriptor(controller, inputContext, ring, slotIndex, - 3 << 8 | stringDescriptor, language, buffer); + usbGetDeviceDescriptor(slot, 3 << 8 | stringDescriptor, language, buffer); uint32_t length = ((*(uint8_t *)buffer) - 2) / 2; char *string = malloc(length); for (uint32_t i = 0; i < length; i++) { @@ -192,49 +189,47 @@ } void resetPort(XHCIController *controller, uint32_t portIndex) { - XHCIPort *port = &controller->operational->ports[portIndex - 1]; + SlotXHCI *slot = malloc(sizeof(SlotXHCI)); + slot->portIndex = portIndex; + slot->controller = controller; + slot->slotIndex = requestSlotIndex(controller); + slot->port = &controller->operational->ports[portIndex - 1]; + printf("port %i: connected, resetting and setting it up now ...\n", portIndex - 1); - port->status |= 1 << 4; - await(serviceId, syscall(SYS_CREATE_EVENT, portIndex << 24, 0, 0, 0)); - if (!(port->status & 1 << 1)) { + slot->port->status |= 1 << 4; + await(serviceId, syscall(SYS_CREATE_EVENT, slot->portIndex << 24, 0, 0, 0)); + if (!(slot->port->status & 1 << 1)) { return printf("port %i reset could not be done, aborting\n", portIndex - 1); } - 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); + slot->slotIndex); + slot->inputContext = createInputContext(slot); + slot->controlRing = createSlotTRB(slot); + slot->controller->deviceContextBaseAddressArray[slot->slotIndex] = + U32(getPhysicalAddress(malloc(sizeof(XHCIDevice)))); printf("port %i: addressing slot\n", portIndex - 1); - addressDevice(controller, - getPhysicalAddress((void *)&inputContext->inputControl), - slotIndex, true); + addressDevice(slot, true); void *buffer = requestMemory(1, 0, 0); UsbDeviceDescriptor *descriptor = malloc(sizeof(UsbDeviceDescriptor)); - usbGetDeviceDescriptor(controller, inputContext, ring, slotIndex, 1 << 8, 0, - buffer); + usbGetDeviceDescriptor(slot, 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, descriptor->deviceClass, descriptor->deviceSubclass, descriptor->deviceProtocol, descriptor->maxPacketSize); - inputContext->deviceContext.endpoints[0].maxPacketSize = + slot->inputContext->deviceContext.endpoints[0].maxPacketSize = descriptor->maxPacketSize == 9 ? 512 : descriptor->maxPacketSize; - usbGetDeviceDescriptor(controller, inputContext, ring, slotIndex, 3 << 8, 0, - buffer); + usbGetDeviceDescriptor(slot, 3 << 8, 0, buffer); uint32_t language = *((uint16_t *)(buffer + 2)); - char *manufacturer = - 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); + char *manufacturer = usbReadString( + slot, language, descriptor->manufacturerStringDescriptor, buffer); + char *device = usbReadString(slot, language, + descriptor->deviceStringDescriptor, buffer); + char *serial = usbReadString( + slot, language, descriptor->serialNumberStringDescriptor, buffer); printf("manufacturer: %s, device: %s, serial: %s\n", manufacturer, device, serial); printf("--------\n"); @@ -289,7 +284,6 @@ continue; } resetPort(controller, i + 1); - sleep(5000); } return; } diff --git a/src/userland/usb/xhci/commands.c b/src/userland/usb/xhci/commands.c index 8bdbf64..62455b5 100644 --- a/src/userland/usb/xhci/commands.c +++ b/src/userland/usb/xhci/commands.c @@ -18,11 +18,13 @@ return PTR(await(serviceId, eventId)); } -void addressDevice(XHCIController *controller, void *inputContext, - uint32_t slotNumber, bool BSR) { +void addressDevice(SlotXHCI *slot, bool BSR) { uint32_t control = - COMMAND_TYPE(11) | COMMAND_SLOT_ID(slotNumber) | COMMAND_BSR(BSR); - xhciCommand(controller, U32(inputContext), 0, 0, control); + COMMAND_TYPE(11) | COMMAND_SLOT_ID(slot->slotIndex) | COMMAND_BSR(BSR); + xhciCommand( + slot->controller, + U32(getPhysicalAddress((void *)&slot->inputContext->inputControl)), 0, + 0, control); } void configureEndpoint(XHCIController *controller, void *inputContext, @@ -42,9 +44,7 @@ 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 *usbGetDeviceDescriptor(SlotXHCI *slot, uint32_t value, uint32_t index, void *buffer) { XHCISetupStageTRB setup = {0}; setup.requestType = 0x80; @@ -72,11 +72,12 @@ status.interruptOnCompletion = 1; status.type = 4; - enqueueCommand(ring, (void *)&setup); - enqueueCommand(ring, (void *)&data); - uint32_t commandAddress = U32(enqueueCommand(ring, (void *)&status)); + enqueueCommand(slot->controlRing, (void *)&setup); + enqueueCommand(slot->controlRing, (void *)&data); + uint32_t commandAddress = + U32(enqueueCommand(slot->controlRing, (void *)&status)); uint32_t eventId = syscall(SYS_CREATE_EVENT, commandAddress, 0, 0, 0); - controller->doorbells[slotIndex] = 1; + slot->controller->doorbells[slot->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 index 00d497c..6c8a694 100644 --- a/src/userland/usb/xhci/commands.h +++ b/src/userland/usb/xhci/commands.h @@ -4,8 +4,7 @@ #include #include -extern void addressDevice(XHCIController *controller, void *inputContext, - uint32_t slotNumber, bool BSR); +extern void addressDevice(SlotXHCI *slot, bool BSR); extern void configureEndpoint(XHCIController *controller, void *inputContext, uint32_t slotNumber, bool deconfigure); extern void evaluateContext(XHCIController *controller, void *inputContext, @@ -13,13 +12,9 @@ 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 void *usbGetDeviceDescriptor(SlotXHCI *slot, uint32_t value, + uint32_t index, void *buffer); -extern TrbRing *createTRB(XHCIController *controller, - XHCIInputContext *inputContext, uint32_t slotIndex); +extern TrbRing *createSlotTRB(SlotXHCI *slot); #endif diff --git a/src/userland/usb/xhci/trbRing.c b/src/userland/usb/xhci/trbRing.c index 9276529..b487be8 100644 --- a/src/userland/usb/xhci/trbRing.c +++ b/src/userland/usb/xhci/trbRing.c @@ -47,15 +47,12 @@ ring->trbs[ring->size - 1].control |= 1 << 1 | COMMAND_TYPE(6); } -TrbRing *createTRB(XHCIController *controller, XHCIInputContext *inputContext, - uint32_t slotIndex) { +TrbRing *createSlotTRB(SlotXHCI *slot) { TrbRing *ring = malloc(sizeof(TrbRing)); setupTrbRing(ring, 256); - inputContext->deviceContext.endpoints[0].transferDequeuePointerLow = + slot->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)); + slot->inputContext->deviceContext.endpoints[0].transferDequeuePointerHigh = + 0; return ring; }