3 * - By John Hodge (thePowersGang)
9 #define VERSION VER2(0,1)
20 #define EHCI_MAX_CONTROLLERS 4
21 #define EHCI_THREADEVENT_IOC THREAD_EVENT_USER1
22 #define EHCI_THREADEVENT_PORTSC THREAD_EVENT_USER2
25 int EHCI_Initialise(char **Arguments);
26 int EHCI_Cleanup(void);
27 int EHCI_InitController(tPAddr BaseAddress, Uint8 InterruptNum);
28 void EHCI_InterruptHandler(int IRQ, void *Ptr);
30 void *EHCI_InitInterrupt(void *Ptr, int Endpoint, int bInput, int Period, tUSBHostCb Cb, void *CbData, void *Buf, size_t Length);
31 void *EHCI_InitIsoch (void *Ptr, int Endpoint, size_t MaxPacketSize);
32 void *EHCI_InitControl(void *Ptr, int Endpoint, size_t MaxPacketSize);
33 void *EHCI_InitBulk (void *Ptr, int Endpoint, size_t MaxPacketSize);
34 void EHCI_RemEndpoint(void *Ptr, void *Handle);
35 void *EHCI_SendControl(void *Ptr, void *Dest, tUSBHostCb Cb, void *CbData,
37 const void *SetupData, size_t SetupLength,
38 const void *OutData, size_t OutLength,
39 void *InData, size_t InLength
41 void *EHCI_SendBulk(void *Ptr, void *Dest, tUSBHostCb Cb, void *CbData, int Dir, void *Data, size_t Length);
42 void EHCI_FreeOp(void *Ptr, void *Handle);
43 Uint32 EHCI_int_RootHub_FeatToMask(int Feat);
44 void EHCI_RootHub_SetPortFeature(void *Ptr, int Port, int Feat);
45 void EHCI_RootHub_ClearPortFeature(void *Ptr, int Port, int Feat);
46 int EHCI_RootHub_GetPortStatus(void *Ptr, int Port, int Flag);
48 tEHCI_qTD *EHCI_int_AllocateTD(tEHCI_Controller *Cont, int PID, void *Data, size_t Length, tUSBHostCb Cb, void *CbData);
49 void EHCI_int_DeallocateTD(tEHCI_Controller *Cont, tEHCI_qTD *TD);
50 void EHCI_int_AppendTD(tEHCI_Controller *Cont, tEHCI_QH *QH, tEHCI_qTD *TD);
51 tEHCI_QH *EHCI_int_AllocateQH(tEHCI_Controller *Cont, int Endpoint, size_t MaxPacketSize);
52 void EHCI_int_DeallocateQH(tEHCI_Controller *Cont, tEHCI_QH *QH);
53 void EHCI_int_InterruptThread(void *ControllerPtr);
56 MODULE_DEFINE(0, VERSION, USB_EHCI, EHCI_Initialise, NULL, "USB_Core", NULL);
57 tEHCI_Controller gaEHCI_Controllers[EHCI_MAX_CONTROLLERS];
58 tUSBHostDef gEHCI_HostDef = {
59 .InitInterrupt = EHCI_InitInterrupt,
60 .InitIsoch = EHCI_InitIsoch,
61 .InitControl = EHCI_InitControl,
62 .InitBulk = EHCI_InitBulk,
63 .RemEndpoint = EHCI_RemEndpoint,
65 .SendControl = EHCI_SendControl,
66 .SendBulk = EHCI_SendBulk,
67 .FreeOp = EHCI_FreeOp,
69 .CheckPorts = NULL, // No need
70 .SetPortFeature = EHCI_RootHub_SetPortFeature,
71 .ClearPortFeature = EHCI_RootHub_ClearPortFeature,
72 .GetPortStatus = EHCI_RootHub_GetPortStatus,
76 int EHCI_Initialise(char **Arguments)
78 for( int id = -1; (id = PCI_GetDeviceByClass(0x0C0320, 0xFFFFFF, id)) >= 0; )
80 Uint32 addr = PCI_GetBAR(id, 0);
82 // TODO: PCI BIOS emulation time
89 Uint8 irq = PCI_GetIRQ(id);
94 Log_Log("ECHI", "Controller at PCI %i 0x%x IRQ 0x%x",
97 if( EHCI_InitController(addr, irq) )
99 // TODO: Detect other forms of failure than "out of slots"
104 for( int i = 0; Arguments && Arguments[i]; i ++ )
106 char *pos = Arguments[i], *next;
107 LOG("pos = '%s'", pos);
108 tPAddr base = strtoull(pos, &next, 16);
112 LOG("pos = '%s'", pos);
115 LOG("pos = '%s'", pos);
116 int irq = strtol(pos, &next, 16);
121 LOG("base=%x, irq=%i", base, irq);
122 if( EHCI_InitController(base, irq) )
131 int EHCI_Cleanup(void)
136 // --- Driver Init ---
137 int EHCI_InitController(tPAddr BaseAddress, Uint8 InterruptNum)
139 tEHCI_Controller *cont = NULL;
141 ENTER("PBaseAddress iInterruptNum",
142 BaseAddress, InterruptNum);
144 for( int i = 0; i < EHCI_MAX_CONTROLLERS; i ++ )
146 if( gaEHCI_Controllers[i].PhysBase == 0 ) {
147 cont = &gaEHCI_Controllers[i];
148 cont->PhysBase = BaseAddress;
153 Log_Notice("EHCI", "Too many controllers (EHCI_MAX_CONTROLLERS=%i)",
154 EHCI_MAX_CONTROLLERS);
159 // - Nuke a couple of fields so error handling code doesn't derp
160 cont->CapRegs = NULL;
161 cont->PeriodicQueue = NULL;
164 // -- Build up structure --
165 cont->CapRegs = (void*)( MM_MapHWPages(BaseAddress, 1) + (BaseAddress % PAGE_SIZE) );
166 if( !cont->CapRegs ) {
167 Log_Warning("EHCI", "Can't map 1 page at %P into kernel space", BaseAddress);
170 LOG("cont->CapRegs = %p", cont->CapRegs);
172 if( (cont->CapRegs->CapLength & 3) ) {
173 Log_Warning("EHCI", "Controller at %P non-aligned op regs (%x)",
174 BaseAddress, cont->CapRegs->CapLength);
177 if( BaseAddress % PAGE_SIZE + cont->CapRegs->CapLength + sizeof(tEHCI_CapRegs) > PAGE_SIZE ) {
178 Log_Warning("EHCI", "%P: Cap regs over page boundary (+0x%x bytes)",
179 BaseAddress % PAGE_SIZE + cont->CapRegs->CapLength + sizeof(tEHCI_CapRegs)
183 cont->OpRegs = (void*)( (Uint32*)cont->CapRegs + cont->CapRegs->CapLength / 4 );
184 LOG("cont->OpRegs = %p", cont->OpRegs);
185 // - Allocate periodic queue
187 cont->PeriodicQueue = (void*)MM_AllocDMA(1, 32, &unused);
188 if( !cont->PeriodicQueue ) {
189 Log_Warning("ECHI", "Can't allocate 1 32-bit page for periodic queue");
192 for( int i = 0; i < 1024; i ++ )
193 cont->PeriodicQueue[i] = 1;
197 // - Allocate TD pool
198 cont->TDPool = (void*)MM_AllocDMA(1, 32, &unused);
199 if( !cont->TDPool ) {
200 Log_Warning("ECHI", "Can't allocate 1 32-bit page for qTD pool");
203 for( int i = 0; i < TD_POOL_SIZE; i ++ ) {
204 cont->TDPool[i].Token = 3 << 8;
208 cont->nPorts = cont->CapRegs->HCSParams & 0xF;
209 LOG("cont->nPorts = %i", cont->nPorts);
212 IRQ_AddHandler(InterruptNum, EHCI_InterruptHandler, cont);
213 cont->InterruptThread = Proc_SpawnWorker(EHCI_int_InterruptThread, cont);
214 if( !cont->InterruptThread ) {
215 Log_Warning("EHCI", "Can't spawn interrupt worker thread");
218 LOG("cont->InterruptThread = %p", cont->InterruptThread);
221 cont->DeadTD = EHCI_int_AllocateTD(cont, 0, NULL, 0, NULL, NULL);
222 memset(cont->DeadTD, 0, sizeof(tEHCI_qTD));
223 cont->DeadTD->Link = 1;
224 cont->DeadTD->Link2 = 1;
225 cont->DeadTD->Token = QTD_TOKEN_STS_HALT;
228 cont->DeadQH = EHCI_int_AllocateQH(cont, 0, 0);
229 memset(cont->DeadQH, 0, sizeof(tEHCI_QH));
230 cont->DeadQH->HLink = MM_GetPhysAddr(cont->DeadQH)|2;
231 cont->DeadQH->Endpoint = (1<<15); // H - Head of Reclamation List
232 cont->DeadQH->CurrentTD = MM_GetPhysAddr(cont->DeadTD);
233 cont->DeadQH->Overlay.Link = MM_GetPhysAddr(cont->DeadTD);
235 // -- Initialisation procedure (from ehci-r10) --
236 // - Reset controller
237 cont->OpRegs->USBCmd = USBCMD_HCReset;
238 // - Set CTRLDSSEGMENT (TODO: 64-bit support)
240 cont->OpRegs->USBIntr = USBINTR_IOC|USBINTR_PortChange|USBINTR_FrameRollover;
241 // - Set PERIODICLIST BASE
242 cont->OpRegs->PeridocListBase = MM_GetPhysAddr( cont->PeriodicQueue );
243 // - Set ASYNCLISTADDR
244 cont->OpRegs->AsyncListAddr = MM_GetPhysAddr(cont->DeadQH);
245 // - Enable controller
246 cont->OpRegs->USBCmd = (0x40 << 16) | USBCMD_PeriodicEnable | USBCMD_AsyncEnable | USBCMD_Run;
248 cont->OpRegs->ConfigFlag = 1;
250 // -- Register with USB Core --
251 cont->RootHub = USB_RegisterHost(&gEHCI_HostDef, cont, cont->nPorts);
252 LOG("cont->RootHub = %p", cont->RootHub);
259 MM_Deallocate( (tVAddr)cont->CapRegs );
260 if( cont->PeriodicQueue )
261 MM_Deallocate( (tVAddr)cont->PeriodicQueue );
263 MM_Deallocate( (tVAddr)cont->TDPool );
268 void EHCI_InterruptHandler(int IRQ, void *Ptr)
270 tEHCI_Controller *Cont = Ptr;
271 Uint32 sts = Cont->OpRegs->USBSts;
272 Uint32 orig_sts = sts;
274 if( sts & 0xFFFF0FC0 ) {
275 LOG("Oops, reserved bits set (%08x), funny hardware?", sts);
279 // Unmask read-only bits
282 if( sts & USBINTR_IOC ) {
284 LOG("%P IOC", Cont->PhysBase);
285 Threads_PostEvent(Cont->InterruptThread, EHCI_THREADEVENT_IOC);
289 if( sts & USBINTR_AsyncAdvance ) {
290 LOG("%P IAAD", Cont->PhysBase);
292 if( Cont->AsyncQHAddHead )
296 sts &= ~USBINTR_AsyncAdvance;
299 if( sts & USBINTR_PortChange ) {
300 // Port change, determine what port and poke helper thread
301 LOG("%P Port status change", Cont->PhysBase);
302 Threads_PostEvent(Cont->InterruptThread, EHCI_THREADEVENT_PORTSC);
303 sts &= ~USBINTR_PortChange;
306 if( sts & USBINTR_FrameRollover ) {
307 // Frame rollover, used to aid timing (trigger per-second operations)
308 //LOG("%p Frame rollover", Ptr);
309 sts &= ~USBINTR_FrameRollover;
313 // Unhandled interupt bits
315 LOG("WARN - Bitmask %x unhandled", sts);
320 Cont->OpRegs->USBSts = orig_sts;
323 // --------------------------------------------------------------------
325 // --------------------------------------------------------------------
326 void *EHCI_InitInterrupt(void *Ptr, int Endpoint, int bOutbound, int Period,
327 tUSBHostCb Cb, void *CbData, void *Buf, size_t Length)
329 tEHCI_Controller *Cont = Ptr;
330 int pow2period, period_pow;
332 ASSERTCR(Endpoint, <, 256*16, NULL);
333 ASSERTCR(Period, >, 0, NULL);
337 LOG("Endpoint=%x, bOutbound=%i, Period=%i, Length=%i", Endpoint, bOutbound, Period, Length);
339 // Round the period to the closest power of two
342 // - Find the first power above the period
343 while( pow2period < Period )
348 // - Check which is closest
349 if( Period - pow2period / 2 > pow2period - Period )
352 Period = pow2period/2;
355 LOG("period_pow = %i, Period = %ims", period_pow, Period);
358 tEHCI_QH *qh = EHCI_int_AllocateQH(Cont, Endpoint, Length);
359 qh->Impl.IntPeriodPow = period_pow;
360 qh->EndpointExt |= 1; // TODO: uFrame load balancing (8 entry bitfield)
362 Mutex_Acquire(&Cont->PeriodicListLock);
364 // Choose an interrupt slot to use
365 int minslot = 0, minslot_load = INT_MAX;
366 for( int slot = 0; slot < Period; slot ++ )
369 for( int i = 0; i < PERIODIC_SIZE; i += Period )
370 load += Cont->InterruptLoad[i+slot];
371 if( load == 0 ) break;
372 if( load < minslot_load ) {
377 // Increase loading on the selected slot
378 for( int i = minslot; i < PERIODIC_SIZE; i += Period )
379 Cont->InterruptLoad[i] += Length;
380 qh->Impl.IntOfs = minslot;
381 LOG("Using slot %i", minslot);
383 // Allocate TD for the data
384 tEHCI_qTD *td = EHCI_int_AllocateTD(Cont, (bOutbound ? PID_OUT : PID_IN), Buf, Length, Cb, CbData);
385 EHCI_int_AppendTD(Cont, qh, td);
387 // Insert into the periodic list
388 for( int i = 0; i < PERIODIC_SIZE; i += Period )
391 // - the end is reached
392 // - this QH is found
393 // - A QH with a lower period is encountered
394 tEHCI_QH *pqh = NULL;
396 for( nqh = Cont->PeriodicQueueV[i]; nqh; pqh = nqh, nqh = nqh->Impl.Next )
400 if( nqh->Impl.IntPeriodPow < period_pow )
404 // Somehow, we've already been added to this queue.
405 if( nqh && nqh == qh )
408 if( qh->Impl.Next && qh->Impl.Next != nqh ) {
409 Log_Warning("EHCI", "Suspected bookkeeping error on %p - int list %i+%i overlap",
410 Cont, period_pow, minslot);
416 qh->HLink = MM_GetPhysAddr(nqh) | 2;
419 qh->Impl.Next = NULL;
420 qh->HLink = 2|1; // QH, Terminate
425 pqh->HLink = MM_GetPhysAddr(qh) | 2;
428 Cont->PeriodicQueueV[i] = qh;
429 Cont->PeriodicQueue[i] = MM_GetPhysAddr(qh) | 2;
432 Mutex_Release(&Cont->PeriodicListLock);
437 void *EHCI_InitIsoch(void *Ptr, int Endpoint, size_t MaxPacketSize)
439 ENTER("pPtr iEndpoint iMaxPacketSize",
440 Ptr, Endpoint, MaxPacketSize);
441 LEAVE_RET('p', (void*)(tVAddr)(Endpoint + 1));
443 void *EHCI_InitControl(void *Ptr, int Endpoint, size_t MaxPacketSize)
445 tEHCI_Controller *Cont = Ptr;
447 ENTER("pPtr iEndpoint iMaxPacketSize",
448 Ptr, Endpoint, MaxPacketSize);
451 tEHCI_QH *qh = EHCI_int_AllocateQH(Cont, Endpoint, MaxPacketSize);
452 qh->CurrentTD = MM_GetPhysAddr(Cont->DeadTD);
454 // Append to async list
455 // TODO: Lock async list
456 qh->Impl.Next = Cont->DeadQH->Impl.Next;
457 Cont->DeadQH->Impl.Next = qh;
458 qh->HLink = Cont->DeadQH->HLink;
459 Cont->DeadQH->HLink = MM_GetPhysAddr(qh)|2;
461 LOG("Created %p(%P) for %P Ep 0x%x - %i bytes MPS",
462 qh, MM_GetPhysAddr(qh), Cont->PhysBase, Endpoint, MaxPacketSize);
467 void *EHCI_InitBulk(void *Ptr, int Endpoint, size_t MaxPacketSize)
469 return EHCI_InitControl(Ptr, Endpoint, MaxPacketSize);
471 void EHCI_RemEndpoint(void *Ptr, void *Handle)
475 else if( (tVAddr)Handle <= 256*16 )
480 // Remove QH from list
481 // - If it's a polling endpoint, need to remove from all periodic lists
482 if( qh->Impl.IntPeriodPow != 0xFF) {
490 EHCI_int_DeallocateQH(Ptr, Handle);
494 void *EHCI_SendControl(void *Ptr, void *Dest, tUSBHostCb Cb, void *CbData,
496 const void *SetupData, size_t SetupLength,
497 const void *OutData, size_t OutLength,
498 void *InData, size_t InLength
501 tEHCI_Controller *Cont = Ptr;
502 tEHCI_qTD *td_setup, *td_data, *td_status;
505 if( (tVAddr)Dest <= 256*16 )
508 LOG("Dest=%p, isOutbound=%i, Lengths(Setup:%i,Out:%i,In:%i)",
509 Dest, isOutbound, SetupLength, OutLength, InLength);
511 // TODO: Check size of SETUP and status
514 td_setup = EHCI_int_AllocateTD(Cont, PID_SETUP, (void*)SetupData, SetupLength, NULL, NULL);
517 td_data = OutData ? EHCI_int_AllocateTD(Cont, PID_OUT, (void*)OutData, OutLength, NULL, NULL) : NULL;
518 td_status = EHCI_int_AllocateTD(Cont, PID_IN, InData, InLength, Cb, CbData);
522 td_data = InData ? EHCI_int_AllocateTD(Cont, PID_IN, InData, InLength, NULL, NULL) : NULL;
523 td_status = EHCI_int_AllocateTD(Cont, PID_OUT, (void*)OutData, OutLength, Cb, CbData);
525 td_status->Token |= QTD_TOKEN_IOC; // IOC
529 td_setup->Link = MM_GetPhysAddr(td_data);
530 td_setup->Next = td_data;
531 td_data->Link = MM_GetPhysAddr(td_status);
532 td_data->Next = td_status;
533 td_data->Token |= QTD_TOKEN_STS_ACTIVE; // Active
536 td_setup->Link = MM_GetPhysAddr(td_status);
537 td_setup->Next = td_status;
539 td_setup->Token |= QTD_TOKEN_STS_ACTIVE; // Active
540 td_status->Token |= QTD_TOKEN_STS_ACTIVE;
542 td_status->Link2 = 1;
543 EHCI_int_AppendTD(Cont, Dest, td_setup);
545 LOG("return td_status=%p", td_status);
549 void *EHCI_SendBulk(void *Ptr, void *Dest, tUSBHostCb Cb, void *CbData, int Dir, void *Data, size_t Length)
551 tEHCI_Controller *Cont = Ptr;
553 // Sanity check the pointer
554 // - Can't be NULL or an isoch
555 if( (tVAddr)Dest <= 256*16 )
558 // Allocate single TD
559 tEHCI_qTD *td = EHCI_int_AllocateTD(Cont, (Dir ? PID_OUT : PID_IN), Data, Length, Cb, CbData);
560 EHCI_int_AppendTD(Cont, Dest, td);
565 void EHCI_FreeOp(void *Ptr, void *Handle)
567 tEHCI_Controller *Cont = Ptr;
569 EHCI_int_DeallocateTD(Cont, Handle);
572 Uint32 EHCI_int_RootHub_FeatToMask(int Feat)
576 case PORT_RESET: return PORTSC_PortReset;
577 case PORT_ENABLE: return PORTSC_PortEnabled;
579 Log_Warning("EHCI", "Unknown root hub port feature %i", Feat);
584 void EHCI_RootHub_SetPortFeature(void *Ptr, int Port, int Feat)
586 tEHCI_Controller *Cont = Ptr;
587 if(Port >= Cont->nPorts) return;
589 Cont->OpRegs->PortSC[Port] |= EHCI_int_RootHub_FeatToMask(Feat);
592 void EHCI_RootHub_ClearPortFeature(void *Ptr, int Port, int Feat)
594 tEHCI_Controller *Cont = Ptr;
595 if(Port >= Cont->nPorts) return;
597 Cont->OpRegs->PortSC[Port] &= ~EHCI_int_RootHub_FeatToMask(Feat);
600 int EHCI_RootHub_GetPortStatus(void *Ptr, int Port, int Flag)
602 tEHCI_Controller *Cont = Ptr;
603 if(Port >= Cont->nPorts) return 0;
605 return !!(Cont->OpRegs->PortSC[Port] & EHCI_int_RootHub_FeatToMask(Flag));
608 // --------------------------------------------------------------------
610 // --------------------------------------------------------------------
611 tEHCI_qTD *EHCI_int_GetTDFromPhys(tEHCI_Controller *Cont, Uint32 Addr)
613 if( Addr == 0 ) return NULL;
614 LOG("%p + (%x - %x)", Cont->TDPool, Addr, MM_GetPhysAddr(Cont->TDPool));
615 return Cont->TDPool + (Addr - MM_GetPhysAddr(Cont->TDPool))/sizeof(tEHCI_qTD);
618 tEHCI_qTD *EHCI_int_AllocateTD(tEHCI_Controller *Cont, int PID, void *Data, size_t Length, tUSBHostCb Cb, void *CbData)
620 // Semaphore_Wait(&Cont->TDSemaphore, 1);
621 Mutex_Acquire(&Cont->TDPoolMutex);
622 for( int i = 0; i < TD_POOL_SIZE; i ++ )
624 if( ((Cont->TDPool[i].Token >> 8) & 3) != 3 )
626 Cont->TDPool[i].Token = (PID << 8) | (Length << 16);
627 Mutex_Release(&Cont->TDPoolMutex);
629 tEHCI_qTD *td = &Cont->TDPool[i];
632 td->CallbackData = CbData;
633 // NOTE: Assumes that `Length` is <= PAGE_SIZE
634 ASSERTC(Length, <, PAGE_SIZE);
635 // TODO: Handle bouncing >32-bit pages
637 ASSERT( MM_GetPhysAddr(Data) >> 32 == 0 );
639 td->Pages[0] = MM_GetPhysAddr(Data);
640 if( (td->Pages[0] & (PAGE_SIZE-1)) + Length - 1 > PAGE_SIZE )
641 td->Pages[1] = MM_GetPhysAddr((char*)Data + Length - 1) & ~(PAGE_SIZE-1);
642 LOG("Allocated %p(%P) for PID %i on %P",
643 td, MM_GetPhysAddr(td), PID, Cont->PhysBase);
647 Mutex_Release(&Cont->TDPoolMutex);
651 void EHCI_int_DeallocateTD(tEHCI_Controller *Cont, tEHCI_qTD *TD)
656 void EHCI_int_AppendTD(tEHCI_Controller *Cont, tEHCI_QH *QH, tEHCI_qTD *TD)
658 tEHCI_qTD *ptd = NULL;
659 Uint32 link = QH->CurrentTD;
660 tPAddr dead_td = MM_GetPhysAddr(Cont->DeadTD);
663 Mutex_Acquire(&Cont->ActiveTDsLock);
664 if( Cont->ActiveTDTail )
665 Cont->ActiveTDTail->Next = TD;
667 Cont->ActiveTDHead = TD;
669 for( last = TD; last->Next; last = last->Next )
671 Cont->ActiveTDTail = last;
672 Mutex_Release(&Cont->ActiveTDsLock);
675 // TODO: Need locking and validation here
676 while( link && !(link & 1) && link != dead_td )
678 ptd = EHCI_int_GetTDFromPhys(Cont, link);
681 // TODO: Figure out how to follow this properly
683 QH->CurrentTD = MM_GetPhysAddr(TD)|2;
684 QH->Overlay.Link = QH->CurrentTD;
685 LOG("Appended %p to beginning of %p", TD, QH);
688 ptd->Link = MM_GetPhysAddr(TD);
689 ptd->Link2 = MM_GetPhysAddr(TD);
690 LOG("Appended %p to end of %p", TD, QH);
692 QH->Endpoint |= QH_ENDPT_H;
693 QH->Overlay.Token &= ~QTD_TOKEN_STS_HALT;
696 tEHCI_QH *EHCI_int_AllocateQH(tEHCI_Controller *Cont, int Endpoint, size_t MaxPacketSize)
699 Mutex_Acquire(&Cont->QHPoolMutex);
700 for( int i = 0; i < QH_POOL_SIZE; i ++ )
702 if( !MM_GetPhysAddr( Cont->QHPools[i/QH_POOL_NPERPAGE] ) ) {
704 Cont->QHPools[i/QH_POOL_NPERPAGE] = (void*)MM_AllocDMA(1, 32, &tmp);
705 memset(Cont->QHPools[i/QH_POOL_NPERPAGE], 0, PAGE_SIZE);
708 ret = &Cont->QHPools[i/QH_POOL_NPERPAGE][i%QH_POOL_NPERPAGE];
709 if( ret->HLink == 0 ) {
710 memset(ret, 0, sizeof(*ret));
712 ret->Overlay.Link = MM_GetPhysAddr(Cont->DeadTD);
713 ret->Endpoint = (Endpoint >> 4) | 0x80 | ((Endpoint & 0xF) << 8)
714 | (MaxPacketSize << 16);
715 ret->EndpointExt = (1<<30);
716 // TODO: Endpoint speed (13:12) 0:Full, 1:Low, 2:High
717 // TODO: Control Endpoint Flag (27) 0:*, 1:Full/Low Control
718 Mutex_Release(&Cont->QHPoolMutex);
722 Mutex_Release(&Cont->QHPoolMutex);
726 void EHCI_int_DeallocateQH(tEHCI_Controller *Cont, tEHCI_QH *QH)
728 // TODO: Ensure it's unused (somehow)
732 void EHCI_int_HandlePortConnectChange(tEHCI_Controller *Cont, int Port)
735 if( Cont->OpRegs->PortSC[Port] & PORTSC_CurrentConnectStatus )
737 // Is the device low-speed?
738 if( (Cont->OpRegs->PortSC[Port] & PORTSC_LineStatus_MASK) == PORTSC_LineStatus_Kstate )
740 LOG("Low speed device on %P Port %i, giving to companion", Cont->PhysBase, Port);
741 Cont->OpRegs->PortSC[Port] |= PORTSC_PortOwner;
743 // not a low-speed device, EHCI reset
746 LOG("Device connected on %P Port %i", Cont->PhysBase, Port);
748 USB_PortCtl_BeginReset(Cont->RootHub, Port);
754 if( Cont->OpRegs->PortSC[Port] & PORTSC_PortOwner ) {
755 LOG("Device disconnected on %P Port %i (companion), taking it back",
756 Cont->PhysBase, Port);
757 Cont->OpRegs->PortSC[Port] &= ~PORTSC_PortOwner;
760 LOG("Device disconnected on %P Port %i", Cont->PhysBase, Port);
761 USB_DeviceDisconnected(Cont->RootHub, Port);
766 void EHCI_int_InterruptThread(void *ControllerPtr)
768 tEHCI_Controller *Cont = ControllerPtr;
773 LOG("sleeping for events");
774 events = Threads_WaitEvents(EHCI_THREADEVENT_IOC|EHCI_THREADEVENT_PORTSC);
776 // TODO: Should this cause a termination?
778 LOG("events = 0x%x", events);
780 if( events & EHCI_THREADEVENT_IOC )
782 // IOC, Do whatever it is you do
783 Log_Warning("EHCI", "%P IOC - TODO: Call registered callbacks and reclaim",
786 Mutex_Acquire(&Cont->ActiveTDsLock);
787 tEHCI_qTD *prev = NULL;
788 for(tEHCI_qTD *td = Cont->ActiveTDHead; td; td = td->Next)
790 LOG("td(%p)->Token = %x", td, td->Token);
791 // If active, continue
792 if( td->Token & QTD_TOKEN_STS_ACTIVE ) {
798 LOG("%p Complete", td);
799 // - call the callback
804 Log_Warning("EHCI", "TODO: Map %x,%x+%i for callback",
805 td->Pages[0], td->Pages[1], td->Size);
807 td->Callback(td->CallbackData, ptr, td->Size);
810 // Remove and release
811 *(prev ? &prev->Next : &Cont->ActiveTDHead) = td->Next;
814 Cont->ActiveTDTail = prev;
815 Mutex_Release(&Cont->ActiveTDsLock);
818 // Port status change interrupt
819 if( events & EHCI_THREADEVENT_PORTSC )
821 // Check for port status changes
822 for(int i = 0; i < Cont->nPorts; i ++ )
824 Uint32 sts = Cont->OpRegs->PortSC[i];
825 LOG("Port %i: sts = %x", i, sts);
826 Cont->OpRegs->PortSC[i] = sts;
827 if( sts & PORTSC_ConnectStatusChange )
828 EHCI_int_HandlePortConnectChange(Cont, i);
830 if( sts & PORTSC_PortEnableChange )
832 // Handle enable/disable
835 if( sts & PORTSC_OvercurrentChange )
837 // Handle over-current change
842 LOG("Going back to sleep");