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, NULL, &req, sizeof(req));
36 // TODO: Multi-packet transfers
41 hdl = Host->HostDef->SendIN(Host->Ptr, Addr, EndPt, 0, NULL, NULL, Data, Len);
43 hdl2 = Host->HostDef->SendOUT(Host->Ptr, Addr, EndPt, 0, NULL, NULL, NULL, 0);
44 while( Host->HostDef->IsOpComplete(Host->Ptr, hdl2) == 0 )
52 hdl = Host->HostDef->SendOUT(Host->Ptr, Addr, EndPt, 0, NULL, NULL, Data, Len);
56 // Status phase (DataToggle=1)
57 hdl2 = Host->HostDef->SendIN(Host->Ptr, Addr, EndPt, 1, NULL, 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 switch( Type & 0xF00 )
80 case 0x000: req.ReqType |= (0 << 5); break; // Standard
81 case 0x100: req.ReqType |= (1 << 5); break; // Class
82 case 0x200: req.ReqType |= (2 << 5); break; // Vendor
85 req.Request = 6; // GET_DESCRIPTOR
86 req.Value = LittleEndian16( ((Type & 0xFF) << 8) | (Index & 0xFF) );
87 req.Index = LittleEndian16( 0 ); // TODO: Language ID
88 req.Length = LittleEndian16( Length );
90 Dev->Host->HostDef->SendSETUP(
91 Dev->Host->Ptr, Dev->Address, Endpoint,
97 while( Length > ciMaxPacketSize )
99 Dev->Host->HostDef->SendIN(
100 Dev->Host->Ptr, Dev->Address, Endpoint,
102 Dest, ciMaxPacketSize
105 Length -= ciMaxPacketSize;
108 final = Dev->Host->HostDef->SendIN(
109 Dev->Host->Ptr, Dev->Address, Endpoint,
110 bToggle, INVLPTR, NULL,
114 while( Dev->Host->HostDef->IsOpComplete(Dev->Host->Ptr, final) == 0 )
120 char *USB_int_GetDeviceString(tUSBDevice *Dev, int Endpoint, int Index)
122 struct sDescriptor_String str;
123 int src_len, new_len;
126 if(Index == 0) return strdup("");
128 USB_int_ReadDescriptor(Dev, Endpoint, 3, Index, sizeof(str), &str);
130 Log_Error("USB", "String %p:%i:%i:%i descriptor is undersized (%i)",
131 Dev->Host, Dev->Address, Endpoint, Index, str.Length);
134 // if(str.Length > sizeof(str)) {
136 // Log_Error("USB", "String is %i bytes, which is over prealloc size (%i)",
137 // str.Length, sizeof(str)
140 src_len = (str.Length - 2) / sizeof(str.Data[0]);
142 LOG("&str = %p, src_len = %i", &str, src_len);
144 new_len = _UTF16to8(str.Data, src_len, NULL);
145 ret = malloc( new_len + 1 );
146 _UTF16to8(str.Data, src_len, ret);
151 int _UTF16to8(Uint16 *Input, int InputLen, char *Dest)
154 Uint32 saved_bits = 0;
156 for( int i = 0; i < InputLen; i ++)
159 Uint16 val = Input[i];
160 if( val >= 0xD800 && val <= 0xDBFF )
162 // Multibyte - Leading
163 if(i + 1 > InputLen) {
167 saved_bits = (val - 0xD800) << 10;
168 saved_bits += 0x10000;
172 else if( val >= 0xDC00 && val <= 0xDFFF )
178 saved_bits |= (val - 0xDC00);
185 cp_len = WriteUTF8((Uint8*)Dest, cp);