diff --git a/src/include/hlib.h b/src/include/hlib.h index d38c59d..dac6387 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -75,4 +75,21 @@ extern void _listAdd(void *(malloc)(uint32_t), ListElement **list, void *data); #define listAdd(list, data) _listAdd(malloc, list, data) +#define REQUEST(functionName, service, function) \ + uint32_t functionName(uint32_t data1, uint32_t data2) { \ + static uint32_t serviceId, functionId, initialized = false; \ + if (!initialized) { \ + while (!serviceId) { \ + serviceId = getService(service); \ + serviceId = getService(service); \ + } \ + while (!functionId) { \ + functionId = getFunction(serviceId, function); \ + functionId = getFunction(serviceId, function); \ + } \ + initialized = true; \ + } \ + return request(serviceId, functionId, data1, data2); \ + } + #endif diff --git a/src/include/hlib.h b/src/include/hlib.h index d38c59d..dac6387 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -75,4 +75,21 @@ extern void _listAdd(void *(malloc)(uint32_t), ListElement **list, void *data); #define listAdd(list, data) _listAdd(malloc, list, data) +#define REQUEST(functionName, service, function) \ + uint32_t functionName(uint32_t data1, uint32_t data2) { \ + static uint32_t serviceId, functionId, initialized = false; \ + if (!initialized) { \ + while (!serviceId) { \ + serviceId = getService(service); \ + serviceId = getService(service); \ + } \ + while (!functionId) { \ + functionId = getFunction(serviceId, function); \ + functionId = getFunction(serviceId, function); \ + } \ + initialized = true; \ + } \ + return request(serviceId, functionId, data1, data2); \ + } + #endif diff --git a/src/userland/usb/include/usb.h b/src/userland/usb/include/usb.h index 009e306..0b26f5f 100644 --- a/src/userland/usb/include/usb.h +++ b/src/userland/usb/include/usb.h @@ -5,150 +5,7 @@ #define OFFSET(ptr, off) (((void *)(ptr)) + (off)) -extern uint32_t i; - -typedef volatile struct { - uint8_t capabilitiesLength; - uint8_t reserved; - uint16_t interfaceVersion; - uint32_t structuralParameters[3]; - uint32_t capabilityParameter1; - uint32_t doorbellOffset; - uint32_t runtimeOffset; - uint32_t capabilityParameter2; -} __attribute__((packed)) XHCICapabilities; - -typedef volatile struct { - uint32_t status; - uint32_t powerStatus; - uint32_t linkInfo; - uint32_t hardwareLPM; -} __attribute__((packed)) XHCIPort; - -typedef volatile struct { - uint32_t command; - uint32_t status; - uint32_t pageSize; - uint32_t reserved[2]; - uint32_t notification; - uint32_t commandRingControl[2]; - uint32_t reserved1[4]; - uint32_t deviceContextBaseAddressArray[2]; - uint32_t config; - uint8_t reserved2[964]; - XHCIPort ports[256]; -} __attribute__((packed)) XHCIOperational; - -typedef volatile struct { - uint32_t dataLow; - uint32_t dataHigh; - uint32_t status; - uint32_t control; -} __attribute__((packed)) XHCITRB; - -#define COMMAND_TYPE(x) (x << 10) -#define COMMAND_CYCLE(x) (x) -#define COMMAND_BSR(x) (x << 9) -#define COMMAND_SLOT_ID(x) (x << 24) - -typedef volatile struct { - uint32_t commandLow; - uint32_t commandHigh; - uint32_t completionParameter : 24, completionCode : 8; - uint32_t cycle : 1, reserved : 9, type : 6, vfId : 8, slotId : 8; -} __attribute__((packed)) CommandCompletionEvent; - -typedef volatile struct { - uint32_t management; - uint32_t moderationCounter : 16, moderationInterval : 16; - uint32_t eventRingSegmentTableSize; // MAX 16 bit - uint32_t reserved2; - uint32_t eventRingSegmentTableAddress[2]; - uint32_t eventRingDequeuePointer[2]; -} __attribute__((packed)) XHCIInterrupter; - -typedef volatile struct { - uint8_t microframeIndex; - uint8_t reserved[0x20 - 1]; - XHCIInterrupter interrupters[1023]; -} __attribute__((packed)) XHCIRuntime; - -typedef volatile struct { - uint32_t ringSegmentBaseAddress[2]; - uint16_t ringSegmentSize, reserved; - uint32_t reserved1; -} __attribute__((packed)) XHCIEventRingSegmentTableEntry; - -typedef volatile struct { - uint32_t requestType : 8, request : 8, value : 16; - uint32_t index : 16, length : 16; - - uint32_t transferLength : 17, // always 8 - reserved : 5, interrupterTarget : 10; - - uint32_t cycle : 1, reserved1 : 4, interruptOnCompletion : 1, - immediateData : 1, // here 1 - reserved2 : 3, type : 6, transferType : 2; -} __attribute__((packed)) XHCISetupStageTRB; - -typedef volatile struct { - uint32_t reserved[2]; - - uint32_t reserved1 : 22, interrupterTarget : 10; - - uint32_t cycle : 1, evaluateNext : 1, reserved2 : 2, chain : 1, - interruptOnCompletion : 1, reserved3 : 4, type : 6, inDirection : 1; -} __attribute__((packed)) XHCIStatusStageTRB; - -typedef volatile struct { - uint32_t dataBuffer[2]; - - uint32_t transferSize : 17, tdSize : 5, interrupterTarget : 10; - - uint32_t cycle : 1, evaluateNext : 1, interruptOnShortPacket : 1, - noSnoop : 1, chain : 1, interruptOnCompletion : 1, immediateData : 1, - reserved : 3, type : 6, inDirection : 1; -} __attribute__((packed)) XHCIDataStageTRB; - -typedef volatile struct { - uint32_t routeString : 20, speed : 4, reserved : 1, multiTT : 1, isHub : 1, - contextEntryCount : 5; - uint32_t maxLatency : 16, rootHubPort : 8, portCount : 8; - uint32_t parentHubSlotId : 8, partentPortNumber : 8, thinkTime : 2, - reserved1 : 4, interrupterTarget : 10; - uint32_t deviceAddress : 8, reserved2 : 19, slotState : 5; - uint32_t reserved3[4]; -} __attribute__((packed)) XHCISlotContext; - -typedef volatile struct { - uint32_t endpointState : 3, reserved : 5, multiplier : 2, - maxPrimaryStreams : 5, linearStreamArray : 1, interval : 8, - maxEISTPayloadHigh : 8; - uint32_t reserved1 : 1, errorCount : 2, endpointType : 3, reserved2 : 1, - hostInitiateDisable : 1, maxBurstSize : 8, maxPacketSize : 16; - uint32_t transferDequeuePointerLow; - uint32_t transferDequeuePointerHigh; - uint32_t averageTRBLength : 16, maxEISTPayloadLow : 16; - uint32_t reserved4[3]; -} __attribute__((packed)) XHCIEndpointContext; - -typedef volatile struct { - XHCISlotContext slot; - XHCIEndpointContext endpoints[32]; -} __attribute__((packed)) XHCIDevice; - -typedef volatile struct { - uint32_t dropContextFlags; - uint32_t addContextFlags; - uint32_t reserved[5]; - uint32_t configuration : 8, interfaceNumber : 8, AlternateSetting : 8, - reserved1 : 8; -} __attribute__((packed)) XHCIInputControl; - -typedef volatile struct { - XHCIInputControl inputControl; - XHCIDevice deviceContext; -} __attribute__((packed)) XHCIInputContext; +extern uint32_t serviceId; typedef volatile struct { uint8_t size; @@ -168,35 +25,6 @@ } __attribute__((packed)) UsbDeviceDescriptor; typedef struct { - XHCITRB *trbs, *physical; - uint32_t size, enqueue, dequeue; - bool cycle; -} TrbRing; - -typedef struct { - XHCICapabilities *capabilities; - XHCIOperational *operational; - XHCIRuntime *runtime; - TrbRing commands; - TrbRing events; - uint32_t pciDevice; - volatile uint32_t *doorbells; - XHCIEventRingSegmentTableEntry *eventRingSegmentTable, - *eventRingSegmentTablePhysical; - uint64_t *deviceContextBaseAddressArray; - uint32_t portCount; - XHCIInputContext *inputContexts[32]; -} XHCIController; - -typedef struct { - XHCIController *controller; - uint32_t slotIndex, portIndex; - XHCIInputContext *inputContext; - XHCIPort *port; - TrbRing *controlRing; -} SlotXHCI; - -typedef struct { uint32_t pciClass; void *(*initialize)(uint32_t, uint32_t, uint32_t); void (*getDeviceDescriptor)(void *, uint32_t, uint32_t, void *); diff --git a/src/include/hlib.h b/src/include/hlib.h index d38c59d..dac6387 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -75,4 +75,21 @@ extern void _listAdd(void *(malloc)(uint32_t), ListElement **list, void *data); #define listAdd(list, data) _listAdd(malloc, list, data) +#define REQUEST(functionName, service, function) \ + uint32_t functionName(uint32_t data1, uint32_t data2) { \ + static uint32_t serviceId, functionId, initialized = false; \ + if (!initialized) { \ + while (!serviceId) { \ + serviceId = getService(service); \ + serviceId = getService(service); \ + } \ + while (!functionId) { \ + functionId = getFunction(serviceId, function); \ + functionId = getFunction(serviceId, function); \ + } \ + initialized = true; \ + } \ + return request(serviceId, functionId, data1, data2); \ + } + #endif diff --git a/src/userland/usb/include/usb.h b/src/userland/usb/include/usb.h index 009e306..0b26f5f 100644 --- a/src/userland/usb/include/usb.h +++ b/src/userland/usb/include/usb.h @@ -5,150 +5,7 @@ #define OFFSET(ptr, off) (((void *)(ptr)) + (off)) -extern uint32_t i; - -typedef volatile struct { - uint8_t capabilitiesLength; - uint8_t reserved; - uint16_t interfaceVersion; - uint32_t structuralParameters[3]; - uint32_t capabilityParameter1; - uint32_t doorbellOffset; - uint32_t runtimeOffset; - uint32_t capabilityParameter2; -} __attribute__((packed)) XHCICapabilities; - -typedef volatile struct { - uint32_t status; - uint32_t powerStatus; - uint32_t linkInfo; - uint32_t hardwareLPM; -} __attribute__((packed)) XHCIPort; - -typedef volatile struct { - uint32_t command; - uint32_t status; - uint32_t pageSize; - uint32_t reserved[2]; - uint32_t notification; - uint32_t commandRingControl[2]; - uint32_t reserved1[4]; - uint32_t deviceContextBaseAddressArray[2]; - uint32_t config; - uint8_t reserved2[964]; - XHCIPort ports[256]; -} __attribute__((packed)) XHCIOperational; - -typedef volatile struct { - uint32_t dataLow; - uint32_t dataHigh; - uint32_t status; - uint32_t control; -} __attribute__((packed)) XHCITRB; - -#define COMMAND_TYPE(x) (x << 10) -#define COMMAND_CYCLE(x) (x) -#define COMMAND_BSR(x) (x << 9) -#define COMMAND_SLOT_ID(x) (x << 24) - -typedef volatile struct { - uint32_t commandLow; - uint32_t commandHigh; - uint32_t completionParameter : 24, completionCode : 8; - uint32_t cycle : 1, reserved : 9, type : 6, vfId : 8, slotId : 8; -} __attribute__((packed)) CommandCompletionEvent; - -typedef volatile struct { - uint32_t management; - uint32_t moderationCounter : 16, moderationInterval : 16; - uint32_t eventRingSegmentTableSize; // MAX 16 bit - uint32_t reserved2; - uint32_t eventRingSegmentTableAddress[2]; - uint32_t eventRingDequeuePointer[2]; -} __attribute__((packed)) XHCIInterrupter; - -typedef volatile struct { - uint8_t microframeIndex; - uint8_t reserved[0x20 - 1]; - XHCIInterrupter interrupters[1023]; -} __attribute__((packed)) XHCIRuntime; - -typedef volatile struct { - uint32_t ringSegmentBaseAddress[2]; - uint16_t ringSegmentSize, reserved; - uint32_t reserved1; -} __attribute__((packed)) XHCIEventRingSegmentTableEntry; - -typedef volatile struct { - uint32_t requestType : 8, request : 8, value : 16; - uint32_t index : 16, length : 16; - - uint32_t transferLength : 17, // always 8 - reserved : 5, interrupterTarget : 10; - - uint32_t cycle : 1, reserved1 : 4, interruptOnCompletion : 1, - immediateData : 1, // here 1 - reserved2 : 3, type : 6, transferType : 2; -} __attribute__((packed)) XHCISetupStageTRB; - -typedef volatile struct { - uint32_t reserved[2]; - - uint32_t reserved1 : 22, interrupterTarget : 10; - - uint32_t cycle : 1, evaluateNext : 1, reserved2 : 2, chain : 1, - interruptOnCompletion : 1, reserved3 : 4, type : 6, inDirection : 1; -} __attribute__((packed)) XHCIStatusStageTRB; - -typedef volatile struct { - uint32_t dataBuffer[2]; - - uint32_t transferSize : 17, tdSize : 5, interrupterTarget : 10; - - uint32_t cycle : 1, evaluateNext : 1, interruptOnShortPacket : 1, - noSnoop : 1, chain : 1, interruptOnCompletion : 1, immediateData : 1, - reserved : 3, type : 6, inDirection : 1; -} __attribute__((packed)) XHCIDataStageTRB; - -typedef volatile struct { - uint32_t routeString : 20, speed : 4, reserved : 1, multiTT : 1, isHub : 1, - contextEntryCount : 5; - uint32_t maxLatency : 16, rootHubPort : 8, portCount : 8; - uint32_t parentHubSlotId : 8, partentPortNumber : 8, thinkTime : 2, - reserved1 : 4, interrupterTarget : 10; - uint32_t deviceAddress : 8, reserved2 : 19, slotState : 5; - uint32_t reserved3[4]; -} __attribute__((packed)) XHCISlotContext; - -typedef volatile struct { - uint32_t endpointState : 3, reserved : 5, multiplier : 2, - maxPrimaryStreams : 5, linearStreamArray : 1, interval : 8, - maxEISTPayloadHigh : 8; - uint32_t reserved1 : 1, errorCount : 2, endpointType : 3, reserved2 : 1, - hostInitiateDisable : 1, maxBurstSize : 8, maxPacketSize : 16; - uint32_t transferDequeuePointerLow; - uint32_t transferDequeuePointerHigh; - uint32_t averageTRBLength : 16, maxEISTPayloadLow : 16; - uint32_t reserved4[3]; -} __attribute__((packed)) XHCIEndpointContext; - -typedef volatile struct { - XHCISlotContext slot; - XHCIEndpointContext endpoints[32]; -} __attribute__((packed)) XHCIDevice; - -typedef volatile struct { - uint32_t dropContextFlags; - uint32_t addContextFlags; - uint32_t reserved[5]; - uint32_t configuration : 8, interfaceNumber : 8, AlternateSetting : 8, - reserved1 : 8; -} __attribute__((packed)) XHCIInputControl; - -typedef volatile struct { - XHCIInputControl inputControl; - XHCIDevice deviceContext; -} __attribute__((packed)) XHCIInputContext; +extern uint32_t serviceId; typedef volatile struct { uint8_t size; @@ -168,35 +25,6 @@ } __attribute__((packed)) UsbDeviceDescriptor; typedef struct { - XHCITRB *trbs, *physical; - uint32_t size, enqueue, dequeue; - bool cycle; -} TrbRing; - -typedef struct { - XHCICapabilities *capabilities; - XHCIOperational *operational; - XHCIRuntime *runtime; - TrbRing commands; - TrbRing events; - uint32_t pciDevice; - volatile uint32_t *doorbells; - XHCIEventRingSegmentTableEntry *eventRingSegmentTable, - *eventRingSegmentTablePhysical; - uint64_t *deviceContextBaseAddressArray; - uint32_t portCount; - XHCIInputContext *inputContexts[32]; -} XHCIController; - -typedef struct { - XHCIController *controller; - uint32_t slotIndex, portIndex; - XHCIInputContext *inputContext; - XHCIPort *port; - TrbRing *controlRing; -} SlotXHCI; - -typedef struct { uint32_t pciClass; void *(*initialize)(uint32_t, uint32_t, uint32_t); void (*getDeviceDescriptor)(void *, uint32_t, uint32_t, void *); diff --git a/src/userland/usb/main.c b/src/userland/usb/main.c index 82637d1..cb58a2f 100644 --- a/src/userland/usb/main.c +++ b/src/userland/usb/main.c @@ -2,30 +2,10 @@ #include #include "../hlib/include/syscalls.h" -#include "xhci/commands.h" -#include "xhci/controller.h" -#include "xhci/trbRing.h" #include uint32_t serviceId; -#define REQUEST(functionName, service, function) \ - uint32_t functionName(uint32_t data1, uint32_t data2) { \ - static uint32_t serviceId, functionId, initialized = false; \ - if (!initialized) { \ - while (!serviceId) { \ - serviceId = getService(service); \ - serviceId = getService(service); \ - } \ - while (!functionId) { \ - functionId = getFunction(serviceId, function); \ - functionId = getFunction(serviceId, function); \ - } \ - initialized = true; \ - } \ - return request(serviceId, functionId, data1, data2); \ - } - REQUEST(getBaseAddress, "lspci", "getBaseAddress"); REQUEST(getDeviceClass, "lspci", "getDeviceClass"); REQUEST(enableBusMaster, "lspci", "enableBusMaster"); @@ -51,7 +31,7 @@ UsbDeviceDescriptor *descriptor = malloc(sizeof(UsbDeviceDescriptor)); slot->interface->getDeviceDescriptor(slot->data, 1 << 8, 0, buffer); memcpy(buffer, (void *)descriptor, sizeof(UsbDeviceDescriptor)); - printf("slot %i: usb version %x.%x\n", slot->portIndex, + printf("port %i: usb version %x.%x\n", slot->portIndex, descriptor->usbVersion >> 8, descriptor->usbVersion & 0xFF); printf("port %i: class: %i, subclass: %i, protocol %i, maxPacketSize: %i\n", slot->portIndex, descriptor->deviceClass, descriptor->deviceSubclass, diff --git a/src/include/hlib.h b/src/include/hlib.h index d38c59d..dac6387 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -75,4 +75,21 @@ extern void _listAdd(void *(malloc)(uint32_t), ListElement **list, void *data); #define listAdd(list, data) _listAdd(malloc, list, data) +#define REQUEST(functionName, service, function) \ + uint32_t functionName(uint32_t data1, uint32_t data2) { \ + static uint32_t serviceId, functionId, initialized = false; \ + if (!initialized) { \ + while (!serviceId) { \ + serviceId = getService(service); \ + serviceId = getService(service); \ + } \ + while (!functionId) { \ + functionId = getFunction(serviceId, function); \ + functionId = getFunction(serviceId, function); \ + } \ + initialized = true; \ + } \ + return request(serviceId, functionId, data1, data2); \ + } + #endif diff --git a/src/userland/usb/include/usb.h b/src/userland/usb/include/usb.h index 009e306..0b26f5f 100644 --- a/src/userland/usb/include/usb.h +++ b/src/userland/usb/include/usb.h @@ -5,150 +5,7 @@ #define OFFSET(ptr, off) (((void *)(ptr)) + (off)) -extern uint32_t i; - -typedef volatile struct { - uint8_t capabilitiesLength; - uint8_t reserved; - uint16_t interfaceVersion; - uint32_t structuralParameters[3]; - uint32_t capabilityParameter1; - uint32_t doorbellOffset; - uint32_t runtimeOffset; - uint32_t capabilityParameter2; -} __attribute__((packed)) XHCICapabilities; - -typedef volatile struct { - uint32_t status; - uint32_t powerStatus; - uint32_t linkInfo; - uint32_t hardwareLPM; -} __attribute__((packed)) XHCIPort; - -typedef volatile struct { - uint32_t command; - uint32_t status; - uint32_t pageSize; - uint32_t reserved[2]; - uint32_t notification; - uint32_t commandRingControl[2]; - uint32_t reserved1[4]; - uint32_t deviceContextBaseAddressArray[2]; - uint32_t config; - uint8_t reserved2[964]; - XHCIPort ports[256]; -} __attribute__((packed)) XHCIOperational; - -typedef volatile struct { - uint32_t dataLow; - uint32_t dataHigh; - uint32_t status; - uint32_t control; -} __attribute__((packed)) XHCITRB; - -#define COMMAND_TYPE(x) (x << 10) -#define COMMAND_CYCLE(x) (x) -#define COMMAND_BSR(x) (x << 9) -#define COMMAND_SLOT_ID(x) (x << 24) - -typedef volatile struct { - uint32_t commandLow; - uint32_t commandHigh; - uint32_t completionParameter : 24, completionCode : 8; - uint32_t cycle : 1, reserved : 9, type : 6, vfId : 8, slotId : 8; -} __attribute__((packed)) CommandCompletionEvent; - -typedef volatile struct { - uint32_t management; - uint32_t moderationCounter : 16, moderationInterval : 16; - uint32_t eventRingSegmentTableSize; // MAX 16 bit - uint32_t reserved2; - uint32_t eventRingSegmentTableAddress[2]; - uint32_t eventRingDequeuePointer[2]; -} __attribute__((packed)) XHCIInterrupter; - -typedef volatile struct { - uint8_t microframeIndex; - uint8_t reserved[0x20 - 1]; - XHCIInterrupter interrupters[1023]; -} __attribute__((packed)) XHCIRuntime; - -typedef volatile struct { - uint32_t ringSegmentBaseAddress[2]; - uint16_t ringSegmentSize, reserved; - uint32_t reserved1; -} __attribute__((packed)) XHCIEventRingSegmentTableEntry; - -typedef volatile struct { - uint32_t requestType : 8, request : 8, value : 16; - uint32_t index : 16, length : 16; - - uint32_t transferLength : 17, // always 8 - reserved : 5, interrupterTarget : 10; - - uint32_t cycle : 1, reserved1 : 4, interruptOnCompletion : 1, - immediateData : 1, // here 1 - reserved2 : 3, type : 6, transferType : 2; -} __attribute__((packed)) XHCISetupStageTRB; - -typedef volatile struct { - uint32_t reserved[2]; - - uint32_t reserved1 : 22, interrupterTarget : 10; - - uint32_t cycle : 1, evaluateNext : 1, reserved2 : 2, chain : 1, - interruptOnCompletion : 1, reserved3 : 4, type : 6, inDirection : 1; -} __attribute__((packed)) XHCIStatusStageTRB; - -typedef volatile struct { - uint32_t dataBuffer[2]; - - uint32_t transferSize : 17, tdSize : 5, interrupterTarget : 10; - - uint32_t cycle : 1, evaluateNext : 1, interruptOnShortPacket : 1, - noSnoop : 1, chain : 1, interruptOnCompletion : 1, immediateData : 1, - reserved : 3, type : 6, inDirection : 1; -} __attribute__((packed)) XHCIDataStageTRB; - -typedef volatile struct { - uint32_t routeString : 20, speed : 4, reserved : 1, multiTT : 1, isHub : 1, - contextEntryCount : 5; - uint32_t maxLatency : 16, rootHubPort : 8, portCount : 8; - uint32_t parentHubSlotId : 8, partentPortNumber : 8, thinkTime : 2, - reserved1 : 4, interrupterTarget : 10; - uint32_t deviceAddress : 8, reserved2 : 19, slotState : 5; - uint32_t reserved3[4]; -} __attribute__((packed)) XHCISlotContext; - -typedef volatile struct { - uint32_t endpointState : 3, reserved : 5, multiplier : 2, - maxPrimaryStreams : 5, linearStreamArray : 1, interval : 8, - maxEISTPayloadHigh : 8; - uint32_t reserved1 : 1, errorCount : 2, endpointType : 3, reserved2 : 1, - hostInitiateDisable : 1, maxBurstSize : 8, maxPacketSize : 16; - uint32_t transferDequeuePointerLow; - uint32_t transferDequeuePointerHigh; - uint32_t averageTRBLength : 16, maxEISTPayloadLow : 16; - uint32_t reserved4[3]; -} __attribute__((packed)) XHCIEndpointContext; - -typedef volatile struct { - XHCISlotContext slot; - XHCIEndpointContext endpoints[32]; -} __attribute__((packed)) XHCIDevice; - -typedef volatile struct { - uint32_t dropContextFlags; - uint32_t addContextFlags; - uint32_t reserved[5]; - uint32_t configuration : 8, interfaceNumber : 8, AlternateSetting : 8, - reserved1 : 8; -} __attribute__((packed)) XHCIInputControl; - -typedef volatile struct { - XHCIInputControl inputControl; - XHCIDevice deviceContext; -} __attribute__((packed)) XHCIInputContext; +extern uint32_t serviceId; typedef volatile struct { uint8_t size; @@ -168,35 +25,6 @@ } __attribute__((packed)) UsbDeviceDescriptor; typedef struct { - XHCITRB *trbs, *physical; - uint32_t size, enqueue, dequeue; - bool cycle; -} TrbRing; - -typedef struct { - XHCICapabilities *capabilities; - XHCIOperational *operational; - XHCIRuntime *runtime; - TrbRing commands; - TrbRing events; - uint32_t pciDevice; - volatile uint32_t *doorbells; - XHCIEventRingSegmentTableEntry *eventRingSegmentTable, - *eventRingSegmentTablePhysical; - uint64_t *deviceContextBaseAddressArray; - uint32_t portCount; - XHCIInputContext *inputContexts[32]; -} XHCIController; - -typedef struct { - XHCIController *controller; - uint32_t slotIndex, portIndex; - XHCIInputContext *inputContext; - XHCIPort *port; - TrbRing *controlRing; -} SlotXHCI; - -typedef struct { uint32_t pciClass; void *(*initialize)(uint32_t, uint32_t, uint32_t); void (*getDeviceDescriptor)(void *, uint32_t, uint32_t, void *); diff --git a/src/userland/usb/main.c b/src/userland/usb/main.c index 82637d1..cb58a2f 100644 --- a/src/userland/usb/main.c +++ b/src/userland/usb/main.c @@ -2,30 +2,10 @@ #include #include "../hlib/include/syscalls.h" -#include "xhci/commands.h" -#include "xhci/controller.h" -#include "xhci/trbRing.h" #include uint32_t serviceId; -#define REQUEST(functionName, service, function) \ - uint32_t functionName(uint32_t data1, uint32_t data2) { \ - static uint32_t serviceId, functionId, initialized = false; \ - if (!initialized) { \ - while (!serviceId) { \ - serviceId = getService(service); \ - serviceId = getService(service); \ - } \ - while (!functionId) { \ - functionId = getFunction(serviceId, function); \ - functionId = getFunction(serviceId, function); \ - } \ - initialized = true; \ - } \ - return request(serviceId, functionId, data1, data2); \ - } - REQUEST(getBaseAddress, "lspci", "getBaseAddress"); REQUEST(getDeviceClass, "lspci", "getDeviceClass"); REQUEST(enableBusMaster, "lspci", "enableBusMaster"); @@ -51,7 +31,7 @@ UsbDeviceDescriptor *descriptor = malloc(sizeof(UsbDeviceDescriptor)); slot->interface->getDeviceDescriptor(slot->data, 1 << 8, 0, buffer); memcpy(buffer, (void *)descriptor, sizeof(UsbDeviceDescriptor)); - printf("slot %i: usb version %x.%x\n", slot->portIndex, + printf("port %i: usb version %x.%x\n", slot->portIndex, descriptor->usbVersion >> 8, descriptor->usbVersion & 0xFF); printf("port %i: class: %i, subclass: %i, protocol %i, maxPacketSize: %i\n", slot->portIndex, descriptor->deviceClass, descriptor->deviceSubclass, diff --git a/src/userland/usb/xhci/commands.c b/src/userland/usb/xhci/commands.c index 62455b5..f0a99b5 100644 --- a/src/userland/usb/xhci/commands.c +++ b/src/userland/usb/xhci/commands.c @@ -1,6 +1,7 @@ #include "commands.h" #include "../../hlib/include/syscalls.h" #include "trbRing.h" +#include "xhci.h" #include #include diff --git a/src/include/hlib.h b/src/include/hlib.h index d38c59d..dac6387 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -75,4 +75,21 @@ extern void _listAdd(void *(malloc)(uint32_t), ListElement **list, void *data); #define listAdd(list, data) _listAdd(malloc, list, data) +#define REQUEST(functionName, service, function) \ + uint32_t functionName(uint32_t data1, uint32_t data2) { \ + static uint32_t serviceId, functionId, initialized = false; \ + if (!initialized) { \ + while (!serviceId) { \ + serviceId = getService(service); \ + serviceId = getService(service); \ + } \ + while (!functionId) { \ + functionId = getFunction(serviceId, function); \ + functionId = getFunction(serviceId, function); \ + } \ + initialized = true; \ + } \ + return request(serviceId, functionId, data1, data2); \ + } + #endif diff --git a/src/userland/usb/include/usb.h b/src/userland/usb/include/usb.h index 009e306..0b26f5f 100644 --- a/src/userland/usb/include/usb.h +++ b/src/userland/usb/include/usb.h @@ -5,150 +5,7 @@ #define OFFSET(ptr, off) (((void *)(ptr)) + (off)) -extern uint32_t i; - -typedef volatile struct { - uint8_t capabilitiesLength; - uint8_t reserved; - uint16_t interfaceVersion; - uint32_t structuralParameters[3]; - uint32_t capabilityParameter1; - uint32_t doorbellOffset; - uint32_t runtimeOffset; - uint32_t capabilityParameter2; -} __attribute__((packed)) XHCICapabilities; - -typedef volatile struct { - uint32_t status; - uint32_t powerStatus; - uint32_t linkInfo; - uint32_t hardwareLPM; -} __attribute__((packed)) XHCIPort; - -typedef volatile struct { - uint32_t command; - uint32_t status; - uint32_t pageSize; - uint32_t reserved[2]; - uint32_t notification; - uint32_t commandRingControl[2]; - uint32_t reserved1[4]; - uint32_t deviceContextBaseAddressArray[2]; - uint32_t config; - uint8_t reserved2[964]; - XHCIPort ports[256]; -} __attribute__((packed)) XHCIOperational; - -typedef volatile struct { - uint32_t dataLow; - uint32_t dataHigh; - uint32_t status; - uint32_t control; -} __attribute__((packed)) XHCITRB; - -#define COMMAND_TYPE(x) (x << 10) -#define COMMAND_CYCLE(x) (x) -#define COMMAND_BSR(x) (x << 9) -#define COMMAND_SLOT_ID(x) (x << 24) - -typedef volatile struct { - uint32_t commandLow; - uint32_t commandHigh; - uint32_t completionParameter : 24, completionCode : 8; - uint32_t cycle : 1, reserved : 9, type : 6, vfId : 8, slotId : 8; -} __attribute__((packed)) CommandCompletionEvent; - -typedef volatile struct { - uint32_t management; - uint32_t moderationCounter : 16, moderationInterval : 16; - uint32_t eventRingSegmentTableSize; // MAX 16 bit - uint32_t reserved2; - uint32_t eventRingSegmentTableAddress[2]; - uint32_t eventRingDequeuePointer[2]; -} __attribute__((packed)) XHCIInterrupter; - -typedef volatile struct { - uint8_t microframeIndex; - uint8_t reserved[0x20 - 1]; - XHCIInterrupter interrupters[1023]; -} __attribute__((packed)) XHCIRuntime; - -typedef volatile struct { - uint32_t ringSegmentBaseAddress[2]; - uint16_t ringSegmentSize, reserved; - uint32_t reserved1; -} __attribute__((packed)) XHCIEventRingSegmentTableEntry; - -typedef volatile struct { - uint32_t requestType : 8, request : 8, value : 16; - uint32_t index : 16, length : 16; - - uint32_t transferLength : 17, // always 8 - reserved : 5, interrupterTarget : 10; - - uint32_t cycle : 1, reserved1 : 4, interruptOnCompletion : 1, - immediateData : 1, // here 1 - reserved2 : 3, type : 6, transferType : 2; -} __attribute__((packed)) XHCISetupStageTRB; - -typedef volatile struct { - uint32_t reserved[2]; - - uint32_t reserved1 : 22, interrupterTarget : 10; - - uint32_t cycle : 1, evaluateNext : 1, reserved2 : 2, chain : 1, - interruptOnCompletion : 1, reserved3 : 4, type : 6, inDirection : 1; -} __attribute__((packed)) XHCIStatusStageTRB; - -typedef volatile struct { - uint32_t dataBuffer[2]; - - uint32_t transferSize : 17, tdSize : 5, interrupterTarget : 10; - - uint32_t cycle : 1, evaluateNext : 1, interruptOnShortPacket : 1, - noSnoop : 1, chain : 1, interruptOnCompletion : 1, immediateData : 1, - reserved : 3, type : 6, inDirection : 1; -} __attribute__((packed)) XHCIDataStageTRB; - -typedef volatile struct { - uint32_t routeString : 20, speed : 4, reserved : 1, multiTT : 1, isHub : 1, - contextEntryCount : 5; - uint32_t maxLatency : 16, rootHubPort : 8, portCount : 8; - uint32_t parentHubSlotId : 8, partentPortNumber : 8, thinkTime : 2, - reserved1 : 4, interrupterTarget : 10; - uint32_t deviceAddress : 8, reserved2 : 19, slotState : 5; - uint32_t reserved3[4]; -} __attribute__((packed)) XHCISlotContext; - -typedef volatile struct { - uint32_t endpointState : 3, reserved : 5, multiplier : 2, - maxPrimaryStreams : 5, linearStreamArray : 1, interval : 8, - maxEISTPayloadHigh : 8; - uint32_t reserved1 : 1, errorCount : 2, endpointType : 3, reserved2 : 1, - hostInitiateDisable : 1, maxBurstSize : 8, maxPacketSize : 16; - uint32_t transferDequeuePointerLow; - uint32_t transferDequeuePointerHigh; - uint32_t averageTRBLength : 16, maxEISTPayloadLow : 16; - uint32_t reserved4[3]; -} __attribute__((packed)) XHCIEndpointContext; - -typedef volatile struct { - XHCISlotContext slot; - XHCIEndpointContext endpoints[32]; -} __attribute__((packed)) XHCIDevice; - -typedef volatile struct { - uint32_t dropContextFlags; - uint32_t addContextFlags; - uint32_t reserved[5]; - uint32_t configuration : 8, interfaceNumber : 8, AlternateSetting : 8, - reserved1 : 8; -} __attribute__((packed)) XHCIInputControl; - -typedef volatile struct { - XHCIInputControl inputControl; - XHCIDevice deviceContext; -} __attribute__((packed)) XHCIInputContext; +extern uint32_t serviceId; typedef volatile struct { uint8_t size; @@ -168,35 +25,6 @@ } __attribute__((packed)) UsbDeviceDescriptor; typedef struct { - XHCITRB *trbs, *physical; - uint32_t size, enqueue, dequeue; - bool cycle; -} TrbRing; - -typedef struct { - XHCICapabilities *capabilities; - XHCIOperational *operational; - XHCIRuntime *runtime; - TrbRing commands; - TrbRing events; - uint32_t pciDevice; - volatile uint32_t *doorbells; - XHCIEventRingSegmentTableEntry *eventRingSegmentTable, - *eventRingSegmentTablePhysical; - uint64_t *deviceContextBaseAddressArray; - uint32_t portCount; - XHCIInputContext *inputContexts[32]; -} XHCIController; - -typedef struct { - XHCIController *controller; - uint32_t slotIndex, portIndex; - XHCIInputContext *inputContext; - XHCIPort *port; - TrbRing *controlRing; -} SlotXHCI; - -typedef struct { uint32_t pciClass; void *(*initialize)(uint32_t, uint32_t, uint32_t); void (*getDeviceDescriptor)(void *, uint32_t, uint32_t, void *); diff --git a/src/userland/usb/main.c b/src/userland/usb/main.c index 82637d1..cb58a2f 100644 --- a/src/userland/usb/main.c +++ b/src/userland/usb/main.c @@ -2,30 +2,10 @@ #include #include "../hlib/include/syscalls.h" -#include "xhci/commands.h" -#include "xhci/controller.h" -#include "xhci/trbRing.h" #include uint32_t serviceId; -#define REQUEST(functionName, service, function) \ - uint32_t functionName(uint32_t data1, uint32_t data2) { \ - static uint32_t serviceId, functionId, initialized = false; \ - if (!initialized) { \ - while (!serviceId) { \ - serviceId = getService(service); \ - serviceId = getService(service); \ - } \ - while (!functionId) { \ - functionId = getFunction(serviceId, function); \ - functionId = getFunction(serviceId, function); \ - } \ - initialized = true; \ - } \ - return request(serviceId, functionId, data1, data2); \ - } - REQUEST(getBaseAddress, "lspci", "getBaseAddress"); REQUEST(getDeviceClass, "lspci", "getDeviceClass"); REQUEST(enableBusMaster, "lspci", "enableBusMaster"); @@ -51,7 +31,7 @@ UsbDeviceDescriptor *descriptor = malloc(sizeof(UsbDeviceDescriptor)); slot->interface->getDeviceDescriptor(slot->data, 1 << 8, 0, buffer); memcpy(buffer, (void *)descriptor, sizeof(UsbDeviceDescriptor)); - printf("slot %i: usb version %x.%x\n", slot->portIndex, + printf("port %i: usb version %x.%x\n", slot->portIndex, descriptor->usbVersion >> 8, descriptor->usbVersion & 0xFF); printf("port %i: class: %i, subclass: %i, protocol %i, maxPacketSize: %i\n", slot->portIndex, descriptor->deviceClass, descriptor->deviceSubclass, diff --git a/src/userland/usb/xhci/commands.c b/src/userland/usb/xhci/commands.c index 62455b5..f0a99b5 100644 --- a/src/userland/usb/xhci/commands.c +++ b/src/userland/usb/xhci/commands.c @@ -1,6 +1,7 @@ #include "commands.h" #include "../../hlib/include/syscalls.h" #include "trbRing.h" +#include "xhci.h" #include #include diff --git a/src/userland/usb/xhci/commands.h b/src/userland/usb/xhci/commands.h index 6c8a694..cae27b6 100644 --- a/src/userland/usb/xhci/commands.h +++ b/src/userland/usb/xhci/commands.h @@ -1,8 +1,8 @@ #ifndef COMMANDS_H #define COMMANDS_H +#include "xhci.h" #include -#include extern void addressDevice(SlotXHCI *slot, bool BSR); extern void configureEndpoint(XHCIController *controller, void *inputContext, diff --git a/src/include/hlib.h b/src/include/hlib.h index d38c59d..dac6387 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -75,4 +75,21 @@ extern void _listAdd(void *(malloc)(uint32_t), ListElement **list, void *data); #define listAdd(list, data) _listAdd(malloc, list, data) +#define REQUEST(functionName, service, function) \ + uint32_t functionName(uint32_t data1, uint32_t data2) { \ + static uint32_t serviceId, functionId, initialized = false; \ + if (!initialized) { \ + while (!serviceId) { \ + serviceId = getService(service); \ + serviceId = getService(service); \ + } \ + while (!functionId) { \ + functionId = getFunction(serviceId, function); \ + functionId = getFunction(serviceId, function); \ + } \ + initialized = true; \ + } \ + return request(serviceId, functionId, data1, data2); \ + } + #endif diff --git a/src/userland/usb/include/usb.h b/src/userland/usb/include/usb.h index 009e306..0b26f5f 100644 --- a/src/userland/usb/include/usb.h +++ b/src/userland/usb/include/usb.h @@ -5,150 +5,7 @@ #define OFFSET(ptr, off) (((void *)(ptr)) + (off)) -extern uint32_t i; - -typedef volatile struct { - uint8_t capabilitiesLength; - uint8_t reserved; - uint16_t interfaceVersion; - uint32_t structuralParameters[3]; - uint32_t capabilityParameter1; - uint32_t doorbellOffset; - uint32_t runtimeOffset; - uint32_t capabilityParameter2; -} __attribute__((packed)) XHCICapabilities; - -typedef volatile struct { - uint32_t status; - uint32_t powerStatus; - uint32_t linkInfo; - uint32_t hardwareLPM; -} __attribute__((packed)) XHCIPort; - -typedef volatile struct { - uint32_t command; - uint32_t status; - uint32_t pageSize; - uint32_t reserved[2]; - uint32_t notification; - uint32_t commandRingControl[2]; - uint32_t reserved1[4]; - uint32_t deviceContextBaseAddressArray[2]; - uint32_t config; - uint8_t reserved2[964]; - XHCIPort ports[256]; -} __attribute__((packed)) XHCIOperational; - -typedef volatile struct { - uint32_t dataLow; - uint32_t dataHigh; - uint32_t status; - uint32_t control; -} __attribute__((packed)) XHCITRB; - -#define COMMAND_TYPE(x) (x << 10) -#define COMMAND_CYCLE(x) (x) -#define COMMAND_BSR(x) (x << 9) -#define COMMAND_SLOT_ID(x) (x << 24) - -typedef volatile struct { - uint32_t commandLow; - uint32_t commandHigh; - uint32_t completionParameter : 24, completionCode : 8; - uint32_t cycle : 1, reserved : 9, type : 6, vfId : 8, slotId : 8; -} __attribute__((packed)) CommandCompletionEvent; - -typedef volatile struct { - uint32_t management; - uint32_t moderationCounter : 16, moderationInterval : 16; - uint32_t eventRingSegmentTableSize; // MAX 16 bit - uint32_t reserved2; - uint32_t eventRingSegmentTableAddress[2]; - uint32_t eventRingDequeuePointer[2]; -} __attribute__((packed)) XHCIInterrupter; - -typedef volatile struct { - uint8_t microframeIndex; - uint8_t reserved[0x20 - 1]; - XHCIInterrupter interrupters[1023]; -} __attribute__((packed)) XHCIRuntime; - -typedef volatile struct { - uint32_t ringSegmentBaseAddress[2]; - uint16_t ringSegmentSize, reserved; - uint32_t reserved1; -} __attribute__((packed)) XHCIEventRingSegmentTableEntry; - -typedef volatile struct { - uint32_t requestType : 8, request : 8, value : 16; - uint32_t index : 16, length : 16; - - uint32_t transferLength : 17, // always 8 - reserved : 5, interrupterTarget : 10; - - uint32_t cycle : 1, reserved1 : 4, interruptOnCompletion : 1, - immediateData : 1, // here 1 - reserved2 : 3, type : 6, transferType : 2; -} __attribute__((packed)) XHCISetupStageTRB; - -typedef volatile struct { - uint32_t reserved[2]; - - uint32_t reserved1 : 22, interrupterTarget : 10; - - uint32_t cycle : 1, evaluateNext : 1, reserved2 : 2, chain : 1, - interruptOnCompletion : 1, reserved3 : 4, type : 6, inDirection : 1; -} __attribute__((packed)) XHCIStatusStageTRB; - -typedef volatile struct { - uint32_t dataBuffer[2]; - - uint32_t transferSize : 17, tdSize : 5, interrupterTarget : 10; - - uint32_t cycle : 1, evaluateNext : 1, interruptOnShortPacket : 1, - noSnoop : 1, chain : 1, interruptOnCompletion : 1, immediateData : 1, - reserved : 3, type : 6, inDirection : 1; -} __attribute__((packed)) XHCIDataStageTRB; - -typedef volatile struct { - uint32_t routeString : 20, speed : 4, reserved : 1, multiTT : 1, isHub : 1, - contextEntryCount : 5; - uint32_t maxLatency : 16, rootHubPort : 8, portCount : 8; - uint32_t parentHubSlotId : 8, partentPortNumber : 8, thinkTime : 2, - reserved1 : 4, interrupterTarget : 10; - uint32_t deviceAddress : 8, reserved2 : 19, slotState : 5; - uint32_t reserved3[4]; -} __attribute__((packed)) XHCISlotContext; - -typedef volatile struct { - uint32_t endpointState : 3, reserved : 5, multiplier : 2, - maxPrimaryStreams : 5, linearStreamArray : 1, interval : 8, - maxEISTPayloadHigh : 8; - uint32_t reserved1 : 1, errorCount : 2, endpointType : 3, reserved2 : 1, - hostInitiateDisable : 1, maxBurstSize : 8, maxPacketSize : 16; - uint32_t transferDequeuePointerLow; - uint32_t transferDequeuePointerHigh; - uint32_t averageTRBLength : 16, maxEISTPayloadLow : 16; - uint32_t reserved4[3]; -} __attribute__((packed)) XHCIEndpointContext; - -typedef volatile struct { - XHCISlotContext slot; - XHCIEndpointContext endpoints[32]; -} __attribute__((packed)) XHCIDevice; - -typedef volatile struct { - uint32_t dropContextFlags; - uint32_t addContextFlags; - uint32_t reserved[5]; - uint32_t configuration : 8, interfaceNumber : 8, AlternateSetting : 8, - reserved1 : 8; -} __attribute__((packed)) XHCIInputControl; - -typedef volatile struct { - XHCIInputControl inputControl; - XHCIDevice deviceContext; -} __attribute__((packed)) XHCIInputContext; +extern uint32_t serviceId; typedef volatile struct { uint8_t size; @@ -168,35 +25,6 @@ } __attribute__((packed)) UsbDeviceDescriptor; typedef struct { - XHCITRB *trbs, *physical; - uint32_t size, enqueue, dequeue; - bool cycle; -} TrbRing; - -typedef struct { - XHCICapabilities *capabilities; - XHCIOperational *operational; - XHCIRuntime *runtime; - TrbRing commands; - TrbRing events; - uint32_t pciDevice; - volatile uint32_t *doorbells; - XHCIEventRingSegmentTableEntry *eventRingSegmentTable, - *eventRingSegmentTablePhysical; - uint64_t *deviceContextBaseAddressArray; - uint32_t portCount; - XHCIInputContext *inputContexts[32]; -} XHCIController; - -typedef struct { - XHCIController *controller; - uint32_t slotIndex, portIndex; - XHCIInputContext *inputContext; - XHCIPort *port; - TrbRing *controlRing; -} SlotXHCI; - -typedef struct { uint32_t pciClass; void *(*initialize)(uint32_t, uint32_t, uint32_t); void (*getDeviceDescriptor)(void *, uint32_t, uint32_t, void *); diff --git a/src/userland/usb/main.c b/src/userland/usb/main.c index 82637d1..cb58a2f 100644 --- a/src/userland/usb/main.c +++ b/src/userland/usb/main.c @@ -2,30 +2,10 @@ #include #include "../hlib/include/syscalls.h" -#include "xhci/commands.h" -#include "xhci/controller.h" -#include "xhci/trbRing.h" #include uint32_t serviceId; -#define REQUEST(functionName, service, function) \ - uint32_t functionName(uint32_t data1, uint32_t data2) { \ - static uint32_t serviceId, functionId, initialized = false; \ - if (!initialized) { \ - while (!serviceId) { \ - serviceId = getService(service); \ - serviceId = getService(service); \ - } \ - while (!functionId) { \ - functionId = getFunction(serviceId, function); \ - functionId = getFunction(serviceId, function); \ - } \ - initialized = true; \ - } \ - return request(serviceId, functionId, data1, data2); \ - } - REQUEST(getBaseAddress, "lspci", "getBaseAddress"); REQUEST(getDeviceClass, "lspci", "getDeviceClass"); REQUEST(enableBusMaster, "lspci", "enableBusMaster"); @@ -51,7 +31,7 @@ UsbDeviceDescriptor *descriptor = malloc(sizeof(UsbDeviceDescriptor)); slot->interface->getDeviceDescriptor(slot->data, 1 << 8, 0, buffer); memcpy(buffer, (void *)descriptor, sizeof(UsbDeviceDescriptor)); - printf("slot %i: usb version %x.%x\n", slot->portIndex, + printf("port %i: usb version %x.%x\n", slot->portIndex, descriptor->usbVersion >> 8, descriptor->usbVersion & 0xFF); printf("port %i: class: %i, subclass: %i, protocol %i, maxPacketSize: %i\n", slot->portIndex, descriptor->deviceClass, descriptor->deviceSubclass, diff --git a/src/userland/usb/xhci/commands.c b/src/userland/usb/xhci/commands.c index 62455b5..f0a99b5 100644 --- a/src/userland/usb/xhci/commands.c +++ b/src/userland/usb/xhci/commands.c @@ -1,6 +1,7 @@ #include "commands.h" #include "../../hlib/include/syscalls.h" #include "trbRing.h" +#include "xhci.h" #include #include diff --git a/src/userland/usb/xhci/commands.h b/src/userland/usb/xhci/commands.h index 6c8a694..cae27b6 100644 --- a/src/userland/usb/xhci/commands.h +++ b/src/userland/usb/xhci/commands.h @@ -1,8 +1,8 @@ #ifndef COMMANDS_H #define COMMANDS_H +#include "xhci.h" #include -#include extern void addressDevice(SlotXHCI *slot, bool BSR); extern void configureEndpoint(XHCIController *controller, void *inputContext, diff --git a/src/userland/usb/xhci/controller.c b/src/userland/usb/xhci/controller.c index 234384f..8c1ab5b 100644 --- a/src/userland/usb/xhci/controller.c +++ b/src/userland/usb/xhci/controller.c @@ -2,6 +2,7 @@ #include "../../hlib/include/syscalls.h" #include "commands.h" #include "trbRing.h" +#include "xhci.h" #include #include diff --git a/src/include/hlib.h b/src/include/hlib.h index d38c59d..dac6387 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -75,4 +75,21 @@ extern void _listAdd(void *(malloc)(uint32_t), ListElement **list, void *data); #define listAdd(list, data) _listAdd(malloc, list, data) +#define REQUEST(functionName, service, function) \ + uint32_t functionName(uint32_t data1, uint32_t data2) { \ + static uint32_t serviceId, functionId, initialized = false; \ + if (!initialized) { \ + while (!serviceId) { \ + serviceId = getService(service); \ + serviceId = getService(service); \ + } \ + while (!functionId) { \ + functionId = getFunction(serviceId, function); \ + functionId = getFunction(serviceId, function); \ + } \ + initialized = true; \ + } \ + return request(serviceId, functionId, data1, data2); \ + } + #endif diff --git a/src/userland/usb/include/usb.h b/src/userland/usb/include/usb.h index 009e306..0b26f5f 100644 --- a/src/userland/usb/include/usb.h +++ b/src/userland/usb/include/usb.h @@ -5,150 +5,7 @@ #define OFFSET(ptr, off) (((void *)(ptr)) + (off)) -extern uint32_t i; - -typedef volatile struct { - uint8_t capabilitiesLength; - uint8_t reserved; - uint16_t interfaceVersion; - uint32_t structuralParameters[3]; - uint32_t capabilityParameter1; - uint32_t doorbellOffset; - uint32_t runtimeOffset; - uint32_t capabilityParameter2; -} __attribute__((packed)) XHCICapabilities; - -typedef volatile struct { - uint32_t status; - uint32_t powerStatus; - uint32_t linkInfo; - uint32_t hardwareLPM; -} __attribute__((packed)) XHCIPort; - -typedef volatile struct { - uint32_t command; - uint32_t status; - uint32_t pageSize; - uint32_t reserved[2]; - uint32_t notification; - uint32_t commandRingControl[2]; - uint32_t reserved1[4]; - uint32_t deviceContextBaseAddressArray[2]; - uint32_t config; - uint8_t reserved2[964]; - XHCIPort ports[256]; -} __attribute__((packed)) XHCIOperational; - -typedef volatile struct { - uint32_t dataLow; - uint32_t dataHigh; - uint32_t status; - uint32_t control; -} __attribute__((packed)) XHCITRB; - -#define COMMAND_TYPE(x) (x << 10) -#define COMMAND_CYCLE(x) (x) -#define COMMAND_BSR(x) (x << 9) -#define COMMAND_SLOT_ID(x) (x << 24) - -typedef volatile struct { - uint32_t commandLow; - uint32_t commandHigh; - uint32_t completionParameter : 24, completionCode : 8; - uint32_t cycle : 1, reserved : 9, type : 6, vfId : 8, slotId : 8; -} __attribute__((packed)) CommandCompletionEvent; - -typedef volatile struct { - uint32_t management; - uint32_t moderationCounter : 16, moderationInterval : 16; - uint32_t eventRingSegmentTableSize; // MAX 16 bit - uint32_t reserved2; - uint32_t eventRingSegmentTableAddress[2]; - uint32_t eventRingDequeuePointer[2]; -} __attribute__((packed)) XHCIInterrupter; - -typedef volatile struct { - uint8_t microframeIndex; - uint8_t reserved[0x20 - 1]; - XHCIInterrupter interrupters[1023]; -} __attribute__((packed)) XHCIRuntime; - -typedef volatile struct { - uint32_t ringSegmentBaseAddress[2]; - uint16_t ringSegmentSize, reserved; - uint32_t reserved1; -} __attribute__((packed)) XHCIEventRingSegmentTableEntry; - -typedef volatile struct { - uint32_t requestType : 8, request : 8, value : 16; - uint32_t index : 16, length : 16; - - uint32_t transferLength : 17, // always 8 - reserved : 5, interrupterTarget : 10; - - uint32_t cycle : 1, reserved1 : 4, interruptOnCompletion : 1, - immediateData : 1, // here 1 - reserved2 : 3, type : 6, transferType : 2; -} __attribute__((packed)) XHCISetupStageTRB; - -typedef volatile struct { - uint32_t reserved[2]; - - uint32_t reserved1 : 22, interrupterTarget : 10; - - uint32_t cycle : 1, evaluateNext : 1, reserved2 : 2, chain : 1, - interruptOnCompletion : 1, reserved3 : 4, type : 6, inDirection : 1; -} __attribute__((packed)) XHCIStatusStageTRB; - -typedef volatile struct { - uint32_t dataBuffer[2]; - - uint32_t transferSize : 17, tdSize : 5, interrupterTarget : 10; - - uint32_t cycle : 1, evaluateNext : 1, interruptOnShortPacket : 1, - noSnoop : 1, chain : 1, interruptOnCompletion : 1, immediateData : 1, - reserved : 3, type : 6, inDirection : 1; -} __attribute__((packed)) XHCIDataStageTRB; - -typedef volatile struct { - uint32_t routeString : 20, speed : 4, reserved : 1, multiTT : 1, isHub : 1, - contextEntryCount : 5; - uint32_t maxLatency : 16, rootHubPort : 8, portCount : 8; - uint32_t parentHubSlotId : 8, partentPortNumber : 8, thinkTime : 2, - reserved1 : 4, interrupterTarget : 10; - uint32_t deviceAddress : 8, reserved2 : 19, slotState : 5; - uint32_t reserved3[4]; -} __attribute__((packed)) XHCISlotContext; - -typedef volatile struct { - uint32_t endpointState : 3, reserved : 5, multiplier : 2, - maxPrimaryStreams : 5, linearStreamArray : 1, interval : 8, - maxEISTPayloadHigh : 8; - uint32_t reserved1 : 1, errorCount : 2, endpointType : 3, reserved2 : 1, - hostInitiateDisable : 1, maxBurstSize : 8, maxPacketSize : 16; - uint32_t transferDequeuePointerLow; - uint32_t transferDequeuePointerHigh; - uint32_t averageTRBLength : 16, maxEISTPayloadLow : 16; - uint32_t reserved4[3]; -} __attribute__((packed)) XHCIEndpointContext; - -typedef volatile struct { - XHCISlotContext slot; - XHCIEndpointContext endpoints[32]; -} __attribute__((packed)) XHCIDevice; - -typedef volatile struct { - uint32_t dropContextFlags; - uint32_t addContextFlags; - uint32_t reserved[5]; - uint32_t configuration : 8, interfaceNumber : 8, AlternateSetting : 8, - reserved1 : 8; -} __attribute__((packed)) XHCIInputControl; - -typedef volatile struct { - XHCIInputControl inputControl; - XHCIDevice deviceContext; -} __attribute__((packed)) XHCIInputContext; +extern uint32_t serviceId; typedef volatile struct { uint8_t size; @@ -168,35 +25,6 @@ } __attribute__((packed)) UsbDeviceDescriptor; typedef struct { - XHCITRB *trbs, *physical; - uint32_t size, enqueue, dequeue; - bool cycle; -} TrbRing; - -typedef struct { - XHCICapabilities *capabilities; - XHCIOperational *operational; - XHCIRuntime *runtime; - TrbRing commands; - TrbRing events; - uint32_t pciDevice; - volatile uint32_t *doorbells; - XHCIEventRingSegmentTableEntry *eventRingSegmentTable, - *eventRingSegmentTablePhysical; - uint64_t *deviceContextBaseAddressArray; - uint32_t portCount; - XHCIInputContext *inputContexts[32]; -} XHCIController; - -typedef struct { - XHCIController *controller; - uint32_t slotIndex, portIndex; - XHCIInputContext *inputContext; - XHCIPort *port; - TrbRing *controlRing; -} SlotXHCI; - -typedef struct { uint32_t pciClass; void *(*initialize)(uint32_t, uint32_t, uint32_t); void (*getDeviceDescriptor)(void *, uint32_t, uint32_t, void *); diff --git a/src/userland/usb/main.c b/src/userland/usb/main.c index 82637d1..cb58a2f 100644 --- a/src/userland/usb/main.c +++ b/src/userland/usb/main.c @@ -2,30 +2,10 @@ #include #include "../hlib/include/syscalls.h" -#include "xhci/commands.h" -#include "xhci/controller.h" -#include "xhci/trbRing.h" #include uint32_t serviceId; -#define REQUEST(functionName, service, function) \ - uint32_t functionName(uint32_t data1, uint32_t data2) { \ - static uint32_t serviceId, functionId, initialized = false; \ - if (!initialized) { \ - while (!serviceId) { \ - serviceId = getService(service); \ - serviceId = getService(service); \ - } \ - while (!functionId) { \ - functionId = getFunction(serviceId, function); \ - functionId = getFunction(serviceId, function); \ - } \ - initialized = true; \ - } \ - return request(serviceId, functionId, data1, data2); \ - } - REQUEST(getBaseAddress, "lspci", "getBaseAddress"); REQUEST(getDeviceClass, "lspci", "getDeviceClass"); REQUEST(enableBusMaster, "lspci", "enableBusMaster"); @@ -51,7 +31,7 @@ UsbDeviceDescriptor *descriptor = malloc(sizeof(UsbDeviceDescriptor)); slot->interface->getDeviceDescriptor(slot->data, 1 << 8, 0, buffer); memcpy(buffer, (void *)descriptor, sizeof(UsbDeviceDescriptor)); - printf("slot %i: usb version %x.%x\n", slot->portIndex, + printf("port %i: usb version %x.%x\n", slot->portIndex, descriptor->usbVersion >> 8, descriptor->usbVersion & 0xFF); printf("port %i: class: %i, subclass: %i, protocol %i, maxPacketSize: %i\n", slot->portIndex, descriptor->deviceClass, descriptor->deviceSubclass, diff --git a/src/userland/usb/xhci/commands.c b/src/userland/usb/xhci/commands.c index 62455b5..f0a99b5 100644 --- a/src/userland/usb/xhci/commands.c +++ b/src/userland/usb/xhci/commands.c @@ -1,6 +1,7 @@ #include "commands.h" #include "../../hlib/include/syscalls.h" #include "trbRing.h" +#include "xhci.h" #include #include diff --git a/src/userland/usb/xhci/commands.h b/src/userland/usb/xhci/commands.h index 6c8a694..cae27b6 100644 --- a/src/userland/usb/xhci/commands.h +++ b/src/userland/usb/xhci/commands.h @@ -1,8 +1,8 @@ #ifndef COMMANDS_H #define COMMANDS_H +#include "xhci.h" #include -#include extern void addressDevice(SlotXHCI *slot, bool BSR); extern void configureEndpoint(XHCIController *controller, void *inputContext, diff --git a/src/userland/usb/xhci/controller.c b/src/userland/usb/xhci/controller.c index 234384f..8c1ab5b 100644 --- a/src/userland/usb/xhci/controller.c +++ b/src/userland/usb/xhci/controller.c @@ -2,6 +2,7 @@ #include "../../hlib/include/syscalls.h" #include "commands.h" #include "trbRing.h" +#include "xhci.h" #include #include diff --git a/src/userland/usb/xhci/controller.h b/src/userland/usb/xhci/controller.h index 568c274..61355b0 100644 --- a/src/userland/usb/xhci/controller.h +++ b/src/userland/usb/xhci/controller.h @@ -1,6 +1,7 @@ #ifndef XHCI_CONTROLLER_H #define XHCI_CONTROLLER_H +#include "xhci.h" #include extern XHCIController *xhciSetup(uint32_t deviceId, uint32_t bar0, diff --git a/src/include/hlib.h b/src/include/hlib.h index d38c59d..dac6387 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -75,4 +75,21 @@ extern void _listAdd(void *(malloc)(uint32_t), ListElement **list, void *data); #define listAdd(list, data) _listAdd(malloc, list, data) +#define REQUEST(functionName, service, function) \ + uint32_t functionName(uint32_t data1, uint32_t data2) { \ + static uint32_t serviceId, functionId, initialized = false; \ + if (!initialized) { \ + while (!serviceId) { \ + serviceId = getService(service); \ + serviceId = getService(service); \ + } \ + while (!functionId) { \ + functionId = getFunction(serviceId, function); \ + functionId = getFunction(serviceId, function); \ + } \ + initialized = true; \ + } \ + return request(serviceId, functionId, data1, data2); \ + } + #endif diff --git a/src/userland/usb/include/usb.h b/src/userland/usb/include/usb.h index 009e306..0b26f5f 100644 --- a/src/userland/usb/include/usb.h +++ b/src/userland/usb/include/usb.h @@ -5,150 +5,7 @@ #define OFFSET(ptr, off) (((void *)(ptr)) + (off)) -extern uint32_t i; - -typedef volatile struct { - uint8_t capabilitiesLength; - uint8_t reserved; - uint16_t interfaceVersion; - uint32_t structuralParameters[3]; - uint32_t capabilityParameter1; - uint32_t doorbellOffset; - uint32_t runtimeOffset; - uint32_t capabilityParameter2; -} __attribute__((packed)) XHCICapabilities; - -typedef volatile struct { - uint32_t status; - uint32_t powerStatus; - uint32_t linkInfo; - uint32_t hardwareLPM; -} __attribute__((packed)) XHCIPort; - -typedef volatile struct { - uint32_t command; - uint32_t status; - uint32_t pageSize; - uint32_t reserved[2]; - uint32_t notification; - uint32_t commandRingControl[2]; - uint32_t reserved1[4]; - uint32_t deviceContextBaseAddressArray[2]; - uint32_t config; - uint8_t reserved2[964]; - XHCIPort ports[256]; -} __attribute__((packed)) XHCIOperational; - -typedef volatile struct { - uint32_t dataLow; - uint32_t dataHigh; - uint32_t status; - uint32_t control; -} __attribute__((packed)) XHCITRB; - -#define COMMAND_TYPE(x) (x << 10) -#define COMMAND_CYCLE(x) (x) -#define COMMAND_BSR(x) (x << 9) -#define COMMAND_SLOT_ID(x) (x << 24) - -typedef volatile struct { - uint32_t commandLow; - uint32_t commandHigh; - uint32_t completionParameter : 24, completionCode : 8; - uint32_t cycle : 1, reserved : 9, type : 6, vfId : 8, slotId : 8; -} __attribute__((packed)) CommandCompletionEvent; - -typedef volatile struct { - uint32_t management; - uint32_t moderationCounter : 16, moderationInterval : 16; - uint32_t eventRingSegmentTableSize; // MAX 16 bit - uint32_t reserved2; - uint32_t eventRingSegmentTableAddress[2]; - uint32_t eventRingDequeuePointer[2]; -} __attribute__((packed)) XHCIInterrupter; - -typedef volatile struct { - uint8_t microframeIndex; - uint8_t reserved[0x20 - 1]; - XHCIInterrupter interrupters[1023]; -} __attribute__((packed)) XHCIRuntime; - -typedef volatile struct { - uint32_t ringSegmentBaseAddress[2]; - uint16_t ringSegmentSize, reserved; - uint32_t reserved1; -} __attribute__((packed)) XHCIEventRingSegmentTableEntry; - -typedef volatile struct { - uint32_t requestType : 8, request : 8, value : 16; - uint32_t index : 16, length : 16; - - uint32_t transferLength : 17, // always 8 - reserved : 5, interrupterTarget : 10; - - uint32_t cycle : 1, reserved1 : 4, interruptOnCompletion : 1, - immediateData : 1, // here 1 - reserved2 : 3, type : 6, transferType : 2; -} __attribute__((packed)) XHCISetupStageTRB; - -typedef volatile struct { - uint32_t reserved[2]; - - uint32_t reserved1 : 22, interrupterTarget : 10; - - uint32_t cycle : 1, evaluateNext : 1, reserved2 : 2, chain : 1, - interruptOnCompletion : 1, reserved3 : 4, type : 6, inDirection : 1; -} __attribute__((packed)) XHCIStatusStageTRB; - -typedef volatile struct { - uint32_t dataBuffer[2]; - - uint32_t transferSize : 17, tdSize : 5, interrupterTarget : 10; - - uint32_t cycle : 1, evaluateNext : 1, interruptOnShortPacket : 1, - noSnoop : 1, chain : 1, interruptOnCompletion : 1, immediateData : 1, - reserved : 3, type : 6, inDirection : 1; -} __attribute__((packed)) XHCIDataStageTRB; - -typedef volatile struct { - uint32_t routeString : 20, speed : 4, reserved : 1, multiTT : 1, isHub : 1, - contextEntryCount : 5; - uint32_t maxLatency : 16, rootHubPort : 8, portCount : 8; - uint32_t parentHubSlotId : 8, partentPortNumber : 8, thinkTime : 2, - reserved1 : 4, interrupterTarget : 10; - uint32_t deviceAddress : 8, reserved2 : 19, slotState : 5; - uint32_t reserved3[4]; -} __attribute__((packed)) XHCISlotContext; - -typedef volatile struct { - uint32_t endpointState : 3, reserved : 5, multiplier : 2, - maxPrimaryStreams : 5, linearStreamArray : 1, interval : 8, - maxEISTPayloadHigh : 8; - uint32_t reserved1 : 1, errorCount : 2, endpointType : 3, reserved2 : 1, - hostInitiateDisable : 1, maxBurstSize : 8, maxPacketSize : 16; - uint32_t transferDequeuePointerLow; - uint32_t transferDequeuePointerHigh; - uint32_t averageTRBLength : 16, maxEISTPayloadLow : 16; - uint32_t reserved4[3]; -} __attribute__((packed)) XHCIEndpointContext; - -typedef volatile struct { - XHCISlotContext slot; - XHCIEndpointContext endpoints[32]; -} __attribute__((packed)) XHCIDevice; - -typedef volatile struct { - uint32_t dropContextFlags; - uint32_t addContextFlags; - uint32_t reserved[5]; - uint32_t configuration : 8, interfaceNumber : 8, AlternateSetting : 8, - reserved1 : 8; -} __attribute__((packed)) XHCIInputControl; - -typedef volatile struct { - XHCIInputControl inputControl; - XHCIDevice deviceContext; -} __attribute__((packed)) XHCIInputContext; +extern uint32_t serviceId; typedef volatile struct { uint8_t size; @@ -168,35 +25,6 @@ } __attribute__((packed)) UsbDeviceDescriptor; typedef struct { - XHCITRB *trbs, *physical; - uint32_t size, enqueue, dequeue; - bool cycle; -} TrbRing; - -typedef struct { - XHCICapabilities *capabilities; - XHCIOperational *operational; - XHCIRuntime *runtime; - TrbRing commands; - TrbRing events; - uint32_t pciDevice; - volatile uint32_t *doorbells; - XHCIEventRingSegmentTableEntry *eventRingSegmentTable, - *eventRingSegmentTablePhysical; - uint64_t *deviceContextBaseAddressArray; - uint32_t portCount; - XHCIInputContext *inputContexts[32]; -} XHCIController; - -typedef struct { - XHCIController *controller; - uint32_t slotIndex, portIndex; - XHCIInputContext *inputContext; - XHCIPort *port; - TrbRing *controlRing; -} SlotXHCI; - -typedef struct { uint32_t pciClass; void *(*initialize)(uint32_t, uint32_t, uint32_t); void (*getDeviceDescriptor)(void *, uint32_t, uint32_t, void *); diff --git a/src/userland/usb/main.c b/src/userland/usb/main.c index 82637d1..cb58a2f 100644 --- a/src/userland/usb/main.c +++ b/src/userland/usb/main.c @@ -2,30 +2,10 @@ #include #include "../hlib/include/syscalls.h" -#include "xhci/commands.h" -#include "xhci/controller.h" -#include "xhci/trbRing.h" #include uint32_t serviceId; -#define REQUEST(functionName, service, function) \ - uint32_t functionName(uint32_t data1, uint32_t data2) { \ - static uint32_t serviceId, functionId, initialized = false; \ - if (!initialized) { \ - while (!serviceId) { \ - serviceId = getService(service); \ - serviceId = getService(service); \ - } \ - while (!functionId) { \ - functionId = getFunction(serviceId, function); \ - functionId = getFunction(serviceId, function); \ - } \ - initialized = true; \ - } \ - return request(serviceId, functionId, data1, data2); \ - } - REQUEST(getBaseAddress, "lspci", "getBaseAddress"); REQUEST(getDeviceClass, "lspci", "getDeviceClass"); REQUEST(enableBusMaster, "lspci", "enableBusMaster"); @@ -51,7 +31,7 @@ UsbDeviceDescriptor *descriptor = malloc(sizeof(UsbDeviceDescriptor)); slot->interface->getDeviceDescriptor(slot->data, 1 << 8, 0, buffer); memcpy(buffer, (void *)descriptor, sizeof(UsbDeviceDescriptor)); - printf("slot %i: usb version %x.%x\n", slot->portIndex, + printf("port %i: usb version %x.%x\n", slot->portIndex, descriptor->usbVersion >> 8, descriptor->usbVersion & 0xFF); printf("port %i: class: %i, subclass: %i, protocol %i, maxPacketSize: %i\n", slot->portIndex, descriptor->deviceClass, descriptor->deviceSubclass, diff --git a/src/userland/usb/xhci/commands.c b/src/userland/usb/xhci/commands.c index 62455b5..f0a99b5 100644 --- a/src/userland/usb/xhci/commands.c +++ b/src/userland/usb/xhci/commands.c @@ -1,6 +1,7 @@ #include "commands.h" #include "../../hlib/include/syscalls.h" #include "trbRing.h" +#include "xhci.h" #include #include diff --git a/src/userland/usb/xhci/commands.h b/src/userland/usb/xhci/commands.h index 6c8a694..cae27b6 100644 --- a/src/userland/usb/xhci/commands.h +++ b/src/userland/usb/xhci/commands.h @@ -1,8 +1,8 @@ #ifndef COMMANDS_H #define COMMANDS_H +#include "xhci.h" #include -#include extern void addressDevice(SlotXHCI *slot, bool BSR); extern void configureEndpoint(XHCIController *controller, void *inputContext, diff --git a/src/userland/usb/xhci/controller.c b/src/userland/usb/xhci/controller.c index 234384f..8c1ab5b 100644 --- a/src/userland/usb/xhci/controller.c +++ b/src/userland/usb/xhci/controller.c @@ -2,6 +2,7 @@ #include "../../hlib/include/syscalls.h" #include "commands.h" #include "trbRing.h" +#include "xhci.h" #include #include diff --git a/src/userland/usb/xhci/controller.h b/src/userland/usb/xhci/controller.h index 568c274..61355b0 100644 --- a/src/userland/usb/xhci/controller.h +++ b/src/userland/usb/xhci/controller.h @@ -1,6 +1,7 @@ #ifndef XHCI_CONTROLLER_H #define XHCI_CONTROLLER_H +#include "xhci.h" #include extern XHCIController *xhciSetup(uint32_t deviceId, uint32_t bar0, diff --git a/src/userland/usb/xhci/trbRing.c b/src/userland/usb/xhci/trbRing.c index b487be8..fac19e1 100644 --- a/src/userland/usb/xhci/trbRing.c +++ b/src/userland/usb/xhci/trbRing.c @@ -1,3 +1,4 @@ +#include "xhci.h" #include XHCITRB *enqueueCommand(TrbRing *ring, XHCITRB *trb) { diff --git a/src/include/hlib.h b/src/include/hlib.h index d38c59d..dac6387 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -75,4 +75,21 @@ extern void _listAdd(void *(malloc)(uint32_t), ListElement **list, void *data); #define listAdd(list, data) _listAdd(malloc, list, data) +#define REQUEST(functionName, service, function) \ + uint32_t functionName(uint32_t data1, uint32_t data2) { \ + static uint32_t serviceId, functionId, initialized = false; \ + if (!initialized) { \ + while (!serviceId) { \ + serviceId = getService(service); \ + serviceId = getService(service); \ + } \ + while (!functionId) { \ + functionId = getFunction(serviceId, function); \ + functionId = getFunction(serviceId, function); \ + } \ + initialized = true; \ + } \ + return request(serviceId, functionId, data1, data2); \ + } + #endif diff --git a/src/userland/usb/include/usb.h b/src/userland/usb/include/usb.h index 009e306..0b26f5f 100644 --- a/src/userland/usb/include/usb.h +++ b/src/userland/usb/include/usb.h @@ -5,150 +5,7 @@ #define OFFSET(ptr, off) (((void *)(ptr)) + (off)) -extern uint32_t i; - -typedef volatile struct { - uint8_t capabilitiesLength; - uint8_t reserved; - uint16_t interfaceVersion; - uint32_t structuralParameters[3]; - uint32_t capabilityParameter1; - uint32_t doorbellOffset; - uint32_t runtimeOffset; - uint32_t capabilityParameter2; -} __attribute__((packed)) XHCICapabilities; - -typedef volatile struct { - uint32_t status; - uint32_t powerStatus; - uint32_t linkInfo; - uint32_t hardwareLPM; -} __attribute__((packed)) XHCIPort; - -typedef volatile struct { - uint32_t command; - uint32_t status; - uint32_t pageSize; - uint32_t reserved[2]; - uint32_t notification; - uint32_t commandRingControl[2]; - uint32_t reserved1[4]; - uint32_t deviceContextBaseAddressArray[2]; - uint32_t config; - uint8_t reserved2[964]; - XHCIPort ports[256]; -} __attribute__((packed)) XHCIOperational; - -typedef volatile struct { - uint32_t dataLow; - uint32_t dataHigh; - uint32_t status; - uint32_t control; -} __attribute__((packed)) XHCITRB; - -#define COMMAND_TYPE(x) (x << 10) -#define COMMAND_CYCLE(x) (x) -#define COMMAND_BSR(x) (x << 9) -#define COMMAND_SLOT_ID(x) (x << 24) - -typedef volatile struct { - uint32_t commandLow; - uint32_t commandHigh; - uint32_t completionParameter : 24, completionCode : 8; - uint32_t cycle : 1, reserved : 9, type : 6, vfId : 8, slotId : 8; -} __attribute__((packed)) CommandCompletionEvent; - -typedef volatile struct { - uint32_t management; - uint32_t moderationCounter : 16, moderationInterval : 16; - uint32_t eventRingSegmentTableSize; // MAX 16 bit - uint32_t reserved2; - uint32_t eventRingSegmentTableAddress[2]; - uint32_t eventRingDequeuePointer[2]; -} __attribute__((packed)) XHCIInterrupter; - -typedef volatile struct { - uint8_t microframeIndex; - uint8_t reserved[0x20 - 1]; - XHCIInterrupter interrupters[1023]; -} __attribute__((packed)) XHCIRuntime; - -typedef volatile struct { - uint32_t ringSegmentBaseAddress[2]; - uint16_t ringSegmentSize, reserved; - uint32_t reserved1; -} __attribute__((packed)) XHCIEventRingSegmentTableEntry; - -typedef volatile struct { - uint32_t requestType : 8, request : 8, value : 16; - uint32_t index : 16, length : 16; - - uint32_t transferLength : 17, // always 8 - reserved : 5, interrupterTarget : 10; - - uint32_t cycle : 1, reserved1 : 4, interruptOnCompletion : 1, - immediateData : 1, // here 1 - reserved2 : 3, type : 6, transferType : 2; -} __attribute__((packed)) XHCISetupStageTRB; - -typedef volatile struct { - uint32_t reserved[2]; - - uint32_t reserved1 : 22, interrupterTarget : 10; - - uint32_t cycle : 1, evaluateNext : 1, reserved2 : 2, chain : 1, - interruptOnCompletion : 1, reserved3 : 4, type : 6, inDirection : 1; -} __attribute__((packed)) XHCIStatusStageTRB; - -typedef volatile struct { - uint32_t dataBuffer[2]; - - uint32_t transferSize : 17, tdSize : 5, interrupterTarget : 10; - - uint32_t cycle : 1, evaluateNext : 1, interruptOnShortPacket : 1, - noSnoop : 1, chain : 1, interruptOnCompletion : 1, immediateData : 1, - reserved : 3, type : 6, inDirection : 1; -} __attribute__((packed)) XHCIDataStageTRB; - -typedef volatile struct { - uint32_t routeString : 20, speed : 4, reserved : 1, multiTT : 1, isHub : 1, - contextEntryCount : 5; - uint32_t maxLatency : 16, rootHubPort : 8, portCount : 8; - uint32_t parentHubSlotId : 8, partentPortNumber : 8, thinkTime : 2, - reserved1 : 4, interrupterTarget : 10; - uint32_t deviceAddress : 8, reserved2 : 19, slotState : 5; - uint32_t reserved3[4]; -} __attribute__((packed)) XHCISlotContext; - -typedef volatile struct { - uint32_t endpointState : 3, reserved : 5, multiplier : 2, - maxPrimaryStreams : 5, linearStreamArray : 1, interval : 8, - maxEISTPayloadHigh : 8; - uint32_t reserved1 : 1, errorCount : 2, endpointType : 3, reserved2 : 1, - hostInitiateDisable : 1, maxBurstSize : 8, maxPacketSize : 16; - uint32_t transferDequeuePointerLow; - uint32_t transferDequeuePointerHigh; - uint32_t averageTRBLength : 16, maxEISTPayloadLow : 16; - uint32_t reserved4[3]; -} __attribute__((packed)) XHCIEndpointContext; - -typedef volatile struct { - XHCISlotContext slot; - XHCIEndpointContext endpoints[32]; -} __attribute__((packed)) XHCIDevice; - -typedef volatile struct { - uint32_t dropContextFlags; - uint32_t addContextFlags; - uint32_t reserved[5]; - uint32_t configuration : 8, interfaceNumber : 8, AlternateSetting : 8, - reserved1 : 8; -} __attribute__((packed)) XHCIInputControl; - -typedef volatile struct { - XHCIInputControl inputControl; - XHCIDevice deviceContext; -} __attribute__((packed)) XHCIInputContext; +extern uint32_t serviceId; typedef volatile struct { uint8_t size; @@ -168,35 +25,6 @@ } __attribute__((packed)) UsbDeviceDescriptor; typedef struct { - XHCITRB *trbs, *physical; - uint32_t size, enqueue, dequeue; - bool cycle; -} TrbRing; - -typedef struct { - XHCICapabilities *capabilities; - XHCIOperational *operational; - XHCIRuntime *runtime; - TrbRing commands; - TrbRing events; - uint32_t pciDevice; - volatile uint32_t *doorbells; - XHCIEventRingSegmentTableEntry *eventRingSegmentTable, - *eventRingSegmentTablePhysical; - uint64_t *deviceContextBaseAddressArray; - uint32_t portCount; - XHCIInputContext *inputContexts[32]; -} XHCIController; - -typedef struct { - XHCIController *controller; - uint32_t slotIndex, portIndex; - XHCIInputContext *inputContext; - XHCIPort *port; - TrbRing *controlRing; -} SlotXHCI; - -typedef struct { uint32_t pciClass; void *(*initialize)(uint32_t, uint32_t, uint32_t); void (*getDeviceDescriptor)(void *, uint32_t, uint32_t, void *); diff --git a/src/userland/usb/main.c b/src/userland/usb/main.c index 82637d1..cb58a2f 100644 --- a/src/userland/usb/main.c +++ b/src/userland/usb/main.c @@ -2,30 +2,10 @@ #include #include "../hlib/include/syscalls.h" -#include "xhci/commands.h" -#include "xhci/controller.h" -#include "xhci/trbRing.h" #include uint32_t serviceId; -#define REQUEST(functionName, service, function) \ - uint32_t functionName(uint32_t data1, uint32_t data2) { \ - static uint32_t serviceId, functionId, initialized = false; \ - if (!initialized) { \ - while (!serviceId) { \ - serviceId = getService(service); \ - serviceId = getService(service); \ - } \ - while (!functionId) { \ - functionId = getFunction(serviceId, function); \ - functionId = getFunction(serviceId, function); \ - } \ - initialized = true; \ - } \ - return request(serviceId, functionId, data1, data2); \ - } - REQUEST(getBaseAddress, "lspci", "getBaseAddress"); REQUEST(getDeviceClass, "lspci", "getDeviceClass"); REQUEST(enableBusMaster, "lspci", "enableBusMaster"); @@ -51,7 +31,7 @@ UsbDeviceDescriptor *descriptor = malloc(sizeof(UsbDeviceDescriptor)); slot->interface->getDeviceDescriptor(slot->data, 1 << 8, 0, buffer); memcpy(buffer, (void *)descriptor, sizeof(UsbDeviceDescriptor)); - printf("slot %i: usb version %x.%x\n", slot->portIndex, + printf("port %i: usb version %x.%x\n", slot->portIndex, descriptor->usbVersion >> 8, descriptor->usbVersion & 0xFF); printf("port %i: class: %i, subclass: %i, protocol %i, maxPacketSize: %i\n", slot->portIndex, descriptor->deviceClass, descriptor->deviceSubclass, diff --git a/src/userland/usb/xhci/commands.c b/src/userland/usb/xhci/commands.c index 62455b5..f0a99b5 100644 --- a/src/userland/usb/xhci/commands.c +++ b/src/userland/usb/xhci/commands.c @@ -1,6 +1,7 @@ #include "commands.h" #include "../../hlib/include/syscalls.h" #include "trbRing.h" +#include "xhci.h" #include #include diff --git a/src/userland/usb/xhci/commands.h b/src/userland/usb/xhci/commands.h index 6c8a694..cae27b6 100644 --- a/src/userland/usb/xhci/commands.h +++ b/src/userland/usb/xhci/commands.h @@ -1,8 +1,8 @@ #ifndef COMMANDS_H #define COMMANDS_H +#include "xhci.h" #include -#include extern void addressDevice(SlotXHCI *slot, bool BSR); extern void configureEndpoint(XHCIController *controller, void *inputContext, diff --git a/src/userland/usb/xhci/controller.c b/src/userland/usb/xhci/controller.c index 234384f..8c1ab5b 100644 --- a/src/userland/usb/xhci/controller.c +++ b/src/userland/usb/xhci/controller.c @@ -2,6 +2,7 @@ #include "../../hlib/include/syscalls.h" #include "commands.h" #include "trbRing.h" +#include "xhci.h" #include #include diff --git a/src/userland/usb/xhci/controller.h b/src/userland/usb/xhci/controller.h index 568c274..61355b0 100644 --- a/src/userland/usb/xhci/controller.h +++ b/src/userland/usb/xhci/controller.h @@ -1,6 +1,7 @@ #ifndef XHCI_CONTROLLER_H #define XHCI_CONTROLLER_H +#include "xhci.h" #include extern XHCIController *xhciSetup(uint32_t deviceId, uint32_t bar0, diff --git a/src/userland/usb/xhci/trbRing.c b/src/userland/usb/xhci/trbRing.c index b487be8..fac19e1 100644 --- a/src/userland/usb/xhci/trbRing.c +++ b/src/userland/usb/xhci/trbRing.c @@ -1,3 +1,4 @@ +#include "xhci.h" #include XHCITRB *enqueueCommand(TrbRing *ring, XHCITRB *trb) { diff --git a/src/userland/usb/xhci/trbRing.h b/src/userland/usb/xhci/trbRing.h index bd31092..b0a7a3f 100644 --- a/src/userland/usb/xhci/trbRing.h +++ b/src/userland/usb/xhci/trbRing.h @@ -1,7 +1,7 @@ #ifndef TRB_RING_H #define TRB_RING_H -#include +#include "xhci.h" extern XHCITRB *enqueueCommand(TrbRing *ring, XHCITRB *trb); extern XHCITRB *trbRingFetch(TrbRing *ring, uint32_t *index); diff --git a/src/include/hlib.h b/src/include/hlib.h index d38c59d..dac6387 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -75,4 +75,21 @@ extern void _listAdd(void *(malloc)(uint32_t), ListElement **list, void *data); #define listAdd(list, data) _listAdd(malloc, list, data) +#define REQUEST(functionName, service, function) \ + uint32_t functionName(uint32_t data1, uint32_t data2) { \ + static uint32_t serviceId, functionId, initialized = false; \ + if (!initialized) { \ + while (!serviceId) { \ + serviceId = getService(service); \ + serviceId = getService(service); \ + } \ + while (!functionId) { \ + functionId = getFunction(serviceId, function); \ + functionId = getFunction(serviceId, function); \ + } \ + initialized = true; \ + } \ + return request(serviceId, functionId, data1, data2); \ + } + #endif diff --git a/src/userland/usb/include/usb.h b/src/userland/usb/include/usb.h index 009e306..0b26f5f 100644 --- a/src/userland/usb/include/usb.h +++ b/src/userland/usb/include/usb.h @@ -5,150 +5,7 @@ #define OFFSET(ptr, off) (((void *)(ptr)) + (off)) -extern uint32_t i; - -typedef volatile struct { - uint8_t capabilitiesLength; - uint8_t reserved; - uint16_t interfaceVersion; - uint32_t structuralParameters[3]; - uint32_t capabilityParameter1; - uint32_t doorbellOffset; - uint32_t runtimeOffset; - uint32_t capabilityParameter2; -} __attribute__((packed)) XHCICapabilities; - -typedef volatile struct { - uint32_t status; - uint32_t powerStatus; - uint32_t linkInfo; - uint32_t hardwareLPM; -} __attribute__((packed)) XHCIPort; - -typedef volatile struct { - uint32_t command; - uint32_t status; - uint32_t pageSize; - uint32_t reserved[2]; - uint32_t notification; - uint32_t commandRingControl[2]; - uint32_t reserved1[4]; - uint32_t deviceContextBaseAddressArray[2]; - uint32_t config; - uint8_t reserved2[964]; - XHCIPort ports[256]; -} __attribute__((packed)) XHCIOperational; - -typedef volatile struct { - uint32_t dataLow; - uint32_t dataHigh; - uint32_t status; - uint32_t control; -} __attribute__((packed)) XHCITRB; - -#define COMMAND_TYPE(x) (x << 10) -#define COMMAND_CYCLE(x) (x) -#define COMMAND_BSR(x) (x << 9) -#define COMMAND_SLOT_ID(x) (x << 24) - -typedef volatile struct { - uint32_t commandLow; - uint32_t commandHigh; - uint32_t completionParameter : 24, completionCode : 8; - uint32_t cycle : 1, reserved : 9, type : 6, vfId : 8, slotId : 8; -} __attribute__((packed)) CommandCompletionEvent; - -typedef volatile struct { - uint32_t management; - uint32_t moderationCounter : 16, moderationInterval : 16; - uint32_t eventRingSegmentTableSize; // MAX 16 bit - uint32_t reserved2; - uint32_t eventRingSegmentTableAddress[2]; - uint32_t eventRingDequeuePointer[2]; -} __attribute__((packed)) XHCIInterrupter; - -typedef volatile struct { - uint8_t microframeIndex; - uint8_t reserved[0x20 - 1]; - XHCIInterrupter interrupters[1023]; -} __attribute__((packed)) XHCIRuntime; - -typedef volatile struct { - uint32_t ringSegmentBaseAddress[2]; - uint16_t ringSegmentSize, reserved; - uint32_t reserved1; -} __attribute__((packed)) XHCIEventRingSegmentTableEntry; - -typedef volatile struct { - uint32_t requestType : 8, request : 8, value : 16; - uint32_t index : 16, length : 16; - - uint32_t transferLength : 17, // always 8 - reserved : 5, interrupterTarget : 10; - - uint32_t cycle : 1, reserved1 : 4, interruptOnCompletion : 1, - immediateData : 1, // here 1 - reserved2 : 3, type : 6, transferType : 2; -} __attribute__((packed)) XHCISetupStageTRB; - -typedef volatile struct { - uint32_t reserved[2]; - - uint32_t reserved1 : 22, interrupterTarget : 10; - - uint32_t cycle : 1, evaluateNext : 1, reserved2 : 2, chain : 1, - interruptOnCompletion : 1, reserved3 : 4, type : 6, inDirection : 1; -} __attribute__((packed)) XHCIStatusStageTRB; - -typedef volatile struct { - uint32_t dataBuffer[2]; - - uint32_t transferSize : 17, tdSize : 5, interrupterTarget : 10; - - uint32_t cycle : 1, evaluateNext : 1, interruptOnShortPacket : 1, - noSnoop : 1, chain : 1, interruptOnCompletion : 1, immediateData : 1, - reserved : 3, type : 6, inDirection : 1; -} __attribute__((packed)) XHCIDataStageTRB; - -typedef volatile struct { - uint32_t routeString : 20, speed : 4, reserved : 1, multiTT : 1, isHub : 1, - contextEntryCount : 5; - uint32_t maxLatency : 16, rootHubPort : 8, portCount : 8; - uint32_t parentHubSlotId : 8, partentPortNumber : 8, thinkTime : 2, - reserved1 : 4, interrupterTarget : 10; - uint32_t deviceAddress : 8, reserved2 : 19, slotState : 5; - uint32_t reserved3[4]; -} __attribute__((packed)) XHCISlotContext; - -typedef volatile struct { - uint32_t endpointState : 3, reserved : 5, multiplier : 2, - maxPrimaryStreams : 5, linearStreamArray : 1, interval : 8, - maxEISTPayloadHigh : 8; - uint32_t reserved1 : 1, errorCount : 2, endpointType : 3, reserved2 : 1, - hostInitiateDisable : 1, maxBurstSize : 8, maxPacketSize : 16; - uint32_t transferDequeuePointerLow; - uint32_t transferDequeuePointerHigh; - uint32_t averageTRBLength : 16, maxEISTPayloadLow : 16; - uint32_t reserved4[3]; -} __attribute__((packed)) XHCIEndpointContext; - -typedef volatile struct { - XHCISlotContext slot; - XHCIEndpointContext endpoints[32]; -} __attribute__((packed)) XHCIDevice; - -typedef volatile struct { - uint32_t dropContextFlags; - uint32_t addContextFlags; - uint32_t reserved[5]; - uint32_t configuration : 8, interfaceNumber : 8, AlternateSetting : 8, - reserved1 : 8; -} __attribute__((packed)) XHCIInputControl; - -typedef volatile struct { - XHCIInputControl inputControl; - XHCIDevice deviceContext; -} __attribute__((packed)) XHCIInputContext; +extern uint32_t serviceId; typedef volatile struct { uint8_t size; @@ -168,35 +25,6 @@ } __attribute__((packed)) UsbDeviceDescriptor; typedef struct { - XHCITRB *trbs, *physical; - uint32_t size, enqueue, dequeue; - bool cycle; -} TrbRing; - -typedef struct { - XHCICapabilities *capabilities; - XHCIOperational *operational; - XHCIRuntime *runtime; - TrbRing commands; - TrbRing events; - uint32_t pciDevice; - volatile uint32_t *doorbells; - XHCIEventRingSegmentTableEntry *eventRingSegmentTable, - *eventRingSegmentTablePhysical; - uint64_t *deviceContextBaseAddressArray; - uint32_t portCount; - XHCIInputContext *inputContexts[32]; -} XHCIController; - -typedef struct { - XHCIController *controller; - uint32_t slotIndex, portIndex; - XHCIInputContext *inputContext; - XHCIPort *port; - TrbRing *controlRing; -} SlotXHCI; - -typedef struct { uint32_t pciClass; void *(*initialize)(uint32_t, uint32_t, uint32_t); void (*getDeviceDescriptor)(void *, uint32_t, uint32_t, void *); diff --git a/src/userland/usb/main.c b/src/userland/usb/main.c index 82637d1..cb58a2f 100644 --- a/src/userland/usb/main.c +++ b/src/userland/usb/main.c @@ -2,30 +2,10 @@ #include #include "../hlib/include/syscalls.h" -#include "xhci/commands.h" -#include "xhci/controller.h" -#include "xhci/trbRing.h" #include uint32_t serviceId; -#define REQUEST(functionName, service, function) \ - uint32_t functionName(uint32_t data1, uint32_t data2) { \ - static uint32_t serviceId, functionId, initialized = false; \ - if (!initialized) { \ - while (!serviceId) { \ - serviceId = getService(service); \ - serviceId = getService(service); \ - } \ - while (!functionId) { \ - functionId = getFunction(serviceId, function); \ - functionId = getFunction(serviceId, function); \ - } \ - initialized = true; \ - } \ - return request(serviceId, functionId, data1, data2); \ - } - REQUEST(getBaseAddress, "lspci", "getBaseAddress"); REQUEST(getDeviceClass, "lspci", "getDeviceClass"); REQUEST(enableBusMaster, "lspci", "enableBusMaster"); @@ -51,7 +31,7 @@ UsbDeviceDescriptor *descriptor = malloc(sizeof(UsbDeviceDescriptor)); slot->interface->getDeviceDescriptor(slot->data, 1 << 8, 0, buffer); memcpy(buffer, (void *)descriptor, sizeof(UsbDeviceDescriptor)); - printf("slot %i: usb version %x.%x\n", slot->portIndex, + printf("port %i: usb version %x.%x\n", slot->portIndex, descriptor->usbVersion >> 8, descriptor->usbVersion & 0xFF); printf("port %i: class: %i, subclass: %i, protocol %i, maxPacketSize: %i\n", slot->portIndex, descriptor->deviceClass, descriptor->deviceSubclass, diff --git a/src/userland/usb/xhci/commands.c b/src/userland/usb/xhci/commands.c index 62455b5..f0a99b5 100644 --- a/src/userland/usb/xhci/commands.c +++ b/src/userland/usb/xhci/commands.c @@ -1,6 +1,7 @@ #include "commands.h" #include "../../hlib/include/syscalls.h" #include "trbRing.h" +#include "xhci.h" #include #include diff --git a/src/userland/usb/xhci/commands.h b/src/userland/usb/xhci/commands.h index 6c8a694..cae27b6 100644 --- a/src/userland/usb/xhci/commands.h +++ b/src/userland/usb/xhci/commands.h @@ -1,8 +1,8 @@ #ifndef COMMANDS_H #define COMMANDS_H +#include "xhci.h" #include -#include extern void addressDevice(SlotXHCI *slot, bool BSR); extern void configureEndpoint(XHCIController *controller, void *inputContext, diff --git a/src/userland/usb/xhci/controller.c b/src/userland/usb/xhci/controller.c index 234384f..8c1ab5b 100644 --- a/src/userland/usb/xhci/controller.c +++ b/src/userland/usb/xhci/controller.c @@ -2,6 +2,7 @@ #include "../../hlib/include/syscalls.h" #include "commands.h" #include "trbRing.h" +#include "xhci.h" #include #include diff --git a/src/userland/usb/xhci/controller.h b/src/userland/usb/xhci/controller.h index 568c274..61355b0 100644 --- a/src/userland/usb/xhci/controller.h +++ b/src/userland/usb/xhci/controller.h @@ -1,6 +1,7 @@ #ifndef XHCI_CONTROLLER_H #define XHCI_CONTROLLER_H +#include "xhci.h" #include extern XHCIController *xhciSetup(uint32_t deviceId, uint32_t bar0, diff --git a/src/userland/usb/xhci/trbRing.c b/src/userland/usb/xhci/trbRing.c index b487be8..fac19e1 100644 --- a/src/userland/usb/xhci/trbRing.c +++ b/src/userland/usb/xhci/trbRing.c @@ -1,3 +1,4 @@ +#include "xhci.h" #include XHCITRB *enqueueCommand(TrbRing *ring, XHCITRB *trb) { diff --git a/src/userland/usb/xhci/trbRing.h b/src/userland/usb/xhci/trbRing.h index bd31092..b0a7a3f 100644 --- a/src/userland/usb/xhci/trbRing.h +++ b/src/userland/usb/xhci/trbRing.h @@ -1,7 +1,7 @@ #ifndef TRB_RING_H #define TRB_RING_H -#include +#include "xhci.h" extern XHCITRB *enqueueCommand(TrbRing *ring, XHCITRB *trb); extern XHCITRB *trbRingFetch(TrbRing *ring, uint32_t *index); diff --git a/src/userland/usb/xhci/xhci.c b/src/userland/usb/xhci/xhci.c index b72ef2f..2bc5c29 100644 --- a/src/userland/usb/xhci/xhci.c +++ b/src/userland/usb/xhci/xhci.c @@ -1,3 +1,4 @@ +#include "xhci.h" #include "../../hlib/include/syscalls.h" #include "commands.h" #include "controller.h" diff --git a/src/include/hlib.h b/src/include/hlib.h index d38c59d..dac6387 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -75,4 +75,21 @@ extern void _listAdd(void *(malloc)(uint32_t), ListElement **list, void *data); #define listAdd(list, data) _listAdd(malloc, list, data) +#define REQUEST(functionName, service, function) \ + uint32_t functionName(uint32_t data1, uint32_t data2) { \ + static uint32_t serviceId, functionId, initialized = false; \ + if (!initialized) { \ + while (!serviceId) { \ + serviceId = getService(service); \ + serviceId = getService(service); \ + } \ + while (!functionId) { \ + functionId = getFunction(serviceId, function); \ + functionId = getFunction(serviceId, function); \ + } \ + initialized = true; \ + } \ + return request(serviceId, functionId, data1, data2); \ + } + #endif diff --git a/src/userland/usb/include/usb.h b/src/userland/usb/include/usb.h index 009e306..0b26f5f 100644 --- a/src/userland/usb/include/usb.h +++ b/src/userland/usb/include/usb.h @@ -5,150 +5,7 @@ #define OFFSET(ptr, off) (((void *)(ptr)) + (off)) -extern uint32_t i; - -typedef volatile struct { - uint8_t capabilitiesLength; - uint8_t reserved; - uint16_t interfaceVersion; - uint32_t structuralParameters[3]; - uint32_t capabilityParameter1; - uint32_t doorbellOffset; - uint32_t runtimeOffset; - uint32_t capabilityParameter2; -} __attribute__((packed)) XHCICapabilities; - -typedef volatile struct { - uint32_t status; - uint32_t powerStatus; - uint32_t linkInfo; - uint32_t hardwareLPM; -} __attribute__((packed)) XHCIPort; - -typedef volatile struct { - uint32_t command; - uint32_t status; - uint32_t pageSize; - uint32_t reserved[2]; - uint32_t notification; - uint32_t commandRingControl[2]; - uint32_t reserved1[4]; - uint32_t deviceContextBaseAddressArray[2]; - uint32_t config; - uint8_t reserved2[964]; - XHCIPort ports[256]; -} __attribute__((packed)) XHCIOperational; - -typedef volatile struct { - uint32_t dataLow; - uint32_t dataHigh; - uint32_t status; - uint32_t control; -} __attribute__((packed)) XHCITRB; - -#define COMMAND_TYPE(x) (x << 10) -#define COMMAND_CYCLE(x) (x) -#define COMMAND_BSR(x) (x << 9) -#define COMMAND_SLOT_ID(x) (x << 24) - -typedef volatile struct { - uint32_t commandLow; - uint32_t commandHigh; - uint32_t completionParameter : 24, completionCode : 8; - uint32_t cycle : 1, reserved : 9, type : 6, vfId : 8, slotId : 8; -} __attribute__((packed)) CommandCompletionEvent; - -typedef volatile struct { - uint32_t management; - uint32_t moderationCounter : 16, moderationInterval : 16; - uint32_t eventRingSegmentTableSize; // MAX 16 bit - uint32_t reserved2; - uint32_t eventRingSegmentTableAddress[2]; - uint32_t eventRingDequeuePointer[2]; -} __attribute__((packed)) XHCIInterrupter; - -typedef volatile struct { - uint8_t microframeIndex; - uint8_t reserved[0x20 - 1]; - XHCIInterrupter interrupters[1023]; -} __attribute__((packed)) XHCIRuntime; - -typedef volatile struct { - uint32_t ringSegmentBaseAddress[2]; - uint16_t ringSegmentSize, reserved; - uint32_t reserved1; -} __attribute__((packed)) XHCIEventRingSegmentTableEntry; - -typedef volatile struct { - uint32_t requestType : 8, request : 8, value : 16; - uint32_t index : 16, length : 16; - - uint32_t transferLength : 17, // always 8 - reserved : 5, interrupterTarget : 10; - - uint32_t cycle : 1, reserved1 : 4, interruptOnCompletion : 1, - immediateData : 1, // here 1 - reserved2 : 3, type : 6, transferType : 2; -} __attribute__((packed)) XHCISetupStageTRB; - -typedef volatile struct { - uint32_t reserved[2]; - - uint32_t reserved1 : 22, interrupterTarget : 10; - - uint32_t cycle : 1, evaluateNext : 1, reserved2 : 2, chain : 1, - interruptOnCompletion : 1, reserved3 : 4, type : 6, inDirection : 1; -} __attribute__((packed)) XHCIStatusStageTRB; - -typedef volatile struct { - uint32_t dataBuffer[2]; - - uint32_t transferSize : 17, tdSize : 5, interrupterTarget : 10; - - uint32_t cycle : 1, evaluateNext : 1, interruptOnShortPacket : 1, - noSnoop : 1, chain : 1, interruptOnCompletion : 1, immediateData : 1, - reserved : 3, type : 6, inDirection : 1; -} __attribute__((packed)) XHCIDataStageTRB; - -typedef volatile struct { - uint32_t routeString : 20, speed : 4, reserved : 1, multiTT : 1, isHub : 1, - contextEntryCount : 5; - uint32_t maxLatency : 16, rootHubPort : 8, portCount : 8; - uint32_t parentHubSlotId : 8, partentPortNumber : 8, thinkTime : 2, - reserved1 : 4, interrupterTarget : 10; - uint32_t deviceAddress : 8, reserved2 : 19, slotState : 5; - uint32_t reserved3[4]; -} __attribute__((packed)) XHCISlotContext; - -typedef volatile struct { - uint32_t endpointState : 3, reserved : 5, multiplier : 2, - maxPrimaryStreams : 5, linearStreamArray : 1, interval : 8, - maxEISTPayloadHigh : 8; - uint32_t reserved1 : 1, errorCount : 2, endpointType : 3, reserved2 : 1, - hostInitiateDisable : 1, maxBurstSize : 8, maxPacketSize : 16; - uint32_t transferDequeuePointerLow; - uint32_t transferDequeuePointerHigh; - uint32_t averageTRBLength : 16, maxEISTPayloadLow : 16; - uint32_t reserved4[3]; -} __attribute__((packed)) XHCIEndpointContext; - -typedef volatile struct { - XHCISlotContext slot; - XHCIEndpointContext endpoints[32]; -} __attribute__((packed)) XHCIDevice; - -typedef volatile struct { - uint32_t dropContextFlags; - uint32_t addContextFlags; - uint32_t reserved[5]; - uint32_t configuration : 8, interfaceNumber : 8, AlternateSetting : 8, - reserved1 : 8; -} __attribute__((packed)) XHCIInputControl; - -typedef volatile struct { - XHCIInputControl inputControl; - XHCIDevice deviceContext; -} __attribute__((packed)) XHCIInputContext; +extern uint32_t serviceId; typedef volatile struct { uint8_t size; @@ -168,35 +25,6 @@ } __attribute__((packed)) UsbDeviceDescriptor; typedef struct { - XHCITRB *trbs, *physical; - uint32_t size, enqueue, dequeue; - bool cycle; -} TrbRing; - -typedef struct { - XHCICapabilities *capabilities; - XHCIOperational *operational; - XHCIRuntime *runtime; - TrbRing commands; - TrbRing events; - uint32_t pciDevice; - volatile uint32_t *doorbells; - XHCIEventRingSegmentTableEntry *eventRingSegmentTable, - *eventRingSegmentTablePhysical; - uint64_t *deviceContextBaseAddressArray; - uint32_t portCount; - XHCIInputContext *inputContexts[32]; -} XHCIController; - -typedef struct { - XHCIController *controller; - uint32_t slotIndex, portIndex; - XHCIInputContext *inputContext; - XHCIPort *port; - TrbRing *controlRing; -} SlotXHCI; - -typedef struct { uint32_t pciClass; void *(*initialize)(uint32_t, uint32_t, uint32_t); void (*getDeviceDescriptor)(void *, uint32_t, uint32_t, void *); diff --git a/src/userland/usb/main.c b/src/userland/usb/main.c index 82637d1..cb58a2f 100644 --- a/src/userland/usb/main.c +++ b/src/userland/usb/main.c @@ -2,30 +2,10 @@ #include #include "../hlib/include/syscalls.h" -#include "xhci/commands.h" -#include "xhci/controller.h" -#include "xhci/trbRing.h" #include uint32_t serviceId; -#define REQUEST(functionName, service, function) \ - uint32_t functionName(uint32_t data1, uint32_t data2) { \ - static uint32_t serviceId, functionId, initialized = false; \ - if (!initialized) { \ - while (!serviceId) { \ - serviceId = getService(service); \ - serviceId = getService(service); \ - } \ - while (!functionId) { \ - functionId = getFunction(serviceId, function); \ - functionId = getFunction(serviceId, function); \ - } \ - initialized = true; \ - } \ - return request(serviceId, functionId, data1, data2); \ - } - REQUEST(getBaseAddress, "lspci", "getBaseAddress"); REQUEST(getDeviceClass, "lspci", "getDeviceClass"); REQUEST(enableBusMaster, "lspci", "enableBusMaster"); @@ -51,7 +31,7 @@ UsbDeviceDescriptor *descriptor = malloc(sizeof(UsbDeviceDescriptor)); slot->interface->getDeviceDescriptor(slot->data, 1 << 8, 0, buffer); memcpy(buffer, (void *)descriptor, sizeof(UsbDeviceDescriptor)); - printf("slot %i: usb version %x.%x\n", slot->portIndex, + printf("port %i: usb version %x.%x\n", slot->portIndex, descriptor->usbVersion >> 8, descriptor->usbVersion & 0xFF); printf("port %i: class: %i, subclass: %i, protocol %i, maxPacketSize: %i\n", slot->portIndex, descriptor->deviceClass, descriptor->deviceSubclass, diff --git a/src/userland/usb/xhci/commands.c b/src/userland/usb/xhci/commands.c index 62455b5..f0a99b5 100644 --- a/src/userland/usb/xhci/commands.c +++ b/src/userland/usb/xhci/commands.c @@ -1,6 +1,7 @@ #include "commands.h" #include "../../hlib/include/syscalls.h" #include "trbRing.h" +#include "xhci.h" #include #include diff --git a/src/userland/usb/xhci/commands.h b/src/userland/usb/xhci/commands.h index 6c8a694..cae27b6 100644 --- a/src/userland/usb/xhci/commands.h +++ b/src/userland/usb/xhci/commands.h @@ -1,8 +1,8 @@ #ifndef COMMANDS_H #define COMMANDS_H +#include "xhci.h" #include -#include extern void addressDevice(SlotXHCI *slot, bool BSR); extern void configureEndpoint(XHCIController *controller, void *inputContext, diff --git a/src/userland/usb/xhci/controller.c b/src/userland/usb/xhci/controller.c index 234384f..8c1ab5b 100644 --- a/src/userland/usb/xhci/controller.c +++ b/src/userland/usb/xhci/controller.c @@ -2,6 +2,7 @@ #include "../../hlib/include/syscalls.h" #include "commands.h" #include "trbRing.h" +#include "xhci.h" #include #include diff --git a/src/userland/usb/xhci/controller.h b/src/userland/usb/xhci/controller.h index 568c274..61355b0 100644 --- a/src/userland/usb/xhci/controller.h +++ b/src/userland/usb/xhci/controller.h @@ -1,6 +1,7 @@ #ifndef XHCI_CONTROLLER_H #define XHCI_CONTROLLER_H +#include "xhci.h" #include extern XHCIController *xhciSetup(uint32_t deviceId, uint32_t bar0, diff --git a/src/userland/usb/xhci/trbRing.c b/src/userland/usb/xhci/trbRing.c index b487be8..fac19e1 100644 --- a/src/userland/usb/xhci/trbRing.c +++ b/src/userland/usb/xhci/trbRing.c @@ -1,3 +1,4 @@ +#include "xhci.h" #include XHCITRB *enqueueCommand(TrbRing *ring, XHCITRB *trb) { diff --git a/src/userland/usb/xhci/trbRing.h b/src/userland/usb/xhci/trbRing.h index bd31092..b0a7a3f 100644 --- a/src/userland/usb/xhci/trbRing.h +++ b/src/userland/usb/xhci/trbRing.h @@ -1,7 +1,7 @@ #ifndef TRB_RING_H #define TRB_RING_H -#include +#include "xhci.h" extern XHCITRB *enqueueCommand(TrbRing *ring, XHCITRB *trb); extern XHCITRB *trbRingFetch(TrbRing *ring, uint32_t *index); diff --git a/src/userland/usb/xhci/xhci.c b/src/userland/usb/xhci/xhci.c index b72ef2f..2bc5c29 100644 --- a/src/userland/usb/xhci/xhci.c +++ b/src/userland/usb/xhci/xhci.c @@ -1,3 +1,4 @@ +#include "xhci.h" #include "../../hlib/include/syscalls.h" #include "commands.h" #include "controller.h" diff --git a/src/userland/usb/xhci/xhci.h b/src/userland/usb/xhci/xhci.h index e69de29..47fd9fa 100644 --- a/src/userland/usb/xhci/xhci.h +++ b/src/userland/usb/xhci/xhci.h @@ -0,0 +1,179 @@ +#ifndef XHCI_H +#define XHCI_H + +#include +#include + +typedef volatile struct { + uint8_t capabilitiesLength; + uint8_t reserved; + uint16_t interfaceVersion; + uint32_t structuralParameters[3]; + uint32_t capabilityParameter1; + uint32_t doorbellOffset; + uint32_t runtimeOffset; + uint32_t capabilityParameter2; +} __attribute__((packed)) XHCICapabilities; + +typedef volatile struct { + uint32_t status; + uint32_t powerStatus; + uint32_t linkInfo; + uint32_t hardwareLPM; +} __attribute__((packed)) XHCIPort; + +typedef volatile struct { + uint32_t command; + uint32_t status; + uint32_t pageSize; + uint32_t reserved[2]; + uint32_t notification; + uint32_t commandRingControl[2]; + uint32_t reserved1[4]; + uint32_t deviceContextBaseAddressArray[2]; + uint32_t config; + uint8_t reserved2[964]; + XHCIPort ports[256]; +} __attribute__((packed)) XHCIOperational; + +typedef volatile struct { + uint32_t dataLow; + uint32_t dataHigh; + uint32_t status; + uint32_t control; +} __attribute__((packed)) XHCITRB; + +#define COMMAND_TYPE(x) (x << 10) +#define COMMAND_CYCLE(x) (x) +#define COMMAND_BSR(x) (x << 9) +#define COMMAND_SLOT_ID(x) (x << 24) + +typedef volatile struct { + uint32_t commandLow; + uint32_t commandHigh; + uint32_t completionParameter : 24, completionCode : 8; + uint32_t cycle : 1, reserved : 9, type : 6, vfId : 8, slotId : 8; +} __attribute__((packed)) CommandCompletionEvent; + +typedef volatile struct { + uint32_t management; + uint32_t moderationCounter : 16, moderationInterval : 16; + uint32_t eventRingSegmentTableSize; // MAX 16 bit + uint32_t reserved2; + uint32_t eventRingSegmentTableAddress[2]; + uint32_t eventRingDequeuePointer[2]; +} __attribute__((packed)) XHCIInterrupter; + +typedef volatile struct { + uint8_t microframeIndex; + uint8_t reserved[0x20 - 1]; + XHCIInterrupter interrupters[1023]; +} __attribute__((packed)) XHCIRuntime; + +typedef volatile struct { + uint32_t ringSegmentBaseAddress[2]; + uint16_t ringSegmentSize, reserved; + uint32_t reserved1; +} __attribute__((packed)) XHCIEventRingSegmentTableEntry; + +typedef volatile struct { + uint32_t requestType : 8, request : 8, value : 16; + uint32_t index : 16, length : 16; + + uint32_t transferLength : 17, // always 8 + reserved : 5, interrupterTarget : 10; + + uint32_t cycle : 1, reserved1 : 4, interruptOnCompletion : 1, + immediateData : 1, // here 1 + reserved2 : 3, type : 6, transferType : 2; +} __attribute__((packed)) XHCISetupStageTRB; + +typedef volatile struct { + uint32_t reserved[2]; + + uint32_t reserved1 : 22, interrupterTarget : 10; + + uint32_t cycle : 1, evaluateNext : 1, reserved2 : 2, chain : 1, + interruptOnCompletion : 1, reserved3 : 4, type : 6, inDirection : 1; +} __attribute__((packed)) XHCIStatusStageTRB; + +typedef volatile struct { + uint32_t dataBuffer[2]; + + uint32_t transferSize : 17, tdSize : 5, interrupterTarget : 10; + + uint32_t cycle : 1, evaluateNext : 1, interruptOnShortPacket : 1, + noSnoop : 1, chain : 1, interruptOnCompletion : 1, immediateData : 1, + reserved : 3, type : 6, inDirection : 1; +} __attribute__((packed)) XHCIDataStageTRB; + +typedef volatile struct { + uint32_t routeString : 20, speed : 4, reserved : 1, multiTT : 1, isHub : 1, + contextEntryCount : 5; + uint32_t maxLatency : 16, rootHubPort : 8, portCount : 8; + uint32_t parentHubSlotId : 8, partentPortNumber : 8, thinkTime : 2, + reserved1 : 4, interrupterTarget : 10; + uint32_t deviceAddress : 8, reserved2 : 19, slotState : 5; + uint32_t reserved3[4]; +} __attribute__((packed)) XHCISlotContext; + +typedef volatile struct { + uint32_t endpointState : 3, reserved : 5, multiplier : 2, + maxPrimaryStreams : 5, linearStreamArray : 1, interval : 8, + maxEISTPayloadHigh : 8; + uint32_t reserved1 : 1, errorCount : 2, endpointType : 3, reserved2 : 1, + hostInitiateDisable : 1, maxBurstSize : 8, maxPacketSize : 16; + uint32_t transferDequeuePointerLow; + uint32_t transferDequeuePointerHigh; + uint32_t averageTRBLength : 16, maxEISTPayloadLow : 16; + uint32_t reserved4[3]; +} __attribute__((packed)) XHCIEndpointContext; + +typedef volatile struct { + XHCISlotContext slot; + XHCIEndpointContext endpoints[32]; +} __attribute__((packed)) XHCIDevice; + +typedef volatile struct { + uint32_t dropContextFlags; + uint32_t addContextFlags; + uint32_t reserved[5]; + uint32_t configuration : 8, interfaceNumber : 8, AlternateSetting : 8, + reserved1 : 8; +} __attribute__((packed)) XHCIInputControl; + +typedef volatile struct { + XHCIInputControl inputControl; + XHCIDevice deviceContext; +} __attribute__((packed)) XHCIInputContext; + +typedef struct { + XHCITRB *trbs, *physical; + uint32_t size, enqueue, dequeue; + bool cycle; +} TrbRing; + +typedef struct { + XHCICapabilities *capabilities; + XHCIOperational *operational; + XHCIRuntime *runtime; + TrbRing commands; + TrbRing events; + uint32_t pciDevice; + volatile uint32_t *doorbells; + XHCIEventRingSegmentTableEntry *eventRingSegmentTable, + *eventRingSegmentTablePhysical; + uint64_t *deviceContextBaseAddressArray; + uint32_t portCount; + XHCIInputContext *inputContexts[32]; +} XHCIController; + +typedef struct { + XHCIController *controller; + uint32_t slotIndex, portIndex; + XHCIInputContext *inputContext; + XHCIPort *port; + TrbRing *controlRing; +} SlotXHCI; + +#endif