3 * - By John Hodge (thePowersGang)
5 * Universal Host Controller Interface
8 #define VERSION VER2(0,5)
17 #define MAX_CONTROLLERS 4
21 int UHCI_Initialise(char **Arguments);
23 tUHCI_TD *UHCI_int_AllocateTD(tUHCI_Controller *Cont);
24 void UHCI_int_AppendTD(tUHCI_Controller *Cont, tUHCI_TD *TD);
25 void *UHCI_int_SendTransaction(tUHCI_Controller *Cont, int Addr, Uint8 Type, int bTgl, tUSBHostCb Cb, void *CbData, void *Buf, size_t Length);
26 void *UHCI_DataIN(void *Ptr, int Fcn, int Endpt, int DataTgl, tUSBHostCb Cb, void *CbData, void *Buf, size_t Length);
27 void *UHCI_DataOUT(void *Ptr, int Fcn, int Endpt, int DataTgl, tUSBHostCb Cb, void *CbData, void *Buf, size_t Length);
28 void *UHCI_SendSetup(void *Ptr, int Fcn, int Endpt, int DataTgl, tUSBHostCb Cb, void *CbData, void *Buf, size_t Length);
29 int UHCI_IsTransferComplete(void *Ptr, void *Handle);
30 int UHCI_Int_InitHost(tUHCI_Controller *Host);
31 void UHCI_CheckPortUpdate(void *Ptr);
32 void UHCI_InterruptHandler(int IRQ, void *Ptr);
34 static void _OutByte(tUHCI_Controller *Host, int Reg, Uint8 Value);
35 static void _OutWord(tUHCI_Controller *Host, int Reg, Uint16 Value);
36 static void _OutDWord(tUHCI_Controller *Host, int Reg, Uint32 Value);
37 static Uint16 _InWord(tUHCI_Controller *Host, int Reg);
40 MODULE_DEFINE(0, VERSION, USB_UHCI, UHCI_Initialise, NULL, "USB_Core", NULL);
41 tUHCI_TD gaUHCI_TDPool[NUM_TDs];
42 tUHCI_Controller gUHCI_Controllers[MAX_CONTROLLERS];
43 tUSBHostDef gUHCI_HostDef = {
44 .SendIN = UHCI_DataIN,
45 .SendOUT = UHCI_DataOUT,
46 .SendSETUP = UHCI_SendSetup,
47 .CheckPorts = UHCI_CheckPortUpdate,
48 .IsOpComplete = UHCI_IsTransferComplete
53 * \fn int UHCI_Initialise()
54 * \brief Called to initialise the UHCI Driver
56 int UHCI_Initialise(char **Arguments)
63 // Enumerate PCI Bus, getting a maximum of `MAX_CONTROLLERS` devices
64 while( (id = PCI_GetDeviceByClass(0x0C0300, 0xFFFFFF, id)) >= 0 && i < MAX_CONTROLLERS )
66 tUHCI_Controller *cinfo = &gUHCI_Controllers[i];
68 // NOTE: Check "protocol" from PCI?
71 base_addr = PCI_GetBAR(id, 4);
75 cinfo->IOBase = base_addr & ~1;
76 cinfo->MemIOMap = NULL;
80 cinfo->MemIOMap = (void*)MM_MapHWPages(base_addr, 1);
82 cinfo->IRQNum = PCI_GetIRQ(id);
84 Log_Debug("UHCI", "Controller PCI #%i: IO Base = 0x%x, IRQ %i",
85 id, base_addr, cinfo->IRQNum);
87 IRQ_AddHandler(cinfo->IRQNum, UHCI_InterruptHandler, cinfo);
90 ret = UHCI_Int_InitHost(&gUHCI_Controllers[i]);
97 cinfo->RootHub = USB_RegisterHost(&gUHCI_HostDef, cinfo, 2);
98 LOG("cinfo->RootHub = %p", cinfo->RootHub);
104 LEAVE('i', MODULE_ERR_NOTNEEDED);
105 return MODULE_ERR_NOTNEEDED;
108 if(i == MAX_CONTROLLERS) {
109 Log_Warning("UHCI", "Over "EXPAND_STR(MAX_CONTROLLERS)" UHCI controllers detected, ignoring rest");
111 LEAVE('i', MODULE_ERR_OK);
112 return MODULE_ERR_OK;
116 * \fn void UHCI_Cleanup()
117 * \brief Called just before module is unloaded
123 tUHCI_TD *UHCI_int_AllocateTD(tUHCI_Controller *Cont)
126 for(i = 0; i < NUM_TDs; i ++)
128 if(gaUHCI_TDPool[i].Link == 0) {
129 gaUHCI_TDPool[i].Link = 1;
130 gaUHCI_TDPool[i].Control = 1 << 23;
131 return &gaUHCI_TDPool[i];
133 // Still in use? Skip
134 if( gaUHCI_TDPool[i].Control & (1 << 23) )
136 // Is there a callback on it? Skip
137 if( gaUHCI_TDPool[i]._info.Callback )
139 // TODO: Garbage collect, but that means removing from the list too
141 // Ok, this is actually unused
142 gaUHCI_TDPool[i].Link = 1;
143 gaUHCI_TDPool[i].Control = 1 << 23;
144 return &gaUHCI_TDPool[i];
150 tUHCI_TD *UHCI_int_GetTDFromPhys(tPAddr PAddr)
152 // TODO: Fix this to work with a non-contiguous pool
153 static tPAddr td_pool_base;
154 const int pool_size = NUM_TDs;
156 if(!td_pool_base) td_pool_base = MM_GetPhysAddr( (tVAddr)gaUHCI_TDPool );
157 offset = (PAddr - td_pool_base) / sizeof(gaUHCI_TDPool[0]);
158 if( offset < 0 || offset >= pool_size )
160 Log_Error("UHCI", "TD PAddr %P not from pool", PAddr);
163 return gaUHCI_TDPool + offset;
166 void UHCI_int_AppendTD(tUHCI_Controller *Cont, tUHCI_TD *TD)
168 int next_frame = (_InWord(Cont, FRNUM) + 2) & (1024-1);
172 // TODO: How to handle FRNUM incrementing while we are in this function?
175 if( Cont->FrameList[next_frame] & 1 )
177 // TODO: Ensure 32-bit paddr
178 Cont->FrameList[next_frame] = MM_GetPhysAddr( (tVAddr)TD );
179 TD->Control |= (1 << 24); // Ensure that there is an interrupt for each used frame
180 LOG("next_frame = %i", next_frame);
184 // Find the end of the list
185 link = Cont->FrameList[next_frame];
187 prev_td = UHCI_int_GetTDFromPhys(link);
188 link = prev_td->Link;
189 } while( !(link & 1) );
192 prev_td->Link = MM_GetPhysAddr( (tVAddr)TD );
194 LOG("next_frame = %i, prev_td = %p", next_frame, prev_td);
198 * \brief Send a transaction to the USB bus
199 * \param Cont Controller pointer
200 * \param Addr Function Address * 16 + Endpoint
201 * \param bTgl Data toggle value
203 void *UHCI_int_SendTransaction(
204 tUHCI_Controller *Cont, int Addr, Uint8 Type, int bTgl,
205 tUSBHostCb Cb, void *CbData, void *Data, size_t Length)
209 if( Length > 0x400 ) return NULL; // Controller allows up to 0x500, but USB doesn't
211 td = UHCI_int_AllocateTD(Cont);
214 // TODO: Wait for one to free?
215 Log_Error("UHCI", "No avaliable TDs, transaction dropped");
220 td->Control = (Length - 1) & 0x7FF;
221 td->Control |= (1 << 23);
222 td->Token = ((Length - 1) & 0x7FF) << 21;
223 td->Token |= (bTgl & 1) << 19;
224 td->Token |= (Addr & 0xF) << 15;
225 td->Token |= ((Addr/16) & 0xFF) << 8;
228 // TODO: Ensure 32-bit paddr
229 if( ((tVAddr)Data & (PAGE_SIZE-1)) + Length > PAGE_SIZE ) {
230 Log_Warning("UHCI", "TODO: Support non single page transfers (%x + %x > %x)",
231 (tVAddr)Data & (PAGE_SIZE-1), Length, PAGE_SIZE
233 // TODO: Need to enable IOC to copy the data back
234 // td->BufferPointer =
235 td->_info.bCopyData = 1;
239 td->BufferPointer = MM_GetPhysAddr( (tVAddr)Data );
240 td->_info.bCopyData = 0;
243 // Interrupt on completion
245 td->Control |= (1 << 24);
246 LOG("IOC Cb=%p CbData=%p", Cb, CbData);
247 td->_info.Callback = Cb; // NOTE: if ERRPTR then the TD is kept allocated until checked
248 td->_info.CallbackPtr = CbData;
251 td->_info.DataPtr = Data;
253 UHCI_int_AppendTD(Cont, td);
258 void *UHCI_DataIN(void *Ptr, int Fcn, int Endpt, int DataTgl, tUSBHostCb Cb, void *CbData, void *Buf, size_t Length)
260 return UHCI_int_SendTransaction(Ptr, Fcn*16+Endpt, 0x69, DataTgl, Cb, CbData, Buf, Length);
263 void *UHCI_DataOUT(void *Ptr, int Fcn, int Endpt, int DataTgl, tUSBHostCb Cb, void *CbData, void *Buf, size_t Length)
265 return UHCI_int_SendTransaction(Ptr, Fcn*16+Endpt, 0xE1, DataTgl, Cb, CbData, Buf, Length);
268 void *UHCI_SendSetup(void *Ptr, int Fcn, int Endpt, int DataTgl, tUSBHostCb Cb, void *CbData, void *Buf, size_t Length)
270 return UHCI_int_SendTransaction(Ptr, Fcn*16+Endpt, 0x2D, DataTgl, Cb, CbData, Buf, Length);
273 int UHCI_IsTransferComplete(void *Ptr, void *Handle)
275 tUHCI_TD *td = Handle;
277 ret = !(td->Control & (1 << 23));
279 td->_info.Callback = NULL;
285 // === INTERNAL FUNCTIONS ===
287 * \fn int UHCI_Int_InitHost(tUCHI_Controller *Host)
288 * \brief Initialises a UHCI host controller
289 * \param Host Pointer - Host to initialise
291 int UHCI_Int_InitHost(tUHCI_Controller *Host)
293 ENTER("pHost", Host);
295 _OutWord( Host, USBCMD, 4 ); // GRESET
296 // TODO: Wait for at least 10ms
297 _OutWord( Host, USBCMD, 0 ); // GRESET
299 // Allocate Frame List
300 // - 1 Page, 32-bit address
301 // - 1 page = 1024 4 byte entries
302 Host->FrameList = (void *) MM_AllocDMA(1, 32, &Host->PhysFrameList);
303 if( !Host->FrameList ) {
304 Log_Warning("UHCI", "Unable to allocate frame list, aborting");
308 LOG("Allocated frame list 0x%x (0x%x)", Host->FrameList, Host->PhysFrameList);
309 memsetd( Host->FrameList, 1, 1024 ); // Clear List (Disabling all entries)
311 //! \todo Properly fill frame list
313 // Set frame length to 1 ms
314 _OutByte( Host, SOFMOD, 64 );
317 _OutDWord( Host, FLBASEADD, Host->PhysFrameList );
318 _OutWord( Host, FRNUM, 0 );
321 _OutWord( Host, USBINTR, 0x000F );
322 PCI_ConfigWrite( Host->PciId, 0xC0, 2, 0x2000 );
325 _OutWord( Host, USBCMD, 0x0001 );
331 void UHCI_CheckPortUpdate(void *Ptr)
333 tUHCI_Controller *Host = Ptr;
335 for( int i = 0; i < 2; i ++ )
337 int port = PORTSC1 + i*2;
340 status = _InWord(Host, port);
341 // Check for port change
342 if( !(status & 0x0002) ) continue;
343 _OutWord(Host, port, 0x0002);
345 // Check if the port is connected
348 // Tell the USB code it's gone.
349 USB_DeviceDisconnected(Host->RootHub, i);
354 LOG("Port %i has something", i);
355 // Reset port (set bit 9)
357 _OutWord(Host, port, 0x0200);
358 Time_Delay(50); // 50ms delay
359 _OutWord(Host, port, _InWord(Host, port) & ~0x0200);
362 Time_Delay(50); // 50ms delay
363 _OutWord(Host, port, _InWord(Host, port) | 0x0004);
364 // Tell USB there's a new device
365 USB_DeviceConnected(Host->RootHub, i);
370 void UHCI_InterruptHandler(int IRQ, void *Ptr)
372 tUHCI_Controller *Host = Ptr;
373 int frame = (_InWord(Host, FRNUM) - 1) & 0x3FF;
374 Uint16 status = _InWord(Host, USBSTS);
375 // Log_Debug("UHCI", "UHIC Interrupt, status = 0x%x, frame = %i", status, frame);
377 // Interrupt-on-completion
382 for( int i = 0; i < 10; i ++ )
384 link = Host->FrameList[frame];
385 Host->FrameList[frame] = 1;
386 while( link && !(link & 1) )
388 tUHCI_TD *td = UHCI_int_GetTDFromPhys(link);
389 int byte_count = (td->Control&0x7FF)+1;
390 LOG("link = 0x%x, td = %p, byte_count = %i", link, td, byte_count);
391 // Handle non-page aligned destination
392 // TODO: This will break if the destination is not in global memory
393 if(td->_info.bCopyData)
395 void *ptr = (void*)MM_MapTemp(td->BufferPointer);
396 Log_Debug("UHCI", "td->_info.DataPtr = %p", td->_info.DataPtr);
397 memcpy(td->_info.DataPtr, ptr, byte_count);
398 MM_FreeTemp((tVAddr)ptr);
401 if(td->_info.Callback && td->_info.Callback != INVLPTR)
403 LOG("Calling cb %p", td->_info.Callback);
404 td->_info.Callback(td->_info.CallbackPtr, td->_info.DataPtr, byte_count);
405 td->_info.Callback = NULL;
408 if( td->_info.Callback != INVLPTR )
418 // Host->LastCleanedFrame = frame;
421 LOG("status = 0x%02x", status);
422 _OutWord(Host, USBSTS, status);
425 void _OutByte(tUHCI_Controller *Host, int Reg, Uint8 Value)
428 ((Uint8*)Host->MemIOMap)[Reg] = Value;
430 outb(Host->IOBase + Reg, Value);
433 void _OutWord(tUHCI_Controller *Host, int Reg, Uint16 Value)
436 Host->MemIOMap[Reg/2] = Value;
438 outw(Host->IOBase + Reg, Value);
441 void _OutDWord(tUHCI_Controller *Host, int Reg, Uint32 Value)
444 ((Uint32*)Host->MemIOMap)[Reg/4] = Value;
446 outd(Host->IOBase + Reg, Value);
449 Uint16 _InWord(tUHCI_Controller *Host, int Reg)
452 return Host->MemIOMap[Reg/2];
454 return inw(Host->IOBase + Reg);