3 * - By John Hodge (thePowersGang)
11 #include "usb_proto.h"
12 #include "usb_lowlevel.h"
15 void *USB_int_Request(tUSBHost *Host, int Addr, int EndPt, int Type, int Req, int Val, int Indx, int Len, void *Data);
16 int USB_int_SendSetupSetAddress(tUSBHost *Host, int Address);
17 int USB_int_ReadDescriptor(tUSBDevice *Dev, int Endpoint, int Type, int Index, int Length, void *Dest);
18 char *USB_int_GetDeviceString(tUSBDevice *Dev, int Endpoint, int Index);
19 int _UTF16to8(Uint16 *Input, int InputLen, char *Dest);
22 void *USB_int_Request(tUSBHost *Host, int Addr, int EndPt, int Type, int Req, int Val, int Indx, int Len, void *Data)
25 // TODO: Sanity check (and check that Type is valid)
26 struct sDeviceRequest req;
29 req.Value = LittleEndian16( Val );
30 req.Index = LittleEndian16( Indx );
31 req.Length = LittleEndian16( Len );
33 hdl = Host->HostDef->SendSETUP(Host->Ptr, Addr, EndPt, 0, NULL, &req, sizeof(req));
36 // TODO: Multi-packet transfers
41 hdl = Host->HostDef->SendIN(Host->Ptr, Addr, EndPt, 0, NULL, Data, Len);
43 hdl2 = Host->HostDef->SendOUT(Host->Ptr, Addr, EndPt, 0, NULL, NULL, 0);
44 while( Host->HostDef->IsOpComplete(Host->Ptr, hdl2) == 0 )
52 hdl = Host->HostDef->SendOUT(Host->Ptr, Addr, EndPt, 0, NULL, Data, Len);
56 // Status phase (DataToggle=1)
57 hdl2 = Host->HostDef->SendIN(Host->Ptr, Addr, EndPt, 1, NULL, NULL, 0);
58 while( Host->HostDef->IsOpComplete(Host->Ptr, hdl2) == 0 )
64 int USB_int_SendSetupSetAddress(tUSBHost *Host, int Address)
66 USB_int_Request(Host, 0, 0, 0x00, 5, Address & 0x7F, 0, 0, NULL);
70 int USB_int_ReadDescriptor(tUSBDevice *Dev, int Endpoint, int Type, int Index, int Length, void *Dest)
72 const int ciMaxPacketSize = 0x400;
73 struct sDeviceRequest req;
78 req.Request = 6; // GET_DESCRIPTOR
79 req.Value = LittleEndian16( ((Type & 0xFF) << 8) | (Index & 0xFF) );
80 req.Index = LittleEndian16( 0 ); // TODO: Language ID
81 req.Length = LittleEndian16( Length );
83 Dev->Host->HostDef->SendSETUP(
84 Dev->Host->Ptr, Dev->Address, Endpoint,
90 while( Length > ciMaxPacketSize )
92 Dev->Host->HostDef->SendIN(
93 Dev->Host->Ptr, Dev->Address, Endpoint,
98 Length -= ciMaxPacketSize;
101 final = Dev->Host->HostDef->SendIN(
102 Dev->Host->Ptr, Dev->Address, Endpoint,
107 while( Dev->Host->HostDef->IsOpComplete(Dev->Host->Ptr, final) == 0 )
113 char *USB_int_GetDeviceString(tUSBDevice *Dev, int Endpoint, int Index)
115 struct sDescriptor_String str;
116 int src_len, new_len;
119 USB_int_ReadDescriptor(Dev, Endpoint, 3, Index, sizeof(str), &str);
120 // if(str.Length > sizeof(str)) {
122 // Log_Error("USB", "String is %i bytes, which is over prealloc size (%i)",
123 // str.Length, sizeof(str)
126 src_len = (str.Length - 2) / sizeof(str.Data[0]);
128 new_len = _UTF16to8(str.Data, src_len, NULL);
129 ret = malloc( new_len + 1 );
130 _UTF16to8(str.Data, src_len, ret);
135 int _UTF16to8(Uint16 *Input, int InputLen, char *Dest)
138 Uint32 saved_bits = 0;
140 for( int i = 0; i < InputLen; i ++)
143 Uint16 val = Input[i];
144 if( val >= 0xD800 && val <= 0xDBFF )
146 // Multibyte - Leading
147 if(i + 1 > InputLen) {
151 saved_bits = (val - 0xD800) << 10;
152 saved_bits += 0x10000;
156 else if( val >= 0xDC00 && val <= 0xDFFF )
162 saved_bits |= (val - 0xDC00);
169 cp_len = WriteUTF8((Uint8*)Dest, cp);