* usb_lowlevel.c
* - Low Level IO
*/
-#define DEBUG 1
+#define DEBUG 0
#include <acess.h>
#include "usb.h"
#include "usb_proto.h"
#include "usb_lowlevel.h"
#include <timers.h>
+#include <events.h>
// === PROTOTYPES ===
-void *USB_int_Request(tUSBHost *Host, int Addr, int EndPt, int Type, int Req, int Val, int Indx, int Len, void *Data);
+void *USB_int_Request(tUSBDevice *Dev, int EndPt, int Type, int Req, int Val, int Indx, int Len, void *Data);
+void USB_int_WakeThread(void *Thread, void *Data, size_t Length);
int USB_int_SendSetupSetAddress(tUSBHost *Host, int Address);
int USB_int_ReadDescriptor(tUSBDevice *Dev, int Endpoint, int Type, int Index, int Length, void *Dest);
char *USB_int_GetDeviceString(tUSBDevice *Dev, int Endpoint, int Index);
int _UTF16to8(Uint16 *Input, int InputLen, char *Dest);
// === CODE ===
-void *USB_int_Request(tUSBHost *Host, int Addr, int EndPt, int Type, int Req, int Val, int Indx, int Len, void *Data)
+void *USB_int_Request(tUSBDevice *Device, int EndPt, int Type, int Req, int Val, int Indx, int Len, void *Data)
{
+ tUSBHost *Host = Device->Host;
void *hdl;
// TODO: Sanity check (and check that Type is valid)
struct sDeviceRequest req;
- int dest = Addr * 16 + EndPt; // TODO: Validate
+ tThread *thisthread = Proc_GetCurThread();
+ void *dest_hdl;
+
+ ENTER("pDevice xEndPt iType iReq iVal iIndx iLen pData",
+ Device, EndPt, Type, Req, Val, Indx, Len, Data);
+
+ if( EndPt < 0 || EndPt >= 16 ) {
+ LEAVE('n');
+ return NULL;
+ }
+
+ dest_hdl = Device->EndpointHandles[EndPt];
+ if( !dest_hdl ) {
+ LEAVE('n');
+ return NULL;
+ }
+
req.ReqType = Type;
req.Request = Req;
req.Value = LittleEndian16( Val );
req.Index = LittleEndian16( Indx );
req.Length = LittleEndian16( Len );
-
- hdl = Host->HostDef->SendSETUP(Host->Ptr, dest, 0, NULL, NULL, &req, sizeof(req));
- // TODO: Data toggle?
- // TODO: Multi-packet transfers
- if( Type & 0x80 )
- {
- void *hdl2;
-
- hdl = Host->HostDef->SendIN(Host->Ptr, dest, 0, NULL, NULL, Data, Len);
+ Threads_ClearEvent(THREAD_EVENT_SHORTWAIT);
- hdl2 = Host->HostDef->SendOUT(Host->Ptr, dest, 0, NULL, NULL, NULL, 0);
- while( Host->HostDef->IsOpComplete(Host->Ptr, hdl2) == 0 )
- Time_Delay(1);
+ LOG("Send");
+ if( Type & 0x80 ) {
+ // Inbound data
+ hdl = Host->HostDef->SendControl(Host->Ptr, dest_hdl, USB_int_WakeThread, thisthread, 0,
+ &req, sizeof(req),
+ NULL, 0,
+ Data, Len
+ );
}
- else
- {
- void *hdl2;
-
- if( Len > 0 )
- hdl = Host->HostDef->SendOUT(Host->Ptr, dest, 0, NULL, NULL, Data, Len);
- else
- hdl = NULL;
-
- // Status phase (DataToggle=1)
- hdl2 = Host->HostDef->SendIN(Host->Ptr, dest, 1, NULL, NULL, NULL, 0);
- while( Host->HostDef->IsOpComplete(Host->Ptr, hdl2) == 0 )
- Time_Delay(1);
+ else {
+ // Outbound data
+ hdl = Host->HostDef->SendControl(Host->Ptr, dest_hdl, USB_int_WakeThread, thisthread, 1,
+ &req, sizeof(req),
+ Data, Len,
+ NULL, 0
+ );
}
+ LOG("Wait...");
+ Threads_WaitEvents(THREAD_EVENT_SHORTWAIT);
+
+ LEAVE('p', hdl);
return hdl;
}
+void USB_int_WakeThread(void *Thread, void *Data, size_t Length)
+{
+ Threads_PostEvent(Thread, THREAD_EVENT_SHORTWAIT);
+}
+
int USB_int_SendSetupSetAddress(tUSBHost *Host, int Address)
{
- USB_int_Request(Host, 0, 0, 0x00, 5, Address & 0x7F, 0, 0, NULL);
+ USB_int_Request(Host->RootHubDev, 0, 0x00, 5, Address & 0x7F, 0, 0, NULL);
return 0;
}
int USB_int_ReadDescriptor(tUSBDevice *Dev, int Endpoint, int Type, int Index, int Length, void *Dest)
{
- const int ciMaxPacketSize = 0x400;
struct sDeviceRequest req;
- int bToggle = 0;
- void *final;
- int dest = Dev->Address*16 + Endpoint;
+ void *dest_hdl;
+
+ dest_hdl = Dev->EndpointHandles[Endpoint];
+ if( !dest_hdl ) {
+ return -1;
+ }
+
+ ENTER("pDev xEndpoint iType iIndex iLength pDest",
+ Dev, Endpoint, Type, Index, Length, Dest);
req.ReqType = 0x80;
req.ReqType |= ((Type >> 8) & 0x3) << 5; // Bits 5/6
req.Value = LittleEndian16( ((Type & 0xFF) << 8) | (Index & 0xFF) );
req.Index = LittleEndian16( 0 ); // TODO: Language ID / Interface
req.Length = LittleEndian16( Length );
-
- Dev->Host->HostDef->SendSETUP(
- Dev->Host->Ptr, dest,
- 0, NULL, NULL,
- &req, sizeof(req)
- );
-
- bToggle = 1;
- while( Length > ciMaxPacketSize )
- {
- Dev->Host->HostDef->SendIN(
- Dev->Host->Ptr, dest,
- bToggle, NULL, NULL,
- Dest, ciMaxPacketSize
- );
- bToggle = !bToggle;
- Length -= ciMaxPacketSize;
- }
- final = Dev->Host->HostDef->SendIN(
- Dev->Host->Ptr, dest,
- bToggle, INVLPTR, NULL,
+ Threads_ClearEvent(THREAD_EVENT_SHORTWAIT);
+
+ LOG("Send");
+ Dev->Host->HostDef->SendControl(Dev->Host->Ptr, dest_hdl, USB_int_WakeThread, Proc_GetCurThread(), 0,
+ &req, sizeof(req),
+ NULL, 0,
Dest, Length
);
- while( Dev->Host->HostDef->IsOpComplete(Dev->Host->Ptr, final) == 0 )
- Threads_Yield();
-
+ LOG("Waiting");
+ // TODO: Detect errors?
+ Threads_WaitEvents(THREAD_EVENT_SHORTWAIT);
+
+ LEAVE('i', 0);
return 0;
}
if(Index == 0) return strdup("");
+ str.Length = 0;
USB_int_ReadDescriptor(Dev, Endpoint, 3, Index, sizeof(str), &str);
+ if(str.Length == 0) return NULL;
if(str.Length < 2) {
Log_Error("USB", "String %p:%i:%i:%i descriptor is undersized (%i)",
Dev->Host, Dev->Address, Endpoint, Index, str.Length);