10 #include "usb_proto.h"
13 extern tUSBHost *gUSB_Hosts;
18 tUSBHub *USB_RegisterHost(tUSBHostDef *HostDef, void *ControllerPtr, int nPorts);
19 void USB_DeviceConnected(tUSBHub *Hub, int Port);
20 void USB_DeviceDisconnected(tUSBHub *Hub, int Port);
21 void *USB_GetDeviceDataPtr(tUSBInterface *Dev);
22 void USB_SetDeviceDataPtr(tUSBInterface *Dev, void *Ptr);
23 int USB_int_AllocateAddress(tUSBHost *Host);
24 int USB_int_SendSetupSetAddress(tUSBHost *Host, int Address);
25 int USB_int_ReadDescriptor(tUSBDevice *Dev, int Endpoint, int Type, int Index, int Length, void *Dest);
26 char *USB_int_GetDeviceString(tUSBDevice *Dev, int Endpoint, int Index);
28 int _UTF16to8(Uint16 *Input, int InputLen, char *Dest);
31 tUSBHub *USB_RegisterHost(tUSBHostDef *HostDef, void *ControllerPtr, int nPorts)
35 host = malloc(sizeof(tUSBHost) + nPorts*sizeof(void*));
40 host->HostDef = HostDef;
41 host->Ptr = ControllerPtr;
42 memset(host->AddressBitmap, 0, sizeof(host->AddressBitmap));
44 // host->RootHubDev.Next = NULL;
45 host->RootHubDev.ParentHub = NULL;
46 host->RootHubDev.Host = host;
47 host->RootHubDev.Address = 0;
48 // host->RootHubDev.Driver = NULL;
49 // host->RootHubDev.Data = NULL;
51 host->RootHub.Device = &host->RootHubDev;
52 host->RootHub.CheckPorts = NULL;
53 host->RootHub.nPorts = nPorts;
54 memset(host->RootHub.Devices, 0, sizeof(void*)*nPorts);
57 host->Next = gUSB_Hosts;
60 return &host->RootHub;
63 void USB_DeviceConnected(tUSBHub *Hub, int Port)
66 if( Port >= Hub->nPorts ) return ;
67 if( Hub->Devices[Port] ) return ;
69 ENTER("pHub iPort", Hub, Port);
71 // 0. Perform port init? (done in hub?)
74 dev = malloc(sizeof(tUSBDevice));
77 dev->Host = Hub->Device->Host;
82 // 1. Assign an address
83 dev->Address = USB_int_AllocateAddress(dev->Host);
84 if(dev->Address == 0) {
85 Log_Error("USB", "No addresses avaliable on host %p", dev->Host);
90 USB_int_SendSetupSetAddress(dev->Host, dev->Address);
91 LOG("Assigned address %i", dev->Address);
93 // 2. Get device information
95 struct sDescriptor_Device desc;
96 LOG("Getting device descriptor");
97 // Endpoint 0, Desc Type 1, Index 0
98 USB_int_ReadDescriptor(dev, 0, 1, 0, sizeof(desc), &desc);
100 LOG("Device Descriptor = {");
101 LOG(" .Length = %i", desc.Length);
102 LOG(" .Type = %i", desc.Type);
103 LOG(" .USBVersion = 0x%04x", desc.USBVersion);
104 LOG(" .DeviceClass = 0x%02x", desc.DeviceClass);
105 LOG(" .DeviceSubClass = 0x%02x", desc.DeviceSubClass);
106 LOG(" .DeviceProtocol = 0x%02x", desc.DeviceProtocol);
107 LOG(" .MaxPacketSize = 0x%02x", desc.MaxPacketSize);
108 LOG(" .VendorID = 0x%04x", desc.VendorID);
109 LOG(" .ProductID = 0x%04x", desc.ProductID);
110 LOG(" .DeviceID = 0x%04x", desc.DeviceID);
111 LOG(" .ManufacturerStr = Str %i", desc.ManufacturerStr);
112 LOG(" .ProductStr = Str %i", desc.ProductStr);
113 LOG(" .SerialNumberStr = Str %i", desc.SerialNumberStr);
114 LOG(" .NumConfigurations = %i", desc.SerialNumberStr);
117 if( desc.ManufacturerStr )
119 char *tmp = USB_int_GetDeviceString(dev, 0, desc.ManufacturerStr);
120 LOG("ManufacturerStr = '%s'", tmp);
123 if( desc.ProductStr )
125 char *tmp = USB_int_GetDeviceString(dev, 0, desc.ProductStr);
126 LOG("ProductStr = '%s'", tmp);
129 if( desc.SerialNumberStr )
131 char *tmp = USB_int_GetDeviceString(dev, 0, desc.SerialNumberStr);
132 LOG("SerialNumbertStr = '%s'", tmp);
137 // 3. Get configurations
138 for( int i = 0; i < 1; i ++ )
140 struct sDescriptor_Configuration desc;
144 USB_int_ReadDescriptor(dev, 0, 2, i, sizeof(desc), &desc);
145 LOG("Configuration Descriptor %i = {", i);
146 LOG(" .Length = %i", desc.Length);
147 LOG(" .Type = %i", desc.Type);
148 LOG(" .TotalLength = 0x%x", LittleEndian16(desc.TotalLength));
149 LOG(" .NumInterfaces = %i", desc.NumInterfaces);
150 LOG(" .ConfigurationValue = %i", desc.ConfigurationValue);
151 LOG(" .ConfigurationStr = %i", desc.ConfigurationStr);
152 LOG(" .AttributesBmp = 0b%b", desc.AttributesBmp);
153 LOG(" .MaxPower = %i (*2mA)", desc.MaxPower);
155 if( desc.ConfigurationStr ) {
156 char *tmp = USB_int_GetDeviceString(dev, 0, desc.ConfigurationStr);
157 LOG("ConfigurationStr = '%s'", tmp);
161 cur_ptr = full_buf = malloc( LittleEndian16(desc.TotalLength) );
162 USB_int_ReadDescriptor(dev, 0, 2, i, desc.TotalLength, full_buf);
164 cur_ptr += desc.Length;
167 for( int j = 0; j < desc.NumInterfaces; j ++ )
169 struct sDescriptor_Interface *iface;
170 iface = (void*)cur_ptr;
171 // TODO: Sanity check with remaining space
172 cur_ptr += sizeof(*iface);
174 LOG("Interface %i/%i = {", i, j);
175 LOG(" .InterfaceNum = %i", iface->InterfaceNum);
176 LOG(" .NumEndpoints = %i", iface->NumEndpoints);
177 LOG(" .InterfaceClass = 0x%x", iface->InterfaceClass);
178 LOG(" .InterfaceSubClass = 0x%x", iface->InterfaceSubClass);
179 LOG(" .InterfaceProcol = 0x%x", iface->InterfaceProtocol);
181 if( iface->InterfaceStr ) {
182 char *tmp = USB_int_GetDeviceString(dev, 0, iface->InterfaceStr);
183 LOG(" .InterfaceStr = %i '%s'", iface->InterfaceStr, tmp);
188 for( int k = 0; k < iface->NumEndpoints; k ++ )
190 struct sDescriptor_Endpoint *endpt;
191 endpt = (void*)cur_ptr;
192 // TODO: Sanity check with remaining space
193 cur_ptr += sizeof(*endpt);
195 LOG("Endpoint %i/%i/%i = {", i, j, k);
196 LOG(" .Address = 0x%2x", endpt->Address);
197 LOG(" .Attributes = 0b%8b", endpt->Attributes);
198 LOG(" .MaxPacketSize = %i", LittleEndian16(endpt->MaxPacketSize));
199 LOG(" .PollingInterval = %i", endpt->PollingInterval);
211 void USB_DeviceDisconnected(tUSBHub *Hub, int Port)
216 void *USB_GetDeviceDataPtr(tUSBInterface *Dev) { return Dev->Data; }
217 void USB_SetDeviceDataPtr(tUSBInterface *Dev, void *Ptr) { Dev->Data = Ptr; }
219 int USB_int_AllocateAddress(tUSBHost *Host)
222 for( i = 1; i < 128; i ++ )
224 if(Host->AddressBitmap[i/8] & (1 << (i%8)))
226 Host->AddressBitmap[i/8] |= 1 << (i%8);
232 void USB_int_DeallocateAddress(tUSBHost *Host, int Address)
234 Host->AddressBitmap[Address/8] &= ~(1 << (Address%8));
237 void *USB_int_Request(tUSBHost *Host, int Addr, int EndPt, int Type, int Req, int Val, int Indx, int Len, void *Data)
240 // TODO: Sanity check (and check that Type is valid)
241 struct sDeviceRequest req;
244 req.Value = LittleEndian16( Val );
245 req.Index = LittleEndian16( Indx );
246 req.Length = LittleEndian16( Len );
248 hdl = Host->HostDef->SendSETUP(Host->Ptr, Addr, EndPt, 0, NULL, &req, sizeof(req));
250 // TODO: Data toggle?
251 // TODO: Correct sequence? (Some OUT requests need an IN)
254 hdl = Host->HostDef->SendIN(Host->Ptr, Addr, EndPt, 0, NULL, Data, Len);
255 while( Host->HostDef->IsOpComplete(Host->Ptr, hdl) == 0 )
260 hdl = Host->HostDef->SendOUT(Host->Ptr, Addr, EndPt, 0, NULL, Data, Len);
265 int USB_int_SendSetupSetAddress(tUSBHost *Host, int Address)
268 struct sDeviceRequest req;
269 req.ReqType = 0; // bmRequestType
270 req.Request = 5; // SET_ADDRESS
272 req.Value = LittleEndian16( Address & 0x7F ); // wValue
273 req.Index = LittleEndian16( 0 ); // wIndex
274 req.Length = LittleEndian16( 0 ); // wLength
276 // Addr 0:0, Data Toggle = 0, no interrupt
277 hdl = Host->HostDef->SendSETUP(Host->Ptr, 0, 0, 0, NULL, &req, sizeof(req));
281 hdl = Host->HostDef->SendIN(Host->Ptr, 0, 0, 0, NULL, NULL, 0);
283 while( Host->HostDef->IsOpComplete(Host->Ptr, hdl) == 0 )
289 int USB_int_ReadDescriptor(tUSBDevice *Dev, int Endpoint, int Type, int Index, int Length, void *Dest)
291 const int ciMaxPacketSize = 0x400;
292 struct sDeviceRequest req;
297 req.Request = 6; // GET_DESCRIPTOR
298 req.Value = LittleEndian16( ((Type & 0xFF) << 8) | (Index & 0xFF) );
299 req.Index = LittleEndian16( 0 ); // TODO: Language ID
300 req.Length = LittleEndian16( Length );
302 Dev->Host->HostDef->SendSETUP(
303 Dev->Host->Ptr, Dev->Address, Endpoint,
309 while( Length > ciMaxPacketSize )
311 Dev->Host->HostDef->SendIN(
312 Dev->Host->Ptr, Dev->Address, Endpoint,
314 Dest, ciMaxPacketSize
317 Length -= ciMaxPacketSize;
320 final = Dev->Host->HostDef->SendIN(
321 Dev->Host->Ptr, Dev->Address, Endpoint,
326 while( Dev->Host->HostDef->IsOpComplete(Dev->Host->Ptr, final) == 0 )
332 char *USB_int_GetDeviceString(tUSBDevice *Dev, int Endpoint, int Index)
334 struct sDescriptor_String str;
335 int src_len, new_len;
338 USB_int_ReadDescriptor(Dev, Endpoint, 3, Index, sizeof(str), &str);
339 // if(str.Length > sizeof(str)) {
341 // Log_Error("USB", "String is %i bytes, which is over prealloc size (%i)",
342 // str.Length, sizeof(str)
345 src_len = (str.Length - 2) / sizeof(str.Data[0]);
347 new_len = _UTF16to8(str.Data, src_len, NULL);
348 ret = malloc( new_len + 1 );
349 _UTF16to8(str.Data, src_len, ret);
354 int _UTF16to8(Uint16 *Input, int InputLen, char *Dest)
357 Uint32 saved_bits = 0;
359 for( int i = 0; i < InputLen; i ++)
362 Uint16 val = Input[i];
363 if( val >= 0xD800 && val <= 0xDBFF )
365 // Multibyte - Leading
366 if(i + 1 > InputLen) {
370 saved_bits = (val - 0xD800) << 10;
371 saved_bits += 0x10000;
375 else if( val >= 0xDC00 && val <= 0xDFFF )
381 saved_bits |= (val - 0xDC00);
388 cp_len = WriteUTF8((Uint8*)Dest, cp);