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;
99 int dest = Dev->Address*16 + Endpoint;
101 ENTER("pDev xdest iType iIndex iLength pDest",
102 Dev, dest, Type, Index, Length, Dest);
105 req.ReqType |= ((Type >> 8) & 0x3) << 5; // Bits 5/6
106 req.ReqType |= (Type >> 12) & 3; // Destination (Device, Interface, Endpoint, Other);
108 req.Request = 6; // GET_DESCRIPTOR
109 req.Value = LittleEndian16( ((Type & 0xFF) << 8) | (Index & 0xFF) );
110 req.Index = LittleEndian16( 0 ); // TODO: Language ID / Interface
111 req.Length = LittleEndian16( Length );
114 Dev->Host->HostDef->ControlSETUP(Dev->Host->Ptr, dest, 0, &req, sizeof(req));
117 while( Length > ciMaxPacketSize )
119 LOG("IN (%i rem)", Length - ciMaxPacketSize);
120 Dev->Host->HostDef->ControlIN(
121 Dev->Host->Ptr, dest,
123 Dest, ciMaxPacketSize
126 Length -= ciMaxPacketSize;
130 Dev->Host->HostDef->ControlIN( Dev->Host->Ptr, dest, bToggle, NULL, NULL, Dest, Length );
132 Threads_ClearEvent(THREAD_EVENT_SHORTWAIT);
134 final = Dev->Host->HostDef->ControlOUT(
135 Dev->Host->Ptr, dest, 1,
136 USB_int_WakeThread, Proc_GetCurThread(),
141 Threads_WaitEvents(THREAD_EVENT_SHORTWAIT);
147 char *USB_int_GetDeviceString(tUSBDevice *Dev, int Endpoint, int Index)
149 struct sDescriptor_String str;
150 int src_len, new_len;
153 if(Index == 0) return strdup("");
156 USB_int_ReadDescriptor(Dev, Endpoint, 3, Index, sizeof(str), &str);
157 if(str.Length == 0) return NULL;
159 Log_Error("USB", "String %p:%i:%i:%i descriptor is undersized (%i)",
160 Dev->Host, Dev->Address, Endpoint, Index, str.Length);
163 // if(str.Length > sizeof(str)) {
165 // Log_Error("USB", "String is %i bytes, which is over prealloc size (%i)",
166 // str.Length, sizeof(str)
169 src_len = (str.Length - 2) / sizeof(str.Data[0]);
171 LOG("&str = %p, src_len = %i", &str, src_len);
173 new_len = _UTF16to8(str.Data, src_len, NULL);
174 ret = malloc( new_len + 1 );
175 _UTF16to8(str.Data, src_len, ret);
180 int _UTF16to8(Uint16 *Input, int InputLen, char *Dest)
183 Uint32 saved_bits = 0;
185 for( int i = 0; i < InputLen; i ++)
188 Uint16 val = Input[i];
189 if( val >= 0xD800 && val <= 0xDBFF )
191 // Multibyte - Leading
192 if(i + 1 > InputLen) {
196 saved_bits = (val - 0xD800) << 10;
197 saved_bits += 0x10000;
201 else if( val >= 0xDC00 && val <= 0xDFFF )
207 saved_bits |= (val - 0xDC00);
214 cp_len = WriteUTF8((Uint8*)Dest, cp);