3 * - By John Hodge (thePowersGang)
11 #include "usb_proto.h"
12 #include "usb_lowlevel.h"
16 void *USB_int_Request(tUSBHost *Host, int Addr, int EndPt, int Type, int Req, int Val, int Indx, int Len, void *Data);
17 int USB_int_SendSetupSetAddress(tUSBHost *Host, int Address);
18 int USB_int_ReadDescriptor(tUSBDevice *Dev, int Endpoint, int Type, int Index, int Length, void *Dest);
19 char *USB_int_GetDeviceString(tUSBDevice *Dev, int Endpoint, int Index);
20 int _UTF16to8(Uint16 *Input, int InputLen, char *Dest);
23 void *USB_int_Request(tUSBHost *Host, int Addr, int EndPt, int Type, int Req, int Val, int Indx, int Len, void *Data)
26 // TODO: Sanity check (and check that Type is valid)
27 struct sDeviceRequest req;
30 req.Value = LittleEndian16( Val );
31 req.Index = LittleEndian16( Indx );
32 req.Length = LittleEndian16( Len );
34 hdl = Host->HostDef->SendSETUP(Host->Ptr, Addr, EndPt, 0, NULL, NULL, &req, sizeof(req));
37 // TODO: Multi-packet transfers
42 hdl = Host->HostDef->SendIN(Host->Ptr, Addr, EndPt, 0, NULL, NULL, Data, Len);
44 hdl2 = Host->HostDef->SendOUT(Host->Ptr, Addr, EndPt, 0, NULL, NULL, NULL, 0);
45 while( Host->HostDef->IsOpComplete(Host->Ptr, hdl2) == 0 )
53 hdl = Host->HostDef->SendOUT(Host->Ptr, Addr, EndPt, 0, NULL, NULL, Data, Len);
57 // Status phase (DataToggle=1)
58 hdl2 = Host->HostDef->SendIN(Host->Ptr, Addr, EndPt, 1, NULL, NULL, NULL, 0);
59 while( Host->HostDef->IsOpComplete(Host->Ptr, hdl2) == 0 )
65 int USB_int_SendSetupSetAddress(tUSBHost *Host, int Address)
67 USB_int_Request(Host, 0, 0, 0x00, 5, Address & 0x7F, 0, 0, NULL);
71 int USB_int_ReadDescriptor(tUSBDevice *Dev, int Endpoint, int Type, int Index, int Length, void *Dest)
73 const int ciMaxPacketSize = 0x400;
74 struct sDeviceRequest req;
79 switch( Type & 0xF00 )
81 case 0x000: req.ReqType |= (0 << 5); break; // Standard
82 case 0x100: req.ReqType |= (1 << 5); break; // Class
83 case 0x200: req.ReqType |= (2 << 5); break; // Vendor
86 req.Request = 6; // GET_DESCRIPTOR
87 req.Value = LittleEndian16( ((Type & 0xFF) << 8) | (Index & 0xFF) );
88 req.Index = LittleEndian16( 0 ); // TODO: Language ID
89 req.Length = LittleEndian16( Length );
91 Dev->Host->HostDef->SendSETUP(
92 Dev->Host->Ptr, Dev->Address, Endpoint,
98 while( Length > ciMaxPacketSize )
100 Dev->Host->HostDef->SendIN(
101 Dev->Host->Ptr, Dev->Address, Endpoint,
103 Dest, ciMaxPacketSize
106 Length -= ciMaxPacketSize;
109 final = Dev->Host->HostDef->SendIN(
110 Dev->Host->Ptr, Dev->Address, Endpoint,
111 bToggle, INVLPTR, NULL,
115 while( Dev->Host->HostDef->IsOpComplete(Dev->Host->Ptr, final) == 0 )
121 char *USB_int_GetDeviceString(tUSBDevice *Dev, int Endpoint, int Index)
123 struct sDescriptor_String str;
124 int src_len, new_len;
127 if(Index == 0) return strdup("");
129 USB_int_ReadDescriptor(Dev, Endpoint, 3, Index, sizeof(str), &str);
131 Log_Error("USB", "String %p:%i:%i:%i descriptor is undersized (%i)",
132 Dev->Host, Dev->Address, Endpoint, Index, str.Length);
135 // if(str.Length > sizeof(str)) {
137 // Log_Error("USB", "String is %i bytes, which is over prealloc size (%i)",
138 // str.Length, sizeof(str)
141 src_len = (str.Length - 2) / sizeof(str.Data[0]);
143 LOG("&str = %p, src_len = %i", &str, src_len);
145 new_len = _UTF16to8(str.Data, src_len, NULL);
146 ret = malloc( new_len + 1 );
147 _UTF16to8(str.Data, src_len, ret);
152 int _UTF16to8(Uint16 *Input, int InputLen, char *Dest)
155 Uint32 saved_bits = 0;
157 for( int i = 0; i < InputLen; i ++)
160 Uint16 val = Input[i];
161 if( val >= 0xD800 && val <= 0xDBFF )
163 // Multibyte - Leading
164 if(i + 1 > InputLen) {
168 saved_bits = (val - 0xD800) << 10;
169 saved_bits += 0x10000;
173 else if( val >= 0xDC00 && val <= 0xDFFF )
179 saved_bits |= (val - 0xDC00);
186 cp_len = WriteUTF8((Uint8*)Dest, cp);