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 // Oops, PCI BIOS emulation time
84 Uint8 irq = PCI_GetIRQ(id);
89 Log_Log("ECHI", "Controller at PCI %i 0x%x IRQ 0x%x",
92 if( EHCI_InitController(addr, irq) )
94 // TODO: Detect other forms of failure than "out of slots"
98 // TODO: Register with the USB stack
103 int EHCI_Cleanup(void)
108 // --- Driver Init ---
109 int EHCI_InitController(tPAddr BaseAddress, Uint8 InterruptNum)
111 tEHCI_Controller *cont = NULL;
113 for( int i = 0; i < EHCI_MAX_CONTROLLERS; i ++ )
115 if( gaEHCI_Controllers[i].PhysBase == 0 ) {
116 cont = &gaEHCI_Controllers[i];
117 cont->PhysBase = BaseAddress;
122 Log_Notice("EHCI", "Too many controllers (EHCI_MAX_CONTROLLERS=%i)",
123 EHCI_MAX_CONTROLLERS);
127 // - Nuke a couple of fields so error handling code doesn't derp
128 cont->CapRegs = NULL;
129 cont->PeriodicQueue = NULL;
132 // -- Build up structure --
133 cont->CapRegs = (void*)MM_MapHWPages(BaseAddress, 1);
134 if( !cont->CapRegs ) {
135 Log_Warning("EHCI", "Can't map 1 page at %P into kernel space", BaseAddress);
139 if( (cont->CapRegs->CapLength & 3) ) {
140 Log_Warning("EHCI", "Controller at %P non-aligned op regs", BaseAddress);
143 cont->OpRegs = (void*)( (Uint32*)cont->CapRegs + cont->CapRegs->CapLength / 4 );
144 // - Allocate periodic queue
146 cont->PeriodicQueue = (void*)MM_AllocDMA(1, 32, &unused);
147 if( !cont->PeriodicQueue ) {
148 Log_Warning("ECHI", "Can't allocate 1 32-bit page for periodic queue");
151 for( int i = 0; i < 1024; i ++ )
152 cont->PeriodicQueue[i] = 1;
156 // - Allocate TD pool
157 cont->TDPool = (void*)MM_AllocDMA(1, 32, &unused);
158 if( !cont->TDPool ) {
159 Log_Warning("ECHI", "Can't allocate 1 32-bit page for qTD pool");
162 for( int i = 0; i < TD_POOL_SIZE; i ++ ) {
163 cont->TDPool[i].Token = 3 << 8;
167 cont->nPorts = cont->CapRegs->HCSParams & 0xF;
170 IRQ_AddHandler(InterruptNum, EHCI_InterruptHandler, cont);
171 cont->InterruptThread = Proc_SpawnWorker(EHCI_int_InterruptThread, cont);
172 if( !cont->InterruptThread ) {
173 Log_Warning("EHCI", "Can't spawn interrupt worker thread");
176 LOG("cont->InterruptThread = %p", cont->InterruptThread);
178 // -- Initialisation procedure (from ehci-r10) --
179 // - Reset controller
180 cont->OpRegs->USBCmd = USBCMD_HCReset;
181 // - Set CTRLDSSEGMENT (TODO: 64-bit support)
183 cont->OpRegs->USBIntr = USBINTR_IOC|USBINTR_PortChange|USBINTR_FrameRollover;
184 // - Set PERIODICLIST BASE
185 cont->OpRegs->PeridocListBase = MM_GetPhysAddr( cont->PeriodicQueue );
186 // - Enable controller
187 cont->OpRegs->USBCmd = (0x40 << 16) | USBCMD_PeriodicEnable | USBCMD_Run;
189 cont->OpRegs->ConfigFlag = 1;
191 cont->DeadTD = EHCI_int_AllocateTD(cont, 0, NULL, 0, NULL, NULL);
192 cont->DeadTD->Link = 1;
193 cont->DeadTD->Link2 = 1;
194 cont->DeadTD->Token = 0;
196 // -- Register with USB Core --
197 cont->RootHub = USB_RegisterHost(&gEHCI_HostDef, cont, cont->nPorts);
203 MM_Deallocate( (tVAddr)cont->CapRegs );
204 if( cont->PeriodicQueue )
205 MM_Deallocate( (tVAddr)cont->PeriodicQueue );
207 MM_Deallocate( (tVAddr)cont->TDPool );
211 void EHCI_InterruptHandler(int IRQ, void *Ptr)
213 tEHCI_Controller *Cont = Ptr;
214 Uint32 sts = Cont->OpRegs->USBSts;
217 Cont->OpRegs->USBSts = sts;
219 if( sts & 0xFFFF0FC0 ) {
220 LOG("Oops, reserved bits set (%08x), funny hardware?", sts);
224 // Unmask read-only bits
227 if( sts & USBINTR_IOC ) {
229 Threads_PostEvent(Cont->InterruptThread, EHCI_THREADEVENT_IOC);
233 if( sts & USBINTR_PortChange ) {
234 // Port change, determine what port and poke helper thread
235 LOG("Port status change");
236 Threads_PostEvent(Cont->InterruptThread, EHCI_THREADEVENT_PORTSC);
237 sts &= ~USBINTR_PortChange;
240 if( sts & USBINTR_FrameRollover ) {
241 // Frame rollover, used to aid timing (trigger per-second operations)
242 LOG("Frame rollover");
243 sts &= ~USBINTR_FrameRollover;
247 // Unhandled interupt bits
249 LOG("WARN - Bitmask %x unhandled", sts);
255 // --------------------------------------------------------------------
257 // --------------------------------------------------------------------
258 void *EHCI_InitInterrupt(void *Ptr, int Endpoint, int bOutbound, int Period,
259 tUSBHostCb Cb, void *CbData, void *Buf, size_t Length)
261 tEHCI_Controller *Cont = Ptr;
262 int pow2period, period_pow;
264 if( Endpoint >= 256*16 )
271 LOG("Endpoint=%x, bOutbound=%i, Period=%i, Length=%i", Endpoint, bOutbound, Period, Length);
273 // Round the period to the closest power of two
276 // - Find the first power above the period
277 while( pow2period < Period )
282 // - Check which is closest
283 if( Period - pow2period / 2 > pow2period - Period )
286 Period = pow2period/2;
291 tEHCI_QH *qh = EHCI_int_AllocateQH(Cont, Endpoint, Length);
292 qh->Impl.IntPeriodPow = period_pow;
294 Mutex_Acquire(&Cont->PeriodicListLock);
296 // Choose an interrupt slot to use
297 int minslot = 0, minslot_load = INT_MAX;
298 for( int slot = 0; slot < Period; slot ++ )
301 for( int i = 0; i < PERIODIC_SIZE; i += Period )
302 load += Cont->InterruptLoad[i+slot];
303 if( load == 0 ) break;
304 if( load < minslot_load ) {
309 // Increase loading on the selected slot
310 for( int i = minslot; i < PERIODIC_SIZE; i += Period )
311 Cont->InterruptLoad[i] += Length;
312 qh->Impl.IntOfs = minslot;
314 // Allocate TD for the data
315 tEHCI_qTD *td = EHCI_int_AllocateTD(Cont, (bOutbound ? PID_OUT : PID_IN), Buf, Length, Cb, CbData);
316 EHCI_int_AppendTD(Cont, qh, td);
318 // Insert into the periodic list
319 for( int i = 0; i < PERIODIC_SIZE; i += Period )
322 // - the end is reached
323 // - this QH is found
324 // - A QH with a lower period is encountered
325 tEHCI_QH *pqh = NULL;
327 for( nqh = Cont->PeriodicQueueV[i]; nqh; pqh = nqh, nqh = nqh->Impl.Next )
331 if( nqh->Impl.IntPeriodPow < period_pow )
335 // Somehow, we've already been added to this queue.
336 if( nqh && nqh == qh )
339 if( qh->Impl.Next && qh->Impl.Next != nqh ) {
340 Log_Warning("EHCI", "Suspected bookkeeping error on %p - int list %i+%i overlap",
341 Cont, period_pow, minslot);
347 qh->HLink = MM_GetPhysAddr(nqh) | 2;
350 qh->Impl.Next = NULL;
351 qh->HLink = 2|1; // QH, Terminate
356 pqh->HLink = MM_GetPhysAddr(qh) | 2;
359 Cont->PeriodicQueueV[i] = qh;
360 Cont->PeriodicQueue[i] = MM_GetPhysAddr(qh) | 2;
363 Mutex_Release(&Cont->PeriodicListLock);
368 void *EHCI_InitIsoch(void *Ptr, int Endpoint, size_t MaxPacketSize)
370 return (void*)(tVAddr)(Endpoint + 1);
372 void *EHCI_InitControl(void *Ptr, int Endpoint, size_t MaxPacketSize)
374 tEHCI_Controller *Cont = Ptr;
377 tEHCI_QH *qh = EHCI_int_AllocateQH(Cont, Endpoint, MaxPacketSize);
378 qh->CurrentTD = MM_GetPhysAddr(Cont->DeadTD);
380 // Append to async list
381 if( Cont->LastAsyncHead ) {
382 Cont->LastAsyncHead->HLink = MM_GetPhysAddr(qh)|2;
383 Cont->LastAsyncHead->Impl.Next = qh;
384 LOG("- Placed after %p", Cont->LastAsyncHead);
387 Cont->OpRegs->AsyncListAddr = MM_GetPhysAddr(qh)|2;
389 qh->HLink = Cont->OpRegs->AsyncListAddr;
390 Cont->OpRegs->USBCmd |= USBCMD_AsyncEnable;
391 Cont->LastAsyncHead = qh;
393 LOG("Created %p for %p Ep 0x%x - %i bytes MPS", qh, Ptr, Endpoint, MaxPacketSize);
397 void *EHCI_InitBulk(void *Ptr, int Endpoint, size_t MaxPacketSize)
399 return EHCI_InitControl(Ptr, Endpoint, MaxPacketSize);
401 void EHCI_RemEndpoint(void *Ptr, void *Handle)
405 else if( (tVAddr)Handle <= 256*16 )
410 // Remove QH from list
411 // - If it's a polling endpoint, need to remove from all periodic lists
412 if( qh->Impl.IntPeriodPow != 0xFF) {
420 EHCI_int_DeallocateQH(Ptr, Handle);
424 void *EHCI_SendControl(void *Ptr, void *Dest, tUSBHostCb Cb, void *CbData,
426 const void *SetupData, size_t SetupLength,
427 const void *OutData, size_t OutLength,
428 void *InData, size_t InLength
431 tEHCI_Controller *Cont = Ptr;
432 tEHCI_qTD *td_setup, *td_data, *td_status;
435 if( (tVAddr)Dest <= 256*16 )
438 LOG("Dest=%p, isOutbound=%i, Lengths(Setup:%i,Out:%i,In:%i)", Dest, isOutbound, SetupLength, OutLength, InLength);
440 // Check size of SETUP and status
443 td_setup = EHCI_int_AllocateTD(Cont, PID_SETUP, (void*)SetupData, SetupLength, NULL, NULL);
446 td_data = OutData ? EHCI_int_AllocateTD(Cont, PID_OUT, (void*)OutData, OutLength, NULL, NULL) : NULL;
447 td_status = EHCI_int_AllocateTD(Cont, PID_IN, InData, InLength, Cb, CbData);
451 td_data = InData ? EHCI_int_AllocateTD(Cont, PID_IN, InData, InLength, NULL, NULL) : NULL;
452 td_status = EHCI_int_AllocateTD(Cont, PID_OUT, (void*)OutData, OutLength, Cb, CbData);
453 td_status->Token |= (1 << 15); // IOC
458 td_setup->Link = MM_GetPhysAddr(td_data);
459 td_data->Link = MM_GetPhysAddr(td_status) | 1;
460 td_data->Token |= (1 << 8); // Active
463 td_setup->Link = MM_GetPhysAddr(td_status) | 1;
465 td_setup->Token |= (1 << 8); // Active
466 td_status->Token |= (1 << 8);
467 EHCI_int_AppendTD(Cont, Dest, td_setup);
472 void *EHCI_SendBulk(void *Ptr, void *Dest, tUSBHostCb Cb, void *CbData, int Dir, void *Data, size_t Length)
474 tEHCI_Controller *Cont = Ptr;
476 // Sanity check the pointer
477 // - Can't be NULL or an isoch
478 if( (tVAddr)Dest <= 256*16 )
481 // Allocate single TD
482 tEHCI_qTD *td = EHCI_int_AllocateTD(Cont, (Dir ? PID_OUT : PID_IN), Data, Length, Cb, CbData);
483 EHCI_int_AppendTD(Cont, Dest, td);
488 void EHCI_FreeOp(void *Ptr, void *Handle)
490 tEHCI_Controller *Cont = Ptr;
492 EHCI_int_DeallocateTD(Cont, Handle);
495 Uint32 EHCI_int_RootHub_FeatToMask(int Feat)
499 case PORT_RESET: return PORTSC_PortReset;
500 case PORT_ENABLE: return PORTSC_PortEnabled;
502 Log_Warning("EHCI", "Unknown root hub port feature %i", Feat);
507 void EHCI_RootHub_SetPortFeature(void *Ptr, int Port, int Feat)
509 tEHCI_Controller *Cont = Ptr;
510 if(Port >= Cont->nPorts) return;
512 Cont->OpRegs->PortSC[Port] |= EHCI_int_RootHub_FeatToMask(Feat);
515 void EHCI_RootHub_ClearPortFeature(void *Ptr, int Port, int Feat)
517 tEHCI_Controller *Cont = Ptr;
518 if(Port >= Cont->nPorts) return;
520 Cont->OpRegs->PortSC[Port] &= ~EHCI_int_RootHub_FeatToMask(Feat);
523 int EHCI_RootHub_GetPortStatus(void *Ptr, int Port, int Flag)
525 tEHCI_Controller *Cont = Ptr;
526 if(Port >= Cont->nPorts) return 0;
528 return !!(Cont->OpRegs->PortSC[Port] & EHCI_int_RootHub_FeatToMask(Flag));
531 // --------------------------------------------------------------------
533 // --------------------------------------------------------------------
534 tEHCI_qTD *EHCI_int_GetTDFromPhys(tEHCI_Controller *Cont, Uint32 Addr)
536 if( Addr == 0 ) return NULL;
537 LOG("%p + (%x - %x)", Cont->TDPool, Addr, MM_GetPhysAddr(Cont->TDPool));
538 return Cont->TDPool + (Addr - MM_GetPhysAddr(Cont->TDPool))/sizeof(tEHCI_qTD);
541 tEHCI_qTD *EHCI_int_AllocateTD(tEHCI_Controller *Cont, int PID, void *Data, size_t Length, tUSBHostCb Cb, void *CbData)
543 // Semaphore_Wait(&Cont->TDSemaphore, 1);
544 Mutex_Acquire(&Cont->TDPoolMutex);
545 for( int i = 0; i < TD_POOL_SIZE; i ++ )
547 if( ((Cont->TDPool[i].Token >> 8) & 3) != 3 )
549 Cont->TDPool[i].Token = (PID << 8) | (Length << 16);
550 // NOTE: Assumes that `Length` is <= PAGE_SIZE
551 Cont->TDPool[i].Pages[0] = MM_GetPhysAddr(Data);
552 if( (Cont->TDPool[i].Pages[0] & (PAGE_SIZE-1)) + Length - 1 > PAGE_SIZE )
553 Cont->TDPool[i].Pages[1] = MM_GetPhysAddr((char*)Data + Length - 1) & ~(PAGE_SIZE-1);
554 Mutex_Release(&Cont->TDPoolMutex);
555 LOG("Allocated %p for PID %i on %p", &Cont->TDPool[i], PID, Cont);
556 return &Cont->TDPool[i];
559 Mutex_Release(&Cont->TDPoolMutex);
563 void EHCI_int_DeallocateTD(tEHCI_Controller *Cont, tEHCI_qTD *TD)
568 void EHCI_int_AppendTD(tEHCI_Controller *Cont, tEHCI_QH *QH, tEHCI_qTD *TD)
570 tEHCI_qTD *ptd = NULL;
571 Uint32 link = QH->CurrentTD;
573 // TODO: Need locking and validation here
574 while( link && !(link & 1) )
576 ptd = EHCI_int_GetTDFromPhys(Cont, link);
579 // TODO: Figure out how to follow this properly
581 QH->CurrentTD = MM_GetPhysAddr(TD);
582 LOG("Appended %p to beginning of %p", TD, QH);
585 ptd->Link = MM_GetPhysAddr(TD);
586 LOG("Appended %p to end of %p", TD, QH);
590 tEHCI_QH *EHCI_int_AllocateQH(tEHCI_Controller *Cont, int Endpoint, size_t MaxPacketSize)
593 Mutex_Acquire(&Cont->QHPoolMutex);
594 for( int i = 0; i < QH_POOL_SIZE; i ++ )
596 if( !MM_GetPhysAddr( Cont->QHPools[i/QH_POOL_NPERPAGE] ) ) {
598 Cont->QHPools[i/QH_POOL_NPERPAGE] = (void*)MM_AllocDMA(1, 32, &tmp);
599 memset(Cont->QHPools[i/QH_POOL_NPERPAGE], 0, PAGE_SIZE);
602 ret = &Cont->QHPools[i/QH_POOL_NPERPAGE][i%QH_POOL_NPERPAGE];
603 if( ret->HLink == 0 ) {
605 ret->Overlay.Link = 1;
606 ret->Endpoint = (Endpoint >> 4) | 0x80 | ((Endpoint & 0xF) << 8)
607 | (MaxPacketSize << 16);
608 // TODO: Endpoint speed (13:12) 0:Full, 1:Low, 2:High
609 // TODO: Control Endpoint Flag (27) 0:*, 1:Full/Low Control
610 Mutex_Release(&Cont->QHPoolMutex);
614 Mutex_Release(&Cont->QHPoolMutex);
618 void EHCI_int_DeallocateQH(tEHCI_Controller *Cont, tEHCI_QH *QH)
620 // TODO: Ensure it's unused (somehow)
624 void EHCI_int_HandlePortConnectChange(tEHCI_Controller *Cont, int Port)
627 if( Cont->OpRegs->PortSC[Port] & PORTSC_CurrentConnectStatus )
629 // Is the device low-speed?
630 if( (Cont->OpRegs->PortSC[Port] & PORTSC_LineStatus_MASK) == PORTSC_LineStatus_Kstate )
632 LOG("Low speed device on %p Port %i, giving to companion", Cont, Port);
633 Cont->OpRegs->PortSC[Port] |= PORTSC_PortOwner;
635 // not a low-speed device, EHCI reset
638 LOG("Device connected on %p #%i", Cont, Port);
640 USB_PortCtl_BeginReset(Cont->RootHub, Port);
646 if( Cont->OpRegs->PortSC[Port] & PORTSC_PortOwner ) {
647 LOG("Companion port %i disconnected, taking it back", Port);
648 Cont->OpRegs->PortSC[Port] &= ~PORTSC_PortOwner;
651 LOG("Port %i disconnected", Port);
652 USB_DeviceDisconnected(Cont->RootHub, Port);
657 void EHCI_int_InterruptThread(void *ControllerPtr)
659 tEHCI_Controller *Cont = ControllerPtr;
664 LOG("sleeping for events");
665 events = Threads_WaitEvents(EHCI_THREADEVENT_IOC|EHCI_THREADEVENT_PORTSC);
667 // TODO: Should this cause a termination?
669 LOG("events = 0x%x", events);
671 if( events & EHCI_THREADEVENT_IOC )
673 // IOC, Do whatever it is you do
676 // Port status change interrupt
677 if( events & EHCI_THREADEVENT_PORTSC )
679 // Check for port status changes
680 for(int i = 0; i < Cont->nPorts; i ++ )
682 Uint32 sts = Cont->OpRegs->PortSC[i];
683 LOG("Port %i: sts = %x", i, sts);
684 Cont->OpRegs->PortSC[i] = sts;
685 if( sts & PORTSC_ConnectStatusChange )
686 EHCI_int_HandlePortConnectChange(Cont, i);
688 if( sts & PORTSC_PortEnableChange )
690 // Handle enable/disable
693 if( sts & PORTSC_OvercurrentChange )
695 // Handle over-current change
700 LOG("Going back to sleep");