3 * - By John Hodge (thePowersGang)
12 #include "usb_lowlevel.h"
13 #include <workqueue.h>
15 #include "usb_async.h"
18 void USB_ReadDescriptor(tUSBInterface *Iface, int Type, int Index, int Length, void *Data);
19 void USB_Request(tUSBInterface *Iface, int Endpoint, int Type, int Req, int Value, int Index, int Len, void *Data);
20 void USB_SendData(tUSBInterface *Dev, int Endpoint, size_t Length, const void *Data);
21 void USB_WakeCallback(void *Ptr, void *Buf, size_t Length);
22 void USB_AsyncCallback(void *Ptr, void *Buf, size_t Length);
23 void USB_AsyncThread(void *unused);
26 tWorkqueue gUSB_AsyncQueue;
29 void USB_ReadDescriptor(tUSBInterface *Iface, int Type, int Index, int Length, void *Data)
31 USB_int_ReadDescriptor(Iface->Dev, 0, Type, Index, Length, Data);
34 void USB_Request(tUSBInterface *Iface, int Endpoint, int Type, int Req, int Value, int Index, int Len, void *Data)
39 if(Endpoint < 0 || Endpoint >= Iface->nEndpoints)
42 // Get endpoint number
44 endpt = Iface->Endpoints[Endpoint-1].EndpointNum;
48 USB_int_Request(Iface->Dev->Host, Iface->Dev->Address, endpt, Type, Req, Value, Index, Len, Data);
52 void USB_SendData(tUSBInterface *Dev, int Endpoint, size_t Length, const void *Data)
56 ENTER("pDev iEndpoint iLength pData", Dev, Endpoint, Length, Data);
58 ep = &Dev->Endpoints[Endpoint-1];
59 host = Dev->Dev->Host;
61 if( Length > ep->MaxPacketSize ) {
62 Log_Warning("USB", "Max packet size exceeded (%i > %i)", ep->MaxPacketSize);
66 Threads_ClearEvent(THREAD_EVENT_SHORTWAIT);
67 host->HostDef->BulkOUT(
68 host->Ptr, Dev->Dev->Address*16 + Dev->Endpoints[Endpoint-1].EndpointNum,
69 0, USB_WakeCallback, Proc_GetCurThread(),
72 Threads_WaitEvents(THREAD_EVENT_SHORTWAIT);
77 void USB_RecvData(tUSBInterface *Dev, int Endpoint, size_t Length, void *Data)
81 ENTER("pDev iEndpoint iLength pData", Dev, Endpoint, Length, Data);
83 ep = &Dev->Endpoints[Endpoint-1];
84 host = Dev->Dev->Host;
86 if( Length > ep->MaxPacketSize ) {
87 Log_Warning("USB", "Max packet size exceeded (%i > %i)", ep->MaxPacketSize);
91 Threads_ClearEvent(THREAD_EVENT_SHORTWAIT);
92 host->HostDef->BulkIN(
93 host->Ptr, Dev->Dev->Address*16 + Dev->Endpoints[Endpoint-1].EndpointNum,
94 0, USB_WakeCallback, Proc_GetCurThread(),
97 Threads_WaitEvents(THREAD_EVENT_SHORTWAIT);
102 void USB_RecvDataA(tUSBInterface *Dev, int Endpoint, int Length, void *DataBuf, tUSB_DataCallback Callback)
107 ENTER("pDev iEndpoint iLength pDataBuf", Dev, Endpoint, Length, DataBuf);
109 op = malloc(sizeof(*op));
111 op->Endpt = &Dev->Endpoints[Endpoint-1];
115 // TODO: Handle transfers that are larger than one packet
118 host = Dev->Dev->Host;
119 LOG("IN from %p %i:%i", host->Ptr, Dev->Dev->Address, op->Endpt->EndpointNum);
120 host->HostDef->BulkIN(
121 host->Ptr, Dev->Dev->Address*16 + op->Endpt->EndpointNum,
122 0, USB_AsyncCallback, op,
128 // Log_Warning("USB", "TODO: Implement USB_RecvDataA");
131 void USB_WakeCallback(void *Ptr, void *Buf, size_t Length)
133 Threads_PostEvent(Ptr, THREAD_EVENT_SHORTWAIT);
136 void USB_AsyncCallback(void *Ptr, void *Buf, size_t Length)
140 LOG("adding %p to work queue", op);
141 Workqueue_AddWork(&gUSB_AsyncQueue, op);
144 void USB_AsyncThread(void *Unused)
146 Threads_SetName("USB Async IO Thread");
149 tAsyncOp *op = Workqueue_GetWork(&gUSB_AsyncQueue);
150 tUSBInterface *iface = op->Endpt->Interface;
154 iface->Driver->Endpoints[op->Endpt->EndpointIdx].DataAvail(
155 iface, op->Endpt->EndpointIdx,
156 op->Length, op->Data);