3 * - By John Hodge (thePowersGang)
5 * Universal Host Controller Interface
8 #define VERSION VER2(0,5)
18 #define MAX_CONTROLLERS 4
22 int UHCI_Initialise(char **Arguments);
24 tUHCI_TD *UHCI_int_AllocateTD(tUHCI_Controller *Cont);
25 tUHCI_TD *UHCI_int_GetTDFromPhys(tPAddr PAddr);
26 void UHCI_int_AppendTD(tUHCI_Controller *Cont, tUHCI_TD *TD);
27 void *UHCI_int_SendTransaction(tUHCI_Controller *Cont, int Addr, Uint8 Type, int bTgl, tUSBHostCb Cb, void *CbData, void *Buf, size_t Length);
28 void *UHCI_DataIN(void *Ptr, int Dest, int DataTgl, tUSBHostCb Cb, void *CbData, void *Buf, size_t Length);
29 void *UHCI_DataOUT(void *Ptr, int Dest, int DataTgl, tUSBHostCb Cb, void *CbData, void *Buf, size_t Length);
30 void *UHCI_SendSetup(void *Ptr, int Dest, int DataTgl, tUSBHostCb Cb, void *CbData, void *Buf, size_t Length);
31 int UHCI_IsTransferComplete(void *Ptr, void *Handle);
32 int UHCI_Int_InitHost(tUHCI_Controller *Host);
33 void UHCI_CheckPortUpdate(void *Ptr);
34 void UHCI_InterruptHandler(int IRQ, void *Ptr);
36 static void _OutByte(tUHCI_Controller *Host, int Reg, Uint8 Value);
37 static void _OutWord(tUHCI_Controller *Host, int Reg, Uint16 Value);
38 static void _OutDWord(tUHCI_Controller *Host, int Reg, Uint32 Value);
39 static Uint16 _InWord(tUHCI_Controller *Host, int Reg);
42 MODULE_DEFINE(0, VERSION, USB_UHCI, UHCI_Initialise, NULL, "USB_Core", NULL);
43 tUHCI_TD gaUHCI_TDPool[NUM_TDs];
44 tUHCI_Controller gUHCI_Controllers[MAX_CONTROLLERS];
45 tUSBHostDef gUHCI_HostDef = {
46 .SendIN = UHCI_DataIN,
47 .SendOUT = UHCI_DataOUT,
48 .SendSETUP = UHCI_SendSetup,
49 .IsOpComplete = UHCI_IsTransferComplete,
50 .CheckPorts = UHCI_CheckPortUpdate
55 * \fn int UHCI_Initialise()
56 * \brief Called to initialise the UHCI Driver
58 int UHCI_Initialise(char **Arguments)
65 // Enumerate PCI Bus, getting a maximum of `MAX_CONTROLLERS` devices
66 while( (id = PCI_GetDeviceByClass(0x0C0300, 0xFFFFFF, id)) >= 0 && i < MAX_CONTROLLERS )
68 tUHCI_Controller *cinfo = &gUHCI_Controllers[i];
70 // NOTE: Check "protocol" from PCI?
73 base_addr = PCI_GetBAR(id, 4);
77 cinfo->IOBase = base_addr & ~1;
78 cinfo->MemIOMap = NULL;
82 cinfo->MemIOMap = (void*)MM_MapHWPages(base_addr, 1);
84 cinfo->IRQNum = PCI_GetIRQ(id);
86 Log_Debug("UHCI", "Controller PCI #%i: IO Base = 0x%x, IRQ %i",
87 id, base_addr, cinfo->IRQNum);
89 IRQ_AddHandler(cinfo->IRQNum, UHCI_InterruptHandler, cinfo);
92 ret = UHCI_Int_InitHost(&gUHCI_Controllers[i]);
99 cinfo->RootHub = USB_RegisterHost(&gUHCI_HostDef, cinfo, 2);
100 LOG("cinfo->RootHub = %p", cinfo->RootHub);
106 LEAVE('i', MODULE_ERR_NOTNEEDED);
107 return MODULE_ERR_NOTNEEDED;
110 if(i == MAX_CONTROLLERS) {
111 Log_Warning("UHCI", "Over "EXPAND_STR(MAX_CONTROLLERS)" UHCI controllers detected, ignoring rest");
113 LEAVE('i', MODULE_ERR_OK);
114 return MODULE_ERR_OK;
118 * \fn void UHCI_Cleanup()
119 * \brief Called just before module is unloaded
125 tUHCI_TD *UHCI_int_AllocateTD(tUHCI_Controller *Cont)
128 for(i = 0; i < NUM_TDs; i ++)
130 if(gaUHCI_TDPool[i].Link == 0) {
131 gaUHCI_TDPool[i].Link = 1;
132 gaUHCI_TDPool[i].Control = 1 << 23;
133 return &gaUHCI_TDPool[i];
135 // Still in use? Skip
136 if( gaUHCI_TDPool[i].Control & (1 << 23) )
138 // Is there a callback on it? Skip
139 if( gaUHCI_TDPool[i]._info.Callback )
141 // TODO: Garbage collect, but that means removing from the list too
143 // Ok, this is actually unused
144 gaUHCI_TDPool[i].Link = 1;
145 gaUHCI_TDPool[i].Control = 1 << 23;
146 return &gaUHCI_TDPool[i];
152 tUHCI_TD *UHCI_int_GetTDFromPhys(tPAddr PAddr)
154 // TODO: Fix this to work with a non-contiguous pool
155 static tPAddr td_pool_base;
156 const int pool_size = NUM_TDs;
158 if(!td_pool_base) td_pool_base = MM_GetPhysAddr( (tVAddr)gaUHCI_TDPool );
159 offset = (PAddr - td_pool_base) / sizeof(gaUHCI_TDPool[0]);
160 if( offset < 0 || offset >= pool_size )
162 Log_Error("UHCI", "TD PAddr %P not from pool", PAddr);
165 return gaUHCI_TDPool + offset;
168 void UHCI_int_AppendTD(tUHCI_Controller *Cont, tUHCI_TD *TD)
174 ENTER("pCont pTD", Cont, TD);
176 // TODO: How to handle FRNUM incrementing while we are in this function?
177 next_frame = (_InWord(Cont, FRNUM) + 2) & (1024-1);
180 if( Cont->FrameList[next_frame] & 1 )
182 // TODO: Ensure 32-bit paddr
183 Cont->FrameList[next_frame] = MM_GetPhysAddr( (tVAddr)TD );
184 TD->Control |= (1 << 24); // Ensure that there is an interrupt for each used frame
185 LOG("next_frame = %i", next_frame);
190 // Find the end of the list
191 link = Cont->FrameList[next_frame];
193 prev_td = UHCI_int_GetTDFromPhys(link);
195 Log_Error("UHCI", "ERROR: TD list is bad");
200 link = prev_td->Link;
201 } while( !(link & 1) );
204 prev_td->Link = MM_GetPhysAddr( (tVAddr)TD );
206 LOG("next_frame = %i, prev_td = %p", next_frame, prev_td);
211 * \brief Send a transaction to the USB bus
212 * \param Cont Controller pointer
213 * \param Addr Function Address * 16 + Endpoint
214 * \param bTgl Data toggle value
216 void *UHCI_int_SendTransaction(
217 tUHCI_Controller *Cont, int Addr, Uint8 Type, int bTgl,
218 tUSBHostCb Cb, void *CbData, void *Data, size_t Length)
222 if( Length > 0x400 ) return NULL; // Controller allows up to 0x500, but USB doesn't
224 td = UHCI_int_AllocateTD(Cont);
227 // TODO: Wait for one to free?
228 Log_Error("UHCI", "No avaliable TDs, transaction dropped");
233 td->Control = (Length - 1) & 0x7FF;
234 td->Control |= (1 << 23);
235 td->Token = ((Length - 1) & 0x7FF) << 21;
236 td->Token |= (bTgl & 1) << 19;
237 td->Token |= (Addr & 0xF) << 15;
238 td->Token |= ((Addr/16) & 0xFF) << 8;
241 // TODO: Ensure 32-bit paddr
242 if( ((tVAddr)Data & (PAGE_SIZE-1)) + Length > PAGE_SIZE ) {
243 Log_Warning("UHCI", "TODO: Support non single page transfers (%x + %x > %x)",
244 (tVAddr)Data & (PAGE_SIZE-1), Length, PAGE_SIZE
246 // TODO: Need to enable IOC to copy the data back
247 // td->BufferPointer =
248 td->_info.bCopyData = 1;
252 td->BufferPointer = MM_GetPhysAddr( (tVAddr)Data );
253 td->_info.bCopyData = 0;
256 // Interrupt on completion
258 td->Control |= (1 << 24);
259 LOG("IOC Cb=%p CbData=%p", Cb, CbData);
260 td->_info.Callback = Cb; // NOTE: if ERRPTR then the TD is kept allocated until checked
261 td->_info.CallbackPtr = CbData;
264 td->_info.DataPtr = Data;
266 UHCI_int_AppendTD(Cont, td);
271 void *UHCI_DataIN(void *Ptr, int Dest, int DataTgl, tUSBHostCb Cb, void *CbData, void *Buf, size_t Length)
273 return UHCI_int_SendTransaction(Ptr, Dest, 0x69, DataTgl, Cb, CbData, Buf, Length);
276 void *UHCI_DataOUT(void *Ptr, int Dest, int DataTgl, tUSBHostCb Cb, void *CbData, void *Buf, size_t Length)
278 return UHCI_int_SendTransaction(Ptr, Dest, 0xE1, DataTgl, Cb, CbData, Buf, Length);
281 void *UHCI_SendSetup(void *Ptr, int Dest, int DataTgl, tUSBHostCb Cb, void *CbData, void *Buf, size_t Length)
283 return UHCI_int_SendTransaction(Ptr, Dest, 0x2D, DataTgl, Cb, CbData, Buf, Length);
286 int UHCI_IsTransferComplete(void *Ptr, void *Handle)
288 tUHCI_TD *td = Handle;
290 ret = !(td->Control & (1 << 23));
292 td->_info.Callback = NULL;
298 // === INTERNAL FUNCTIONS ===
300 * \fn int UHCI_Int_InitHost(tUCHI_Controller *Host)
301 * \brief Initialises a UHCI host controller
302 * \param Host Pointer - Host to initialise
304 int UHCI_Int_InitHost(tUHCI_Controller *Host)
306 ENTER("pHost", Host);
308 _OutWord( Host, USBCMD, 4 ); // GRESET
309 // TODO: Wait for at least 10ms
310 _OutWord( Host, USBCMD, 0 ); // GRESET
312 // Allocate Frame List
313 // - 1 Page, 32-bit address
314 // - 1 page = 1024 4 byte entries
315 Host->FrameList = (void *) MM_AllocDMA(1, 32, &Host->PhysFrameList);
316 if( !Host->FrameList ) {
317 Log_Warning("UHCI", "Unable to allocate frame list, aborting");
321 LOG("Allocated frame list 0x%x (0x%x)", Host->FrameList, Host->PhysFrameList);
322 for( int i = 0; i < 1024; i ++ )
323 Host->FrameList[i] = 1; // Clear List (Disabling all entries)
325 //! \todo Properly fill frame list
327 // Set frame length to 1 ms
328 _OutByte( Host, SOFMOD, 64 );
331 _OutDWord( Host, FLBASEADD, Host->PhysFrameList );
332 _OutWord( Host, FRNUM, 0 );
335 _OutWord( Host, USBINTR, 0x000F );
336 PCI_ConfigWrite( Host->PciId, 0xC0, 2, 0x2000 );
339 _OutWord( Host, USBCMD, 0x0001 );
345 void UHCI_CheckPortUpdate(void *Ptr)
347 tUHCI_Controller *Host = Ptr;
349 for( int i = 0; i < 2; i ++ )
351 int port = PORTSC1 + i*2;
354 status = _InWord(Host, port);
355 // Check for port change
356 if( !(status & 0x0002) ) continue;
357 _OutWord(Host, port, 0x0002);
359 // Check if the port is connected
362 // Tell the USB code it's gone.
363 USB_DeviceDisconnected(Host->RootHub, i);
368 LOG("Port %i has something", i);
369 // Reset port (set bit 9)
371 _OutWord(Host, port, 0x0200);
372 Time_Delay(50); // 50ms delay
373 _OutWord(Host, port, _InWord(Host, port) & ~0x0200);
376 Time_Delay(50); // 50ms delay
377 _OutWord(Host, port, _InWord(Host, port) | 0x0004);
378 // Tell USB there's a new device
379 USB_DeviceConnected(Host->RootHub, i);
384 void UHCI_InterruptHandler(int IRQ, void *Ptr)
386 tUHCI_Controller *Host = Ptr;
387 int frame = (_InWord(Host, FRNUM) - 1) & 0x3FF;
388 Uint16 status = _InWord(Host, USBSTS);
389 // Log_Debug("UHCI", "UHIC Interrupt, status = 0x%x, frame = %i", status, frame);
391 // Interrupt-on-completion
396 for( int i = 0; i < 10; i ++ )
398 link = Host->FrameList[frame];
399 Host->FrameList[frame] = 1;
400 while( link && !(link & 1) )
402 tUHCI_TD *td = UHCI_int_GetTDFromPhys(link);
403 int byte_count = (td->Control&0x7FF)+1;
404 LOG("link = 0x%x, td = %p, byte_count = %i", link, td, byte_count);
405 // Handle non-page aligned destination
406 // TODO: This will break if the destination is not in global memory
407 if(td->_info.bCopyData)
409 void *ptr = (void*)MM_MapTemp(td->BufferPointer);
410 Log_Debug("UHCI", "td->_info.DataPtr = %p", td->_info.DataPtr);
411 memcpy(td->_info.DataPtr, ptr, byte_count);
412 MM_FreeTemp((tVAddr)ptr);
415 if(td->_info.Callback && td->_info.Callback != INVLPTR)
417 LOG("Calling cb %p", td->_info.Callback);
418 td->_info.Callback(td->_info.CallbackPtr, td->_info.DataPtr, byte_count);
419 td->_info.Callback = NULL;
422 if( td->_info.Callback != INVLPTR )
432 // Host->LastCleanedFrame = frame;
435 LOG("status = 0x%02x", status);
436 _OutWord(Host, USBSTS, status);
439 void _OutByte(tUHCI_Controller *Host, int Reg, Uint8 Value)
442 ((Uint8*)Host->MemIOMap)[Reg] = Value;
444 outb(Host->IOBase + Reg, Value);
447 void _OutWord(tUHCI_Controller *Host, int Reg, Uint16 Value)
450 Host->MemIOMap[Reg/2] = Value;
452 outw(Host->IOBase + Reg, Value);
455 void _OutDWord(tUHCI_Controller *Host, int Reg, Uint32 Value)
458 ((Uint32*)Host->MemIOMap)[Reg/4] = Value;
460 outd(Host->IOBase + Reg, Value);
463 Uint16 _InWord(tUHCI_Controller *Host, int Reg)
466 return Host->MemIOMap[Reg/2];
468 return inw(Host->IOBase + Reg);