From: John Hodge Date: Sun, 16 Sep 2012 04:06:24 +0000 (+0800) Subject: Modules/USB - EHCI Driver and related changes to hub code X-Git-Tag: rel0.15~706^2~10 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=86102de82180e8e3eac12475f065cec5928640dd;p=tpg%2Facess2.git Modules/USB - EHCI Driver and related changes to hub code --- diff --git a/KernelLand/Modules/USB/Core/Makefile b/KernelLand/Modules/USB/Core/Makefile index 33e311b0..8dbe04ed 100644 --- a/KernelLand/Modules/USB/Core/Makefile +++ b/KernelLand/Modules/USB/Core/Makefile @@ -3,7 +3,7 @@ OBJ = main.o OBJ += usb.o usb_lowlevel.o usb_devinit.o usb_io.o usb_poll.o usb_info.o -OBJ += hub.o +OBJ += hub.o portctl.o CPPFLAGS = -Iinclude NAME = Core diff --git a/KernelLand/Modules/USB/Core/hub.c b/KernelLand/Modules/USB/Core/hub.c index 7bc4dc27..23f3a361 100644 --- a/KernelLand/Modules/USB/Core/hub.c +++ b/KernelLand/Modules/USB/Core/hub.c @@ -17,21 +17,6 @@ // resvd #define SET_FEATURE 3 -#define PORT_CONNECTION 0 -#define PORT_ENABLE 1 -#define PORT_SUSPEND 2 -#define PORT_OVER_CURRENT 3 -#define PORT_RESET 4 -#define PORT_POWER 8 -#define PORT_LOW_SPEED 9 -#define C_PORT_CONNECTION 16 -#define C_PORT_ENABLE 17 -#define C_PORT_SUSPEND 18 -#define C_PORT_OVER_CURRENT 19 -#define C_PORT_RESET 20 -#define PORT_TEST 21 -#define PORT_INDICATOR 21 - struct sHubDescriptor { Uint8 DescLength; @@ -56,6 +41,9 @@ void Hub_Connected(tUSBInterface *Dev, void *Descriptors, size_t Length); void Hub_Disconnected(tUSBInterface *Dev); void Hub_PortStatusChange(tUSBInterface *Dev, int Endpoint, int Length, void *Data); void Hub_int_HandleChange(tUSBInterface *Dev, int Port); +void Hub_SetPortFeature(tUSBInterface *Dev, int Port, int Feat); +void Hub_ClearPortFeature(tUSBInterface *HubDev, int Port, int Feat); +int Hub_GetPortStatus(tUSBInterface *HubDev, int Port, int Flag); // === GLOBALS === tUSBDriver gUSBHub_Driver = { @@ -150,13 +138,8 @@ void Hub_int_HandleChange(tUSBInterface *Dev, int Port) // - Power on port USB_Request(Dev, 0, 0x23, SET_FEATURE, PORT_POWER, Port, 0, NULL); Time_Delay(info->PowerOnDelay); - // - Reset - USB_Request(Dev, 0, 0x23, SET_FEATURE, PORT_RESET, Port, 0, NULL); - Time_Delay(20); // Spec says 10ms after reset, but how long is reset? - // - Enable - USB_Request(Dev, 0, 0x23, SET_FEATURE, PORT_ENABLE, Port, 0, NULL); - // - Poke USB Stack - USB_DeviceConnected(info->HubPtr, Port); + // - Start reset process + USB_PortCtl_BeginReset(info->HubPtr, Port); } else { // Disconnected @@ -179,3 +162,20 @@ void Hub_int_HandleChange(tUSBInterface *Dev, int Port) USB_Request(Dev, 0, 0x23, CLEAR_FEATURE, C_PORT_RESET, Port, 0, NULL); } } + +void Hub_SetPortFeature(tUSBInterface *Dev, int Port, int Feat) +{ + USB_Request(Dev, 0, 0x23, SET_FEATURE, Feat, Port, 0, NULL); +} + +void Hub_ClearPortFeature(tUSBInterface *Dev, int Port, int Feat) +{ + USB_Request(Dev, 0, 0x23, CLEAR_FEATURE, Feat, Port, 0, NULL); +} + +int Hub_GetPortStatus(tUSBInterface *Dev, int Port, int Flag) +{ + Uint16 status[2]; // Status, Change + USB_Request(Dev, 0, 0xA3, GET_STATUS, 0, Port, 4, status); + return !!(status[0] & (1 << Flag)); +} diff --git a/KernelLand/Modules/USB/Core/include/usb_host.h b/KernelLand/Modules/USB/Core/include/usb_host.h index 8b4f96da..7806e1bc 100644 --- a/KernelLand/Modules/USB/Core/include/usb_host.h +++ b/KernelLand/Modules/USB/Core/include/usb_host.h @@ -50,7 +50,11 @@ struct sUSBHostDef tUSBBulkOp SendBulk; void (*FreeOp)(void *Ptr, void *Handle); + // Root hub stuff void (*CheckPorts)(void *Ptr); + void (*SetPortFeature)(void *Ptr, int PortNum, int Feat); + void (*ClearPortFeature)(void *Ptr, int PortNum, int Feat); + int (*GetPortStatus)(void *Ptr, int PortNum, int Flag); }; extern tUSBHub *USB_RegisterHost(tUSBHostDef *HostDef, void *ControllerPtr, int nPorts); diff --git a/KernelLand/Modules/USB/Core/include/usb_hub.h b/KernelLand/Modules/USB/Core/include/usb_hub.h index 6b32d49c..b38b1017 100644 --- a/KernelLand/Modules/USB/Core/include/usb_hub.h +++ b/KernelLand/Modules/USB/Core/include/usb_hub.h @@ -23,5 +23,26 @@ extern void USB_RemoveHub(tUSBHub *Hub); extern void USB_DeviceConnected(tUSBHub *Hub, int Port); extern void USB_DeviceDisconnected(tUSBHub *Hub, int Port); +#define PORT_CONNECTION 0 +#define PORT_ENABLE 1 +#define PORT_SUSPEND 2 +#define PORT_OVER_CURRENT 3 +#define PORT_RESET 4 +#define PORT_POWER 8 +#define PORT_LOW_SPEED 9 +#define C_PORT_CONNECTION 16 +#define C_PORT_ENABLE 17 +#define C_PORT_SUSPEND 18 +#define C_PORT_OVER_CURRENT 19 +#define C_PORT_RESET 20 +#define PORT_TEST 21 +#define PORT_INDICATOR 21 + +extern void Hub_SetPortFeature(tUSBInterface *HubDev, int Port, int Feat); +extern void Hub_ClearPortFeature(tUSBInterface *HubDev, int Port, int Feat); +extern int Hub_GetPortStatus(tUSBInterface *HubDev, int Port, int Flag); + +extern void USB_PortCtl_BeginReset(tUSBHub *Hub, int Port); + #endif diff --git a/KernelLand/Modules/USB/Core/main.c b/KernelLand/Modules/USB/Core/main.c index cdbe2ee7..04645507 100644 --- a/KernelLand/Modules/USB/Core/main.c +++ b/KernelLand/Modules/USB/Core/main.c @@ -13,6 +13,7 @@ // === IMPORTS === extern void USB_PollThread(void *unused); extern void USB_AsyncThread(void *Unused); +extern void USB_PortCtl_Init(void); // === PROTOTYPES === int USB_Install(char **Arguments); @@ -43,6 +44,7 @@ tDevFS_Driver gUSB_DrvInfo = { */ int USB_Install(char **Arguments) { + USB_PortCtl_Init(); Proc_SpawnWorker(USB_PollThread, NULL); Proc_SpawnWorker(USB_AsyncThread, NULL); diff --git a/KernelLand/Modules/USB/Core/portctl.c b/KernelLand/Modules/USB/Core/portctl.c new file mode 100644 index 00000000..1f6cf863 --- /dev/null +++ b/KernelLand/Modules/USB/Core/portctl.c @@ -0,0 +1,123 @@ +/* + * Acess2 USB Stack + * - By John Hodge (thePowersGang) + * + * portctl.c + * - Port control code + */ +#define DEBUG 1 +#define SANITY 1 +#include +#include "usb.h" +#include +#include +#include + +// === PROTOTYPES === +void USB_PortCtl_Init(void); +void USB_PortCtl_Worker(void *Unused); +void USB_PortCtl_SetPortFeature(tUSBHub *Hub, int Port, int Feat); +void USB_PortCtl_ClearPortFeature(tUSBHub *Hub, int Port, int Feat); + int USB_PortCtl_GetPortStatus(tUSBHub *Hub, int Port, int Flag); + +// === GLOBALS === +tWorkqueue gUSB_PortCtl_WorkQueue; + +// === CODE === +void USB_PortCtl_Init(void) +{ + Workqueue_Init(&gUSB_PortCtl_WorkQueue, "USB Port Reset Work Queue", offsetof(tUSBHubPort, ListNext)); + Proc_SpawnWorker(USB_PortCtl_Worker, NULL); +} + +void USB_PortCtl_Worker(void *Unused) +{ + Threads_SetName("USB PortCtl Worker"); + for(;;) + { + tUSBHubPort *port; + tUSBHub *hub; + + port = Workqueue_GetWork(&gUSB_PortCtl_WorkQueue); + if( !port ) { + Log_Warning("USB", "PortCtl Workqueue returned NULL"); + break; + } + hub = (tUSBHub*)(port - port->PortNum) - 1; + + LOG("port = %p, hub = %p", port, hub); + + switch(port->Status) + { + case 1: + // Assert reset + USB_PortCtl_SetPortFeature(hub, port->PortNum, PORT_RESET); + LOG("Port reset starting"); + // Wait 50 ms + Time_Delay(50); + USB_PortCtl_ClearPortFeature(hub, port->PortNum, PORT_RESET); + Time_Delay(10); // May take up to 2ms for reset to clear + // Enable port + LOG("Port enabling"); + USB_PortCtl_SetPortFeature(hub, port->PortNum, PORT_ENABLE); + // Begin connect processing + port->Status = 2; + USB_DeviceConnected(hub, port->PortNum); + break; + } + } +} + +void USB_PortCtl_BeginReset(tUSBHub *Hub, int Port) +{ + LOG("Starting %p %i", Hub, Port); + // Set status field in hub structure + Hub->Ports[Port].Status = 1; + Hub->Ports[Port].PortNum = Port; + // Add to the work queue + Workqueue_AddWork(&gUSB_PortCtl_WorkQueue, &Hub->Ports[Port]); +} + +void USB_PortCtl_SetPortFeature(tUSBHub *Hub, int Port, int Feat) +{ + if( Hub->Interface->Driver == NULL ) { + // - Host Port + tUSBHost *host = Hub->Interface->Dev->Host; + ASSERT(host->HostDef->SetPortFeature); + host->HostDef->SetPortFeature(host->Ptr, Port, Feat); + } + else { + // - Hub Port + Hub_SetPortFeature(Hub->Interface, Port, Feat); + } +} + +void USB_PortCtl_ClearPortFeature(tUSBHub *Hub, int Port, int Feat) +{ + if( Hub->Interface->Driver == NULL ) { + // - Host Port + tUSBHost *host = Hub->Interface->Dev->Host; + ASSERT(host->HostDef->ClearPortFeature); + host->HostDef->ClearPortFeature(host->Ptr, Port, Feat); + } + else { + // - Hub Port + Hub_ClearPortFeature(Hub->Interface, Port, Feat); + } +} + +int USB_PortCtl_GetPortStatus(tUSBHub *Hub, int Port, int Flag) +{ + if( Hub->Interface->Driver == NULL ) { + // - Host Port + tUSBHost *host = Hub->Interface->Dev->Host; + ASSERT(host->HostDef->GetPortStatus); + return host->HostDef->GetPortStatus(host->Ptr, Port, Flag); + } + else { + // - Hub Port + return Hub_GetPortStatus(Hub->Interface, Port, Flag); + } + return 0; +} + diff --git a/KernelLand/Modules/USB/Core/usb.c b/KernelLand/Modules/USB/Core/usb.c index 9a0e2bca..3234a778 100644 --- a/KernelLand/Modules/USB/Core/usb.c +++ b/KernelLand/Modules/USB/Core/usb.c @@ -31,7 +31,7 @@ tUSBHub *USB_RegisterHost(tUSBHostDef *HostDef, void *ControllerPtr, int nPorts) { tUSBHost *host; - host = malloc(sizeof(tUSBHost) + nPorts*sizeof(void*)); + host = malloc(sizeof(tUSBHost) + nPorts*sizeof(tUSBHubPort)); if(!host) { // Oh, bugger. return NULL; @@ -43,6 +43,7 @@ tUSBHub *USB_RegisterHost(tUSBHostDef *HostDef, void *ControllerPtr, int nPorts) host->RootHubDev.ParentHub = NULL; host->RootHubDev.Host = host; host->RootHubDev.Address = 0; + ASSERT(HostDef->InitControl); host->RootHubDev.EndpointHandles[0] = HostDef->InitControl(ControllerPtr, 0, 64); // host->RootHubIf.Next = NULL; @@ -53,7 +54,7 @@ tUSBHub *USB_RegisterHost(tUSBHostDef *HostDef, void *ControllerPtr, int nPorts) host->RootHub.Interface = &host->RootHubIf; host->RootHub.nPorts = nPorts; - memset(host->RootHub.Devices, 0, sizeof(void*)*nPorts); + memset(host->RootHub.Ports, 0, sizeof(tUSBHubPort)*nPorts); // Append to list Mutex_Acquire( &glUSB_Hosts ); @@ -98,10 +99,10 @@ tUSBHub *USB_RegisterHub(tUSBInterface *Device, int PortCount) { tUSBHub *ret; - ret = malloc(sizeof(tUSBHub) + sizeof(ret->Devices[0])*PortCount); + ret = malloc(sizeof(tUSBHub) + sizeof(ret->Ports[0])*PortCount); ret->Interface = Device; ret->nPorts = PortCount; - memset(ret->Devices, 0, sizeof(ret->Devices[0])*PortCount); + memset(ret->Ports, 0, sizeof(ret->Ports[0])*PortCount); return ret; } @@ -109,7 +110,7 @@ void USB_RemoveHub(tUSBHub *Hub) { for( int i = 0; i < Hub->nPorts; i ++ ) { - if( Hub->Devices[i] ) + if( Hub->Ports[i].Dev ) { USB_DeviceDisconnected( Hub, i ); } diff --git a/KernelLand/Modules/USB/Core/usb.h b/KernelLand/Modules/USB/Core/usb.h index 24cd80ad..c5c8cad6 100644 --- a/KernelLand/Modules/USB/Core/usb.h +++ b/KernelLand/Modules/USB/Core/usb.h @@ -13,11 +13,20 @@ #include #include "usb_proto.h" +typedef struct sUSBHubPort tUSBHubPort; typedef struct sUSBHost tUSBHost; typedef struct sUSBDevice tUSBDevice; typedef struct sUSBEndpoint tUSBEndpoint; // === STRUCTURES === +struct sUSBHubPort +{ + void *ListNext; + char Status; + char PortNum; + tUSBDevice *Dev; +}; + /** * \brief USB Hub data */ @@ -26,7 +35,7 @@ struct sUSBHub tUSBInterface *Interface; int nPorts; - tUSBDevice *Devices[]; + struct sUSBHubPort Ports[]; }; struct sUSBEndpoint diff --git a/KernelLand/Modules/USB/Core/usb_devinit.c b/KernelLand/Modules/USB/Core/usb_devinit.c index 6143bdbe..0da8c85c 100644 --- a/KernelLand/Modules/USB/Core/usb_devinit.c +++ b/KernelLand/Modules/USB/Core/usb_devinit.c @@ -29,7 +29,7 @@ void USB_DeviceConnected(tUSBHub *Hub, int Port) tUSBDevice tmpdev; tUSBDevice *dev = &tmpdev; if( Port >= Hub->nPorts ) return ; - if( Hub->Devices[Port] ) return ; + if( Hub->Ports[Port].Dev ) return ; ENTER("pHub iPort", Hub, Port); @@ -325,7 +325,7 @@ void USB_DeviceConnected(tUSBHub *Hub, int Port) free(full_buf); } - Hub->Devices[Port] = dev; + Hub->Ports[Port].Dev = dev; // Done. LEAVE('-'); @@ -334,11 +334,11 @@ void USB_DeviceConnected(tUSBHub *Hub, int Port) void USB_DeviceDisconnected(tUSBHub *Hub, int Port) { tUSBDevice *dev; - if( !Hub->Devices[Port] ) { + if( !Hub->Ports[Port].Dev ) { Log_Error("USB", "Non-registered device disconnected"); return; } - dev = Hub->Devices[Port]; + dev = Hub->Ports[Port].Dev; // TODO: Free related resources // - Endpoint registrations @@ -351,8 +351,9 @@ void USB_DeviceDisconnected(tUSBHub *Hub, int Port) // - Bus Address USB_int_DeallocateAddress(dev->Host, dev->Address); // - Inform handler - // - Allocate memory + // - Release memory free(dev); + Hub->Ports[Port].Dev = NULL; } void *USB_GetDeviceDataPtr(tUSBInterface *Dev) { return Dev->Data; } diff --git a/KernelLand/Modules/USB/Core/usb_poll.c b/KernelLand/Modules/USB/Core/usb_poll.c index a32e08e4..5a932693 100644 --- a/KernelLand/Modules/USB/Core/usb_poll.c +++ b/KernelLand/Modules/USB/Core/usb_poll.c @@ -81,7 +81,8 @@ int USB_PollThread(void *unused) // Check hosts for( tUSBHost *host = gUSB_Hosts; host; host = host->Next ) { - host->HostDef->CheckPorts(host->Ptr); + if( host->HostDef->CheckPorts ) + host->HostDef->CheckPorts(host->Ptr); } Time_Delay(100); diff --git a/KernelLand/Modules/USB/EHCI/ehci.c b/KernelLand/Modules/USB/EHCI/ehci.c index 645e142b..1f73cf6f 100644 --- a/KernelLand/Modules/USB/EHCI/ehci.c +++ b/KernelLand/Modules/USB/EHCI/ehci.c @@ -13,9 +13,13 @@ #include "ehci.h" #include #include +#include +#include // === CONSTANTS === #define EHCI_MAX_CONTROLLERS 4 +#define EHCI_THREADEVENT_IOC THREAD_EVENT_USER1 +#define EHCI_THREADEVENT_PORTSC THREAD_EVENT_USER2 // === PROTOTYPES === int EHCI_Initialise(char **Arguments); @@ -36,12 +40,17 @@ void *EHCI_SendControl(void *Ptr, void *Dest, tUSBHostCb Cb, void *CbData, ); void *EHCI_SendBulk(void *Ptr, void *Dest, tUSBHostCb Cb, void *CbData, int Dir, void *Data, size_t Length); void EHCI_FreeOp(void *Ptr, void *Handle); +Uint32 EHCI_int_RootHub_FeatToMask(int Feat); +void EHCI_RootHub_SetPortFeature(void *Ptr, int Port, int Feat); +void EHCI_RootHub_ClearPortFeature(void *Ptr, int Port, int Feat); + int EHCI_RootHub_GetPortStatus(void *Ptr, int Port, int Flag); // --- Internals --- tEHCI_qTD *EHCI_int_AllocateTD(tEHCI_Controller *Cont, int PID, void *Data, size_t Length, tUSBHostCb Cb, void *CbData); void EHCI_int_DeallocateTD(tEHCI_Controller *Cont, tEHCI_qTD *TD); void EHCI_int_AppendTD(tEHCI_QH *QH, tEHCI_qTD *TD); tEHCI_QH *EHCI_int_AllocateQH(tEHCI_Controller *Cont, int Endpoint, size_t MaxPacketSize); void EHCI_int_DeallocateQH(tEHCI_Controller *Cont, tEHCI_QH *QH); +void EHCI_int_InterruptThread(void *ControllerPtr); // === GLOBALS === MODULE_DEFINE(0, VERSION, USB_EHCI, EHCI_Initialise, NULL, "USB_Core", NULL); @@ -49,12 +58,18 @@ tEHCI_Controller gaEHCI_Controllers[EHCI_MAX_CONTROLLERS]; tUSBHostDef gEHCI_HostDef = { .InitInterrupt = EHCI_InitInterrupt, .InitIsoch = EHCI_InitIsoch, + .InitControl = EHCI_InitControl, .InitBulk = EHCI_InitBulk, .RemEndpoint = EHCI_RemEndpoint, .SendIsoch = NULL, .SendControl = EHCI_SendControl, .SendBulk = EHCI_SendBulk, - .FreeOp = EHCI_FreeOp + .FreeOp = EHCI_FreeOp, + + .CheckPorts = NULL, // No need + .SetPortFeature = EHCI_RootHub_SetPortFeature, + .ClearPortFeature = EHCI_RootHub_ClearPortFeature, + .GetPortStatus = EHCI_RootHub_GetPortStatus, }; // === CODE === @@ -103,23 +118,50 @@ int EHCI_InitController(tPAddr BaseAddress, Uint8 InterruptNum) break; } } - if(!cont) { - Log_Notice("EHCI", "Too many controllers (EHCI_MAX_CONTROLLERS=%i)", EHCI_MAX_CONTROLLERS); + Log_Notice("EHCI", "Too many controllers (EHCI_MAX_CONTROLLERS=%i)", + EHCI_MAX_CONTROLLERS); return 1; } + // - Nuke a couple of fields so error handling code doesn't derp + cont->CapRegs = NULL; + cont->PeriodicQueue = NULL; + // -- Build up structure -- cont->CapRegs = (void*)MM_MapHWPages(BaseAddress, 1); + if( !cont->CapRegs ) { + Log_Warning("EHCI", "Can't map 1 page at %P into kernel space", BaseAddress); + goto _error; + } // TODO: Error check + if( (cont->CapRegs->CapLength & 3) ) { + Log_Warning("EHCI", "Controller at %P non-aligned op regs", BaseAddress); + goto _error; + } cont->OpRegs = (void*)( (Uint32*)cont->CapRegs + cont->CapRegs->CapLength / 4 ); // - Allocate periodic queue - cont->PeriodicQueue = (void*)MM_AllocDMA(1, 32, NULL); + tPAddr unused; + cont->PeriodicQueue = (void*)MM_AllocDMA(1, 32, &unused); + if( !cont->PeriodicQueue ) { + Log_Warning("ECHI", "Can't allocate 1 32-bit page for periodic queue"); + goto _error; + } // TODO: Error check // > Populate queue + // Get port count + cont->nPorts = cont->CapRegs->HCSParams & 0xF; + + // -- Bind IRQ -- IRQ_AddHandler(InterruptNum, EHCI_InterruptHandler, cont); + cont->InterruptThread = Proc_SpawnWorker(EHCI_int_InterruptThread, cont); + if( !cont->InterruptThread ) { + Log_Warning("EHCI", "Can't spawn interrupt worker thread"); + goto _error; + } + LOG("cont->InterruptThread = %p", cont->InterruptThread); // -- Initialisation procedure (from ehci-r10) -- // - Reset controller @@ -133,17 +175,27 @@ int EHCI_InitController(tPAddr BaseAddress, Uint8 InterruptNum) cont->OpRegs->USBCmd = (0x40 << 16) | USBCMD_PeriodicEnable | USBCMD_Run; // - Route all ports cont->OpRegs->ConfigFlag = 1; - + + // -- Register with USB Core -- + cont->RootHub = USB_RegisterHost(&gEHCI_HostDef, cont, cont->nPorts); + return 0; +_error: + cont->PhysBase = 0; + if( cont->CapRegs ) + MM_Deallocate( (tVAddr)cont->CapRegs ); + if( cont->PeriodicQueue ) + MM_Deallocate( (tVAddr)cont->PeriodicQueue ); + return 2; } void EHCI_InterruptHandler(int IRQ, void *Ptr) { - tEHCI_Controller *cont = Ptr; - Uint32 sts = cont->OpRegs->USBSts; + tEHCI_Controller *Cont = Ptr; + Uint32 sts = Cont->OpRegs->USBSts; // Clear interrupts - cont->OpRegs->USBSts = sts; + Cont->OpRegs->USBSts = sts; if( sts & 0xFFFF0FC0 ) { LOG("Oops, reserved bits set (%08x), funny hardware?", sts); @@ -155,12 +207,14 @@ void EHCI_InterruptHandler(int IRQ, void *Ptr) if( sts & USBINTR_IOC ) { // IOC + Threads_PostEvent(Cont->InterruptThread, EHCI_THREADEVENT_IOC); sts &= ~USBINTR_IOC; } if( sts & USBINTR_PortChange ) { // Port change, determine what port and poke helper thread LOG("Port status change"); + Threads_PostEvent(Cont->InterruptThread, EHCI_THREADEVENT_PORTSC); sts &= ~USBINTR_PortChange; } @@ -216,6 +270,8 @@ void *EHCI_InitInterrupt(void *Ptr, int Endpoint, int bOutbound, int Period, tEHCI_QH *qh = EHCI_int_AllocateQH(Cont, Endpoint, Length); qh->Impl.IntPeriodPow = period_pow; + Mutex_Acquire(&Cont->PeriodicListLock); + // Choose an interrupt slot to use int minslot = 0, minslot_load = INT_MAX; for( int slot = 0; slot < Period; slot ++ ) @@ -230,8 +286,8 @@ void *EHCI_InitInterrupt(void *Ptr, int Endpoint, int bOutbound, int Period, } } // Increase loading on the selected slot - for( int i = 0; i < PERIODIC_SIZE; i += Period ) - Cont->InterruptLoad[i+minslot] += Length; + for( int i = minslot; i < PERIODIC_SIZE; i += Period ) + Cont->InterruptLoad[i] += Length; qh->Impl.IntOfs = minslot; // Allocate TD for the data @@ -239,6 +295,51 @@ void *EHCI_InitInterrupt(void *Ptr, int Endpoint, int bOutbound, int Period, EHCI_int_AppendTD(qh, td); // Insert into the periodic list + for( int i = 0; i < PERIODIC_SIZE; i += Period ) + { + // Walk list until + // - the end is reached + // - this QH is found + // - A QH with a lower period is encountered + tEHCI_QH *pqh = NULL; + tEHCI_QH *nqh; + for( nqh = Cont->PeriodicQueueV[i]; nqh; pqh = nqh, nqh = nqh->Impl.Next ) + { + if( nqh == qh ) + break; + if( nqh->Impl.IntPeriodPow < period_pow ) + break; + } + + // Somehow, we've already been added to this queue. + if( nqh && nqh == qh ) + continue ; + + if( qh->Impl.Next && qh->Impl.Next != nqh ) { + Log_Warning("EHCI", "Suspected bookkeeping error on %p - int list %i+%i overlap", + Cont, period_pow, minslot); + break; + } + + if( nqh ) { + qh->Impl.Next = nqh; + qh->HLink = MM_GetPhysAddr(nqh) | 2; + } + else { + qh->Impl.Next = NULL; + qh->HLink = 2|1; // QH, Terminate + } + + if( pqh ) { + pqh->Impl.Next = qh; + pqh->HLink = MM_GetPhysAddr(qh) | 2; + } + else { + Cont->PeriodicQueueV[i] = qh; + Cont->PeriodicQueue[i] = MM_GetPhysAddr(qh) | 2; + } + } + Mutex_Release(&Cont->PeriodicListLock); return qh; } @@ -263,6 +364,8 @@ void *EHCI_InitControl(void *Ptr, int Endpoint, size_t MaxPacketSize) Cont->OpRegs->AsyncListAddr = MM_GetPhysAddr(qh)|2; Cont->LastAsyncHead = qh; + LOG("Created %p for %p Ep 0x%x - %i bytes MPS", qh, Ptr, Endpoint, MaxPacketSize); + return qh; } void *EHCI_InitBulk(void *Ptr, int Endpoint, size_t MaxPacketSize) @@ -276,8 +379,16 @@ void EHCI_RemEndpoint(void *Ptr, void *Handle) else if( (tVAddr)Handle <= 256*16 ) return ; // Isoch else { + tEHCI_QH *qh = Ptr; + // Remove QH from list // - If it's a polling endpoint, need to remove from all periodic lists + if( qh->Impl.IntPeriodPow != 0xFF) { + // Poll + } + else { + // GP + } // Deallocate QH EHCI_int_DeallocateQH(Ptr, Handle); @@ -304,18 +415,19 @@ void *EHCI_SendControl(void *Ptr, void *Dest, tUSBHostCb Cb, void *CbData, td_setup = EHCI_int_AllocateTD(Cont, PID_SETUP, (void*)SetupData, SetupLength, NULL, NULL); if( isOutbound ) { - td_data = EHCI_int_AllocateTD(Cont, PID_OUT, (void*)OutData, OutLength, NULL, NULL); + td_data = OutData ? EHCI_int_AllocateTD(Cont, PID_OUT, (void*)OutData, OutLength, NULL, NULL) : NULL; td_status = EHCI_int_AllocateTD(Cont, PID_IN, InData, InLength, Cb, CbData); } else { - td_data = EHCI_int_AllocateTD(Cont, PID_IN, InData, InLength, NULL, NULL); + td_data = InData ? EHCI_int_AllocateTD(Cont, PID_IN, InData, InLength, NULL, NULL) : NULL; td_status = EHCI_int_AllocateTD(Cont, PID_OUT, (void*)OutData, OutLength, Cb, CbData); } // Append TDs EHCI_int_AppendTD(Dest, td_setup); - EHCI_int_AppendTD(Dest, td_data); + if( td_data ) + EHCI_int_AppendTD(Dest, td_data); EHCI_int_AppendTD(Dest, td_status); return td_status; @@ -344,28 +456,148 @@ void EHCI_FreeOp(void *Ptr, void *Handle) EHCI_int_DeallocateTD(Cont, Handle); } +Uint32 EHCI_int_RootHub_FeatToMask(int Feat) +{ + switch(Feat) + { + case PORT_RESET: return PORTSC_PortReset; + case PORT_ENABLE: return PORTSC_PortEnabled; + default: + Log_Warning("EHCI", "Unknown root hub port feature %i", Feat); + return 0; + } +} + +void EHCI_RootHub_SetPortFeature(void *Ptr, int Port, int Feat) +{ + tEHCI_Controller *Cont = Ptr; + if(Port >= Cont->nPorts) return; + + Cont->OpRegs->PortSC[Port] |= EHCI_int_RootHub_FeatToMask(Feat); +} + +void EHCI_RootHub_ClearPortFeature(void *Ptr, int Port, int Feat) +{ + tEHCI_Controller *Cont = Ptr; + if(Port >= Cont->nPorts) return; + + Cont->OpRegs->PortSC[Port] &= ~EHCI_int_RootHub_FeatToMask(Feat); +} + +int EHCI_RootHub_GetPortStatus(void *Ptr, int Port, int Flag) +{ + tEHCI_Controller *Cont = Ptr; + if(Port >= Cont->nPorts) return 0; + + return !!(Cont->OpRegs->PortSC[Port] & EHCI_int_RootHub_FeatToMask(Flag)); +} + // -------------------------------------------------------------------- // Internals // -------------------------------------------------------------------- tEHCI_qTD *EHCI_int_AllocateTD(tEHCI_Controller *Cont, int PID, void *Data, size_t Length, tUSBHostCb Cb, void *CbData) { + UNIMPLEMENTED(); return NULL; } void EHCI_int_DeallocateTD(tEHCI_Controller *Cont, tEHCI_qTD *TD) { + UNIMPLEMENTED(); } void EHCI_int_AppendTD(tEHCI_QH *QH, tEHCI_qTD *TD) { + UNIMPLEMENTED(); } tEHCI_QH *EHCI_int_AllocateQH(tEHCI_Controller *Cont, int Endpoint, size_t MaxPacketSize) { + UNIMPLEMENTED(); return NULL; } void EHCI_int_DeallocateQH(tEHCI_Controller *Cont, tEHCI_QH *QH) { + UNIMPLEMENTED(); +} + +void EHCI_int_HandlePortConnectChange(tEHCI_Controller *Cont, int Port) +{ + // Connect Event + if( Cont->OpRegs->PortSC[Port] & PORTSC_CurrentConnectStatus ) + { + // Is the device low-speed? + if( (Cont->OpRegs->PortSC[Port] & PORTSC_LineStatus_MASK) == PORTSC_LineStatus_Kstate ) + { + LOG("Low speed device on %p Port %i, giving to companion", Cont, Port); + Cont->OpRegs->PortSC[Port] |= PORTSC_PortOwner; + } + // not a low-speed device, EHCI reset + else + { + LOG("Device connected on %p #%i", Cont, Port); + // Reset procedure. + USB_PortCtl_BeginReset(Cont->RootHub, Port); + } + } + // Disconnect Event + else + { + if( Cont->OpRegs->PortSC[Port] & PORTSC_PortOwner ) { + LOG("Companion port %i disconnected, taking it back", Port); + Cont->OpRegs->PortSC[Port] &= ~PORTSC_PortOwner; + } + else { + LOG("Port %i disconnected", Port); + USB_DeviceDisconnected(Cont->RootHub, Port); + } + } } +void EHCI_int_InterruptThread(void *ControllerPtr) +{ + tEHCI_Controller *Cont = ControllerPtr; + while(Cont->OpRegs) + { + Uint32 events; + + LOG("sleeping for events"); + events = Threads_WaitEvents(EHCI_THREADEVENT_IOC|EHCI_THREADEVENT_PORTSC); + if( !events ) { + // TODO: Should this cause a termination? + } + LOG("events = 0x%x", events); + + if( events & EHCI_THREADEVENT_IOC ) + { + // IOC, Do whatever it is you do + } + + // Port status change interrupt + if( events & EHCI_THREADEVENT_PORTSC ) + { + // Check for port status changes + for(int i = 0; i < Cont->nPorts; i ++ ) + { + Uint32 sts = Cont->OpRegs->PortSC[i]; + LOG("Port %i: sts = %x", i, sts); + Cont->OpRegs->PortSC[i] = sts; + if( sts & PORTSC_ConnectStatusChange ) + EHCI_int_HandlePortConnectChange(Cont, i); + + if( sts & PORTSC_PortEnableChange ) + { + // Handle enable/disable + } + + if( sts & PORTSC_OvercurrentChange ) + { + // Handle over-current change + } + } + } + + LOG("Going back to sleep"); + } +} diff --git a/KernelLand/Modules/USB/EHCI/ehci.h b/KernelLand/Modules/USB/EHCI/ehci.h index 366f1606..6c6d8a81 100644 --- a/KernelLand/Modules/USB/EHCI/ehci.h +++ b/KernelLand/Modules/USB/EHCI/ehci.h @@ -8,6 +8,8 @@ #ifndef _EHCI_H_ #define _EHCI_H_ +#include + #define PERIODIC_SIZE 1024 typedef struct sEHCI_CapRegs tEHCI_CapRegs; @@ -159,7 +161,7 @@ struct sEHCI_OpRegs * 22 = Wake on Over-current Enable * 23:31 = Reserved (ZERO) */ - Uint32 PortSC[15]; + volatile Uint32 PortSC[15]; }; #define USBCMD_Run 0x0001 @@ -174,6 +176,28 @@ struct sEHCI_OpRegs #define USBINTR_HostSystemError 0x0010 #define USBINTR_AsyncAdvance 0x0020 +#define PORTSC_CurrentConnectStatus 0x0001 +#define PORTSC_ConnectStatusChange 0x0002 +#define PORTSC_PortEnabled 0x0004 +#define PORTSC_PortEnableChange 0x0008 +#define PORTSC_OvercurrentActive 0x0010 +#define PORTSC_OvercurrentChange 0x0020 +#define PORTSC_ForcePortResume 0x0040 +#define PORTSC_Suspend 0x0080 +#define PORTSC_PortReset 0x0100 +#define PORTSC_Reserved1 0x0200 +#define PORTSC_LineStatus_MASK 0x0C00 +#define PORTSC_LineStatus_SE0 0x0000 +#define PORTSC_LineStatus_Jstate 0x0400 +#define PORTSC_LineStatus_Kstate 0x0800 +#define PORTSC_LineStatus_Undef 0x0C00 +#define PORTSC_PortPower 0x1000 +#define PORTSC_PortOwner 0x2000 +#define PORTSC_PortIndicator_MASK 0xC000 +#define PORTSC_PortIndicator_Off 0x0000 +#define PORTSC_PortIndicator_Amber 0x4000 +#define PORTSC_PortIndicator_Green 0x800 + // Isochronous (High-Speed) Transfer Descriptor struct sEHCI_iTD { @@ -241,6 +265,10 @@ struct sEHCI_QH struct sEHCI_Controller { + tUSBHub *RootHub; + tThread *InterruptThread; + int nPorts; + tPAddr PhysBase; tEHCI_CapRegs *CapRegs; tEHCI_OpRegs *OpRegs; @@ -248,8 +276,9 @@ struct sEHCI_Controller int InterruptLoad[PERIODIC_SIZE]; tEHCI_QH *LastAsyncHead; - Uint32 *PeriodicQueue; - tEHCI_QH PeriodicQueueV[PERIODIC_SIZE]; + tMutex PeriodicListLock; + Uint32 *PeriodicQueue; + tEHCI_QH *PeriodicQueueV[PERIODIC_SIZE]; tEHCI_QH *QHPools[(256*16)*sizeof(tEHCI_QH)/PAGE_SIZE]; // [PAGE_SIZE/64] tEHCI_qTD *TDPool[PAGE_SIZE/sizeof(tEHCI_qTD)];