3 * - By John Hodge (thePowersGang)
11 #include "usb_proto.h"
12 #include "usb_lowlevel.h"
17 void *USB_int_Request(tUSBDevice *Dev, 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(tUSBDevice *Device, int EndPt, int Type, int Req, int Val, int Indx, int Len, void *Data)
27 tUSBHost *Host = Device->Host;
29 // TODO: Sanity check (and check that Type is valid)
30 struct sDeviceRequest req;
31 tThread *thisthread = Proc_GetCurThread();
34 ENTER("pDevice xEndPt iType iReq iVal iIndx iLen pData",
35 Device, EndPt, Type, Req, Val, Indx, Len, Data);
37 if( EndPt < 0 || EndPt >= 16 ) {
42 dest_hdl = Device->EndpointHandles[EndPt];
50 req.Value = LittleEndian16( Val );
51 req.Index = LittleEndian16( Indx );
52 req.Length = LittleEndian16( Len );
54 Threads_ClearEvent(THREAD_EVENT_SHORTWAIT);
59 hdl = Host->HostDef->SendControl(Host->Ptr, dest_hdl, USB_int_WakeThread, thisthread, 0,
67 hdl = Host->HostDef->SendControl(Host->Ptr, dest_hdl, USB_int_WakeThread, thisthread, 1,
74 Threads_WaitEvents(THREAD_EVENT_SHORTWAIT);
80 void USB_int_WakeThread(void *Thread, void *Data, size_t Length)
82 Threads_PostEvent(Thread, THREAD_EVENT_SHORTWAIT);
85 int USB_int_SendSetupSetAddress(tUSBHost *Host, int Address)
87 USB_int_Request(Host->RootHubDev, 0, 0x00, 5, Address & 0x7F, 0, 0, NULL);
91 int USB_int_ReadDescriptor(tUSBDevice *Dev, int Endpoint, int Type, int Index, int Length, void *Dest)
93 struct sDeviceRequest req;
96 dest_hdl = Dev->EndpointHandles[Endpoint];
101 ENTER("pDev xEndpoint iType iIndex iLength pDest",
102 Dev, Endpoint, Type, Index, Length, Dest);
105 req.ReqType |= ((Type >> 8) & 0x3) << 5; // Bits 5/6
106 req.ReqType |= (Type >> 12) & 3; // Destination (Device, Interface, Endpoint, Other);
108 req.Request = 6; // GET_DESCRIPTOR
109 req.Value = LittleEndian16( ((Type & 0xFF) << 8) | (Index & 0xFF) );
110 req.Index = LittleEndian16( 0 ); // TODO: Language ID / Interface
111 req.Length = LittleEndian16( Length );
113 Threads_ClearEvent(THREAD_EVENT_SHORTWAIT);
116 Dev->Host->HostDef->SendControl(Dev->Host->Ptr, dest_hdl, USB_int_WakeThread, Proc_GetCurThread(), 0,
123 // TODO: Detect errors?
124 Threads_WaitEvents(THREAD_EVENT_SHORTWAIT);
130 char *USB_int_GetDeviceString(tUSBDevice *Dev, int Endpoint, int Index)
132 struct sDescriptor_String str;
133 int src_len, new_len;
136 if(Index == 0) return strdup("");
139 USB_int_ReadDescriptor(Dev, Endpoint, 3, Index, sizeof(str), &str);
140 if(str.Length == 0) return NULL;
142 Log_Error("USB", "String %p:%i:%i:%i descriptor is undersized (%i)",
143 Dev->Host, Dev->Address, Endpoint, Index, str.Length);
146 // if(str.Length > sizeof(str)) {
148 // Log_Error("USB", "String is %i bytes, which is over prealloc size (%i)",
149 // str.Length, sizeof(str)
152 src_len = (str.Length - 2) / sizeof(str.Data[0]);
154 LOG("&str = %p, src_len = %i", &str, src_len);
156 new_len = _UTF16to8(str.Data, src_len, NULL);
157 ret = malloc( new_len + 1 );
158 _UTF16to8(str.Data, src_len, ret);
163 int _UTF16to8(Uint16 *Input, int InputLen, char *Dest)
166 Uint32 saved_bits = 0;
168 for( int i = 0; i < InputLen; i ++)
171 Uint16 val = Input[i];
172 if( val >= 0xD800 && val <= 0xDBFF )
174 // Multibyte - Leading
175 if(i + 1 > InputLen) {
179 saved_bits = (val - 0xD800) << 10;
180 saved_bits += 0x10000;
184 else if( val >= 0xDC00 && val <= 0xDFFF )
190 saved_bits |= (val - 0xDC00);
197 cp_len = WriteUTF8((Uint8*)Dest, cp);