3 * - By John Hodge (thePowersGang)
11 #include "usb_proto.h"
12 #include "usb_lowlevel.h"
17 void *USB_int_Request(tUSBHost *Host, int Addr, 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(tUSBHost *Host, int Addr, int EndPt, int Type, int Req, int Val, int Indx, int Len, void *Data)
28 // TODO: Sanity check (and check that Type is valid)
29 struct sDeviceRequest req;
30 int dest = Addr * 16 + EndPt; // TODO: Validate
31 tThread *thisthread = Proc_GetCurThread();
33 ENTER("pHost xdest iType iReq iVal iIndx iLen pData",
34 Host, dest, Type, Req, Val, Indx, Len, Data);
38 req.Value = LittleEndian16( Val );
39 req.Index = LittleEndian16( Indx );
40 req.Length = LittleEndian16( Len );
42 Threads_ClearEvent(THREAD_EVENT_SHORTWAIT);
45 hdl = Host->HostDef->ControlSETUP(Host->Ptr, dest, 0, &req, sizeof(req));
48 // TODO: Multi-packet transfers
54 hdl = Host->HostDef->ControlIN(Host->Ptr, dest, 1, NULL, NULL, Data, Len);
57 hdl = Host->HostDef->ControlOUT(Host->Ptr, dest, 1, USB_int_WakeThread, thisthread, NULL, 0);
62 Host->HostDef->ControlOUT(Host->Ptr, dest, 1, NULL, NULL, Data, Len);
64 // Status phase (DataToggle=1)
66 hdl = Host->HostDef->ControlIN(Host->Ptr, dest, 1, USB_int_WakeThread, thisthread, NULL, 0);
71 // Zero length, IN status
73 hdl = Host->HostDef->ControlIN(Host->Ptr, dest, 1, USB_int_WakeThread, thisthread, NULL, 0);
76 Threads_WaitEvents(THREAD_EVENT_SHORTWAIT);
82 void USB_int_WakeThread(void *Thread, void *Data, size_t Length)
84 Threads_PostEvent(Thread, THREAD_EVENT_SHORTWAIT);
87 int USB_int_SendSetupSetAddress(tUSBHost *Host, int Address)
89 USB_int_Request(Host, 0, 0, 0x00, 5, Address & 0x7F, 0, 0, NULL);
93 int USB_int_ReadDescriptor(tUSBDevice *Dev, int Endpoint, int Type, int Index, int Length, void *Dest)
95 const int ciMaxPacketSize = 0x400;
96 struct sDeviceRequest req;
98 int dest = Dev->Address*16 + Endpoint;
100 ENTER("pDev xdest iType iIndex iLength pDest",
101 Dev, dest, Type, Index, Length, Dest);
104 req.ReqType |= ((Type >> 8) & 0x3) << 5; // Bits 5/6
105 req.ReqType |= (Type >> 12) & 3; // Destination (Device, Interface, Endpoint, Other);
107 req.Request = 6; // GET_DESCRIPTOR
108 req.Value = LittleEndian16( ((Type & 0xFF) << 8) | (Index & 0xFF) );
109 req.Index = LittleEndian16( 0 ); // TODO: Language ID / Interface
110 req.Length = LittleEndian16( Length );
113 Dev->Host->HostDef->ControlSETUP(Dev->Host->Ptr, dest, 0, &req, sizeof(req));
116 while( Length > ciMaxPacketSize )
118 LOG("IN (%i rem)", Length - ciMaxPacketSize);
119 Dev->Host->HostDef->ControlIN(
120 Dev->Host->Ptr, dest,
122 Dest, ciMaxPacketSize
125 Length -= ciMaxPacketSize;
129 Dev->Host->HostDef->ControlIN( Dev->Host->Ptr, dest, bToggle, NULL, NULL, Dest, Length );
131 Threads_ClearEvent(THREAD_EVENT_SHORTWAIT);
133 Dev->Host->HostDef->ControlOUT(
134 Dev->Host->Ptr, dest, 1,
135 USB_int_WakeThread, Proc_GetCurThread(),
140 Threads_WaitEvents(THREAD_EVENT_SHORTWAIT);
146 char *USB_int_GetDeviceString(tUSBDevice *Dev, int Endpoint, int Index)
148 struct sDescriptor_String str;
149 int src_len, new_len;
152 if(Index == 0) return strdup("");
155 USB_int_ReadDescriptor(Dev, Endpoint, 3, Index, sizeof(str), &str);
156 if(str.Length == 0) return NULL;
158 Log_Error("USB", "String %p:%i:%i:%i descriptor is undersized (%i)",
159 Dev->Host, Dev->Address, Endpoint, Index, str.Length);
162 // if(str.Length > sizeof(str)) {
164 // Log_Error("USB", "String is %i bytes, which is over prealloc size (%i)",
165 // str.Length, sizeof(str)
168 src_len = (str.Length - 2) / sizeof(str.Data[0]);
170 LOG("&str = %p, src_len = %i", &str, src_len);
172 new_len = _UTF16to8(str.Data, src_len, NULL);
173 ret = malloc( new_len + 1 );
174 _UTF16to8(str.Data, src_len, ret);
179 int _UTF16to8(Uint16 *Input, int InputLen, char *Dest)
182 Uint32 saved_bits = 0;
184 for( int i = 0; i < InputLen; i ++)
187 Uint16 val = Input[i];
188 if( val >= 0xD800 && val <= 0xDBFF )
190 // Multibyte - Leading
191 if(i + 1 > InputLen) {
195 saved_bits = (val - 0xD800) << 10;
196 saved_bits += 0x10000;
200 else if( val >= 0xDC00 && val <= 0xDFFF )
206 saved_bits |= (val - 0xDC00);
213 cp_len = WriteUTF8((Uint8*)Dest, cp);