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;
28 int dest = Addr * 16 + EndPt; // TODO: Validate
30 ENTER("pHost xdest iType iReq iVal iIndx iLen pData",
31 Host, dest, Type, Req, Val, Indx, Len, Data);
35 req.Value = LittleEndian16( Val );
36 req.Index = LittleEndian16( Indx );
37 req.Length = LittleEndian16( Len );
40 hdl = Host->HostDef->SendSETUP(Host->Ptr, dest, 0, NULL, NULL, &req, sizeof(req));
43 // TODO: Multi-packet transfers
49 hdl = Host->HostDef->SendIN(Host->Ptr, dest, 1, NULL, NULL, Data, Len);
52 hdl = Host->HostDef->SendOUT(Host->Ptr, dest, 1, INVLPTR, NULL, NULL, 0);
57 Host->HostDef->SendOUT(Host->Ptr, dest, 1, NULL, NULL, Data, Len);
59 // Status phase (DataToggle=1)
61 hdl = Host->HostDef->SendIN(Host->Ptr, dest, 1, INVLPTR, NULL, NULL, 0);
66 // Zero length, IN status
68 hdl = Host->HostDef->SendIN(Host->Ptr, dest, 1, INVLPTR, NULL, NULL, 0);
71 while( Host->HostDef->IsOpComplete(Host->Ptr, hdl) == 0 )
77 int USB_int_SendSetupSetAddress(tUSBHost *Host, int Address)
79 USB_int_Request(Host, 0, 0, 0x00, 5, Address & 0x7F, 0, 0, NULL);
83 int USB_int_ReadDescriptor(tUSBDevice *Dev, int Endpoint, int Type, int Index, int Length, void *Dest)
85 const int ciMaxPacketSize = 0x400;
86 struct sDeviceRequest req;
89 int dest = Dev->Address*16 + Endpoint;
91 ENTER("pDev xdest iType iIndex iLength pDest",
92 Dev, dest, Type, Index, Length, Dest);
95 req.ReqType |= ((Type >> 8) & 0x3) << 5; // Bits 5/6
96 req.ReqType |= (Type >> 12) & 3; // Destination (Device, Interface, Endpoint, Other);
98 req.Request = 6; // GET_DESCRIPTOR
99 req.Value = LittleEndian16( ((Type & 0xFF) << 8) | (Index & 0xFF) );
100 req.Index = LittleEndian16( 0 ); // TODO: Language ID / Interface
101 req.Length = LittleEndian16( Length );
104 Dev->Host->HostDef->SendSETUP(
105 Dev->Host->Ptr, dest,
111 while( Length > ciMaxPacketSize )
113 LOG("IN (%i rem)", Length - ciMaxPacketSize);
114 Dev->Host->HostDef->SendIN(
115 Dev->Host->Ptr, dest,
117 Dest, ciMaxPacketSize
120 Length -= ciMaxPacketSize;
124 Dev->Host->HostDef->SendIN(
125 Dev->Host->Ptr, dest,
131 final = Dev->Host->HostDef->SendOUT(Dev->Host->Ptr, dest, 1, INVLPTR, NULL, NULL, 0);
134 while( Dev->Host->HostDef->IsOpComplete(Dev->Host->Ptr, final) == 0 )
135 Threads_Yield(); // BAD BAD BAD
141 char *USB_int_GetDeviceString(tUSBDevice *Dev, int Endpoint, int Index)
143 struct sDescriptor_String str;
144 int src_len, new_len;
147 if(Index == 0) return strdup("");
149 USB_int_ReadDescriptor(Dev, Endpoint, 3, Index, sizeof(str), &str);
151 Log_Error("USB", "String %p:%i:%i:%i descriptor is undersized (%i)",
152 Dev->Host, Dev->Address, Endpoint, Index, str.Length);
155 // if(str.Length > sizeof(str)) {
157 // Log_Error("USB", "String is %i bytes, which is over prealloc size (%i)",
158 // str.Length, sizeof(str)
161 src_len = (str.Length - 2) / sizeof(str.Data[0]);
163 LOG("&str = %p, src_len = %i", &str, src_len);
165 new_len = _UTF16to8(str.Data, src_len, NULL);
166 ret = malloc( new_len + 1 );
167 _UTF16to8(str.Data, src_len, ret);
172 int _UTF16to8(Uint16 *Input, int InputLen, char *Dest)
175 Uint32 saved_bits = 0;
177 for( int i = 0; i < InputLen; i ++)
180 Uint16 val = Input[i];
181 if( val >= 0xD800 && val <= 0xDBFF )
183 // Multibyte - Leading
184 if(i + 1 > InputLen) {
188 saved_bits = (val - 0xD800) << 10;
189 saved_bits += 0x10000;
193 else if( val >= 0xDC00 && val <= 0xDFFF )
199 saved_bits |= (val - 0xDC00);
206 cp_len = WriteUTF8((Uint8*)Dest, cp);