3 * - By John Hodge (thePowersGang)
11 #include "usb_proto.h"
12 #include "usb_lowlevel.h"
17 void *USB_int_Request(tUSBHost *Host, int Addr, int EndPt, int Type, int Req, int Val, int Indx, int Len, void *Data);
18 void USB_int_WakeThread(void *Thread, void *Data, size_t Length);
19 int USB_int_SendSetupSetAddress(tUSBHost *Host, int Address);
20 int USB_int_ReadDescriptor(tUSBDevice *Dev, int Endpoint, int Type, int Index, int Length, void *Dest);
21 char *USB_int_GetDeviceString(tUSBDevice *Dev, int Endpoint, int Index);
22 int _UTF16to8(Uint16 *Input, int InputLen, char *Dest);
25 void *USB_int_Request(tUSBHost *Host, int Addr, int EndPt, int Type, int Req, int Val, int Indx, int Len, void *Data)
28 // TODO: Sanity check (and check that Type is valid)
29 struct sDeviceRequest req;
30 int dest = Addr * 16 + EndPt; // TODO: Validate
31 void *dest_hdl = (void*)(dest+1); // TODO: Get registered handle instead
32 tThread *thisthread = Proc_GetCurThread();
34 ENTER("pHost xdest iType iReq iVal iIndx iLen pData",
35 Host, dest, Type, Req, Val, Indx, Len, Data);
39 req.Value = LittleEndian16( Val );
40 req.Index = LittleEndian16( Indx );
41 req.Length = LittleEndian16( Len );
43 Threads_ClearEvent(THREAD_EVENT_SHORTWAIT);
48 hdl = Host->HostDef->SendControl(Host->Ptr, dest_hdl, USB_int_WakeThread, thisthread, 0,
56 hdl = Host->HostDef->SendControl(Host->Ptr, dest_hdl, USB_int_WakeThread, thisthread, 1,
63 Threads_WaitEvents(THREAD_EVENT_SHORTWAIT);
69 void USB_int_WakeThread(void *Thread, void *Data, size_t Length)
71 Threads_PostEvent(Thread, THREAD_EVENT_SHORTWAIT);
74 int USB_int_SendSetupSetAddress(tUSBHost *Host, int Address)
76 USB_int_Request(Host, 0, 0, 0x00, 5, Address & 0x7F, 0, 0, NULL);
80 int USB_int_ReadDescriptor(tUSBDevice *Dev, int Endpoint, int Type, int Index, int Length, void *Dest)
82 struct sDeviceRequest req;
83 int dest = Dev->Address*16 + Endpoint;
84 void *dest_hdl = (void*)(dest+1); // TODO: Get correct handle
86 ENTER("pDev xdest iType iIndex iLength pDest",
87 Dev, dest, Type, Index, Length, Dest);
90 req.ReqType |= ((Type >> 8) & 0x3) << 5; // Bits 5/6
91 req.ReqType |= (Type >> 12) & 3; // Destination (Device, Interface, Endpoint, Other);
93 req.Request = 6; // GET_DESCRIPTOR
94 req.Value = LittleEndian16( ((Type & 0xFF) << 8) | (Index & 0xFF) );
95 req.Index = LittleEndian16( 0 ); // TODO: Language ID / Interface
96 req.Length = LittleEndian16( Length );
98 Threads_ClearEvent(THREAD_EVENT_SHORTWAIT);
101 Dev->Host->HostDef->SendControl(Dev->Host->Ptr, dest_hdl, USB_int_WakeThread, Proc_GetCurThread(), 0,
108 // TODO: Detect errors?
109 Threads_WaitEvents(THREAD_EVENT_SHORTWAIT);
115 char *USB_int_GetDeviceString(tUSBDevice *Dev, int Endpoint, int Index)
117 struct sDescriptor_String str;
118 int src_len, new_len;
121 if(Index == 0) return strdup("");
124 USB_int_ReadDescriptor(Dev, Endpoint, 3, Index, sizeof(str), &str);
125 if(str.Length == 0) return NULL;
127 Log_Error("USB", "String %p:%i:%i:%i descriptor is undersized (%i)",
128 Dev->Host, Dev->Address, Endpoint, Index, str.Length);
131 // if(str.Length > sizeof(str)) {
133 // Log_Error("USB", "String is %i bytes, which is over prealloc size (%i)",
134 // str.Length, sizeof(str)
137 src_len = (str.Length - 2) / sizeof(str.Data[0]);
139 LOG("&str = %p, src_len = %i", &str, src_len);
141 new_len = _UTF16to8(str.Data, src_len, NULL);
142 ret = malloc( new_len + 1 );
143 _UTF16to8(str.Data, src_len, ret);
148 int _UTF16to8(Uint16 *Input, int InputLen, char *Dest)
151 Uint32 saved_bits = 0;
153 for( int i = 0; i < InputLen; i ++)
156 Uint16 val = Input[i];
157 if( val >= 0xD800 && val <= 0xDBFF )
159 // Multibyte - Leading
160 if(i + 1 > InputLen) {
164 saved_bits = (val - 0xD800) << 10;
165 saved_bits += 0x10000;
169 else if( val >= 0xDC00 && val <= 0xDFFF )
175 saved_bits |= (val - 0xDC00);
182 cp_len = WriteUTF8((Uint8*)Dest, cp);