#include <modules.h>
#include "usb.h"
+// === IMPORTS ===
+extern void USB_PollThread(void *unused);
+extern void USB_AsyncThread(void *Unused);
+
// === PROTOTYPES ===
int USB_Install(char **Arguments);
void USB_Cleanup(void);
int USB_Install(char **Arguments)
{
Log_Warning("USB", "Not Complete - Devel Only");
+
+ Proc_SpawnWorker(USB_PollThread, NULL);
+ Proc_SpawnWorker(USB_AsyncThread, NULL);
+
return MODULE_ERR_OK;
}
#include <usb_core.h>
#include "usb.h"
#include "usb_lowlevel.h"
-#include <semaphore.h>
+#include <workqueue.h>
typedef struct sAsyncOp tAsyncOp;
// === PROTOTYPES ===
void USB_ReadDescriptor(tUSBInterface *Iface, int Type, int Index, int Length, void *Data);
void USB_Request(tUSBInterface *Iface, int Endpoint, int Type, int Req, int Value, int Index, int Len, void *Data);
+void USB_AsyncCallback(void *Ptr, void *Buf, int Length);
+void USB_AsyncThread(void *unused);
// === GLOBALS ===
-tSemaphore glUSB_AsyncQueue;
+tWorkqueue gUSB_AsyncQueue;
// === CODE ===
void USB_ReadDescriptor(tUSBInterface *Iface, int Type, int Index, int Length, void *Data)
void USB_RecvDataA(tUSBInterface *Dev, int Endpoint, int Length, void *DataBuf, tUSB_DataCallback Callback)
{
- Log_Warning("USB", "TODO: Implement USB_RecvDataA");
+ tAsyncOp *op;
+ tUSBHost *host;
+
+ ENTER("pDev iEndpoint iLength pDataBuf", Dev, Endpoint, Length, DataBuf);
+
+ op = malloc(sizeof(*op));
+ op->Next = NULL;
+ op->Endpt = &Dev->Endpoints[Endpoint-1];
+ op->Length = Length;
+ op->Data = DataBuf;
+
+ // TODO: Handle transfers that are larger than one packet
+
+ host = Dev->Dev->Host;
+ LOG("IN from %p %i:%i", host->Ptr, Dev->Dev->Address, op->Endpt->EndpointNum);
+ host->HostDef->SendIN(
+ host->Ptr, Dev->Dev->Address, op->Endpt->EndpointNum,
+ 0, USB_AsyncCallback, op,
+ DataBuf, Length
+ );
+
+ LEAVE('-');
+
+// Log_Warning("USB", "TODO: Implement USB_RecvDataA");
+}
+
+void USB_AsyncCallback(void *Ptr, void *Buf, int Length)
+{
+ tAsyncOp *op = Ptr;
+ op->Length = Length;
+ LOG("adding %p to work queue", op);
+ Workqueue_AddWork(&gUSB_AsyncQueue, op);
}
void USB_AsyncThread(void *Unused)
{
for(;;)
{
- Semaphore_Wait(&glUSB_AsyncQueue, 1);
-
+ tAsyncOp *op = Workqueue_GetWork(&gUSB_AsyncQueue);
+ tUSBInterface *iface = op->Endpt->Interface;
+
+ LOG("op = %p", op);
+
+ iface->Driver->Endpoints[op->Endpt->EndpointIdx].DataAvail(
+ iface, op->Endpt->EndpointIdx,
+ op->Length, op->Data);
}
}
tUSBEndpoint *endpt;
// Some sanity checks
- if(Endpoint <= 0 || Endpoint >= Iface->nEndpoints) return ;
+ if(Endpoint <= 0 || Endpoint > Iface->nEndpoints) return ;
endpt = &Iface->Endpoints[Endpoint-1];
if(endpt->PollingPeriod > POLL_MAX || endpt->PollingPeriod <= 0)
return ;
endpt->PollingAtoms = (endpt->PollingPeriod + POLL_ATOM-1) / POLL_ATOM;
if(endpt->PollingAtoms > POLL_SLOTS) endpt->PollingAtoms = POLL_SLOTS;
// Add to poll queue
- endpt->Next = gUSB_PollQueues[endpt->PollingAtoms];
- gUSB_PollQueues[endpt->PollingAtoms] = endpt;
+ // TODO: Locking
+ {
+ int idx = giUSB_PollPosition + 1;
+ if(idx >= POLL_SLOTS) idx -= POLL_SLOTS;
+ LOG("idx = %i", idx);
+ endpt->Next = gUSB_PollQueues[idx];
+ gUSB_PollQueues[idx] = endpt;
+ }
}
/**
*/
int USB_PollThread(void *unused)
{
+ Threads_SetName("USB Polling Thread");
for(;;)
{
tUSBEndpoint *ep, *prev;
prev = (void*)( (tVAddr)&gUSB_PollQueues[giUSB_PollPosition] - offsetof(tUSBEndpoint, Next) );
// Process queue
+// LOG("giUSB_PollPosition = %i", giUSB_PollPosition);
for( ep = gUSB_PollQueues[giUSB_PollPosition]; ep; prev = ep, ep = ep->Next )
{
int period_in_atoms = ep->PollingAtoms;
+ LOG("%i: ep = %p", giUSB_PollPosition, ep);
// Check for invalid entries
if(period_in_atoms < 0 || period_in_atoms > POLL_ATOM)
// TODO: Async checking?
// - Send the read request on all of them then wait for the first to complete
USB_RecvDataA(
- ep->Interface, ep->EndpointIdx,
+ ep->Interface, ep->EndpointIdx+1,
ep->MaxPacketSize, ep->InputData,
ep->Interface->Driver->Endpoints[ep->EndpointIdx].DataAvail
);
*
* Universal Host Controller Interface
*/
-#define DEBUG 0
+#define DEBUG 1
#define VERSION VER2(0,5)
#include <acess.h>
#include <vfs.h>
// TODO: How to handle FRNUM incrementing while we are in this function?
+// LOG("USBINTR = 0x%x", inw(Cont->IOBase + USBINTR));
+
// Empty list
if( Cont->FrameList[next_frame] & 1 )
{
// Interrupt on completion
if( Cb ) {
td->Control |= (1 << 24);
+ LOG("IOC Cb=%p CbData=%p", Cb, CbData);
td->_info.Callback = Cb; // NOTE: if ERRPTR then the TD is kept allocated until checked
td->_info.CallbackPtr = CbData;
}
void UHCI_InterruptHandler(int IRQ, void *Ptr)
{
tUHCI_Controller *Host = Ptr;
+ int frame = ((int)inw(Host->IOBase + FRNUM) & 0x3FF) - 1;
Uint16 status = inw(Host->IOBase + USBSTS);
- Log_Debug("UHCI", "UHIC Interrupt, status = 0x%x", status);
+ if(frame < 0) frame += 1024;
+ Log_Debug("UHCI", "UHIC Interrupt, status = 0x%x, frame = %i", status, frame);
// Interrupt-on-completion
if( status & 1 )
{
tPAddr link;
- int frame = inw(Host->IOBase + FRNUM) - 1;
- if(frame < 0) frame += 1024;
-
+
link = Host->FrameList[frame];
Host->FrameList[frame] = 1;
while( !(link & 1) )
{
tUHCI_TD *td = UHCI_int_GetTDFromPhys(link);
int byte_count = (td->Control&0x7FF)+1;
+ LOG("link = 0x%x, td = %p, byte_count = %i", link, td, byte_count);
// Handle non-page aligned destination
// TODO: This will break if the destination is not in global memory
if(td->_info.bCopyData)
// Callback
if(td->_info.Callback && td->_info.Callback != INVLPTR)
{
+ LOG("Calling cb %p", td->_info.Callback);
td->_info.Callback(td->_info.CallbackPtr, td->_info.DataPtr, byte_count);
td->_info.Callback = NULL;
}
// Host->LastCleanedFrame = frame;
}
+ LOG("status = 0x%02x", status);
outw(Host->IOBase + USBSTS, status);
}