3 * - By John Hodge (thePowersGang)
6 * - USB Device Initialisation
13 #include "usb_proto.h"
14 #include "usb_lowlevel.h"
16 #define DUMP_DESCRIPTORS 0
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);
26 void USB_DeviceConnected(tUSBHub *Hub, int Port)
29 tUSBDevice *dev = &tmpdev;
30 if( Port >= Hub->nPorts ) return ;
31 if( Hub->Devices[Port] ) return ;
33 ENTER("pHub iPort", Hub, Port);
35 // Device should be in 'Default' state
39 dev->Host = Hub->Interface->Dev->Host;
42 // 1. Assign an address
43 dev->Address = USB_int_AllocateAddress(dev->Host);
44 if(dev->Address == 0) {
45 Log_Error("USB", "No addresses avaliable on host %p", dev->Host);
50 USB_int_SendSetupSetAddress(dev->Host, dev->Address);
51 LOG("Assigned address %i", dev->Address);
53 // 2. Get device information
55 struct sDescriptor_Device desc;
56 LOG("Getting device descriptor");
57 // Endpoint 0, Desc Type 1, Index 0
58 USB_int_ReadDescriptor(dev, 0, 1, 0, sizeof(desc), &desc);
61 LOG("Device Descriptor = {");
62 LOG(" .Length = %i", desc.Length);
63 LOG(" .Type = %i", desc.Type);
64 LOG(" .USBVersion = 0x%04x", desc.USBVersion);
65 LOG(" .DeviceClass = 0x%02x", desc.DeviceClass);
66 LOG(" .DeviceSubClass = 0x%02x", desc.DeviceSubClass);
67 LOG(" .DeviceProtocol = 0x%02x", desc.DeviceProtocol);
68 LOG(" .MaxPacketSize = 0x%02x", desc.MaxPacketSize);
69 LOG(" .VendorID = 0x%04x", desc.VendorID);
70 LOG(" .ProductID = 0x%04x", desc.ProductID);
71 LOG(" .DeviceID = 0x%04x", desc.DeviceID);
72 LOG(" .ManufacturerStr = Str %i", desc.ManufacturerStr);
73 LOG(" .ProductStr = Str %i", desc.ProductStr);
74 LOG(" .SerialNumberStr = Str %i", desc.SerialNumberStr);
75 LOG(" .NumConfigurations = %i", desc.SerialNumberStr);
79 if( desc.ManufacturerStr )
81 char *tmp = USB_int_GetDeviceString(dev, 0, desc.ManufacturerStr);
83 LOG("ManufacturerStr = '%s'", tmp);
89 char *tmp = USB_int_GetDeviceString(dev, 0, desc.ProductStr);
91 LOG("ProductStr = '%s'", tmp);
95 if( desc.SerialNumberStr )
97 char *tmp = USB_int_GetDeviceString(dev, 0, desc.SerialNumberStr);
99 LOG("SerialNumbertStr = '%s'", tmp);
107 // TODO: Support alternate configurations
109 // 3. Get configurations
110 for( int i = 0; i < 1; i ++ )
112 struct sDescriptor_Configuration desc;
117 USB_int_ReadDescriptor(dev, 0, 2, i, sizeof(desc), &desc);
118 // TODO: Check return length? (Do we get a length?)
120 LOG("Configuration Descriptor %i = {", i);
121 LOG(" .Length = %i", desc.Length);
122 LOG(" .Type = %i", desc.Type);
123 LOG(" .TotalLength = 0x%x", LittleEndian16(desc.TotalLength));
124 LOG(" .NumInterfaces = %i", desc.NumInterfaces);
125 LOG(" .ConfigurationValue = %i", desc.ConfigurationValue);
126 LOG(" .ConfigurationStr = %i", desc.ConfigurationStr);
127 LOG(" .AttributesBmp = 0b%b", desc.AttributesBmp);
128 LOG(" .MaxPower = %i (*2mA)", desc.MaxPower);
130 if( desc.ConfigurationStr ) {
131 char *tmp = USB_int_GetDeviceString(dev, 0, desc.ConfigurationStr);
132 LOG("ConfigurationStr = '%s'", tmp);
137 if( desc.NumInterfaces == 0 ) {
138 Log_Notice("USB", "Device does not have any interfaces");
142 // TODO: Split here and allow some method of selection
144 // Allocate device now that we have the configuration
145 dev = malloc(sizeof(tUSBDevice) + desc.NumInterfaces * sizeof(void*));
146 memcpy(dev, &tmpdev, sizeof(tUSBDevice));
147 dev->nInterfaces = desc.NumInterfaces;
149 // Allocate a temp buffer for config info
150 total_length = LittleEndian16(desc.TotalLength);
151 full_buf = malloc( total_length );
152 USB_int_ReadDescriptor(dev, 0, 2, i, total_length, full_buf);
154 ptr_ofs += desc.Length;
157 for( int j = 0; ptr_ofs < total_length && j < desc.NumInterfaces; j ++ )
159 struct sDescriptor_Interface *iface;
160 tUSBInterface *dev_if;
161 size_t iface_base_ofs;
163 iface = (void*)(full_buf + ptr_ofs);
164 if( iface->Length == 0 ) {
165 Log_Warning("USB", "Bad USB descriptor (length = 0)");
168 ptr_ofs += iface->Length;
169 if( ptr_ofs > total_length ) {
173 iface_base_ofs = ptr_ofs;
175 if( iface->Type != 4 ) {
176 LOG("Not an interface (type = %i)", iface->Type);
177 j --; // Counteract j++ in loop
182 LOG("Interface %i/%i = {", i, j);
183 LOG(" .InterfaceNum = %i", iface->InterfaceNum);
184 LOG(" .NumEndpoints = %i", iface->NumEndpoints);
185 LOG(" .InterfaceClass = 0x%x", iface->InterfaceClass);
186 LOG(" .InterfaceSubClass = 0x%x", iface->InterfaceSubClass);
187 LOG(" .InterfaceProcol = 0x%x", iface->InterfaceProtocol);
188 if( iface->InterfaceStr ) {
189 char *tmp = USB_int_GetDeviceString(dev, 0, iface->InterfaceStr);
190 LOG(" .InterfaceStr = %i '%s'", iface->InterfaceStr, tmp);
196 dev_if = malloc(sizeof(tUSBInterface) + iface->NumEndpoints*sizeof(dev_if->Endpoints[0]));
198 dev_if->Driver = NULL;
200 dev_if->nEndpoints = iface->NumEndpoints;
201 dev->Interfaces[j] = dev_if;
203 // Copy interface data
204 for( int k = 0; ptr_ofs < total_length && k < iface->NumEndpoints; k ++ )
206 struct sDescriptor_Endpoint *endpt;
208 endpt = (void*)(full_buf + ptr_ofs);
209 ptr_ofs += endpt->Length;
210 if( ptr_ofs > total_length ) {
216 if( endpt->Type != 5 ) {
218 LOG("Not endpoint, Type = %i", endpt->Type);
223 LOG("Endpoint %i/%i/%i = {", i, j, k);
224 LOG(" .Address = 0x%2x", endpt->Address);
225 LOG(" .Attributes = 0b%8b", endpt->Attributes);
226 LOG(" .MaxPacketSize = %i", LittleEndian16(endpt->MaxPacketSize));
227 LOG(" .PollingInterval = %i", endpt->PollingInterval);
230 dev_if->Endpoints[k].Next = NULL;
231 dev_if->Endpoints[k].Interface = dev_if;
232 dev_if->Endpoints[k].EndpointIdx = k;
233 dev_if->Endpoints[k].EndpointNum = endpt->Address & 0x7F;
234 dev_if->Endpoints[k].PollingPeriod = endpt->PollingInterval;
235 dev_if->Endpoints[k].MaxPacketSize = LittleEndian16(endpt->MaxPacketSize);
236 dev_if->Endpoints[k].Type = endpt->Attributes | (endpt->Address & 0x80);
237 dev_if->Endpoints[k].PollingAtoms = 0;
238 dev_if->Endpoints[k].InputData = NULL;
242 dev_if->Driver = USB_int_FindDriverByClass(
243 ((int)iface->InterfaceClass << 16)
244 |((int)iface->InterfaceSubClass << 8)
245 |((int)iface->InterfaceProtocol << 0)
247 if(!dev_if->Driver) {
248 Log_Notice("USB", "No driver for Class %02x:%02x:%02x",
249 iface->InterfaceClass, iface->InterfaceSubClass, iface->InterfaceProtocol
253 LOG("Driver '%s' in use", dev_if->Driver->Name);
254 dev_if->Driver->Connected(
256 full_buf + iface_base_ofs, ptr_ofs - iface_base_ofs
268 void USB_DeviceDisconnected(tUSBHub *Hub, int Port)
273 void *USB_GetDeviceDataPtr(tUSBInterface *Dev) { return Dev->Data; }
274 void USB_SetDeviceDataPtr(tUSBInterface *Dev, void *Ptr) { Dev->Data = Ptr; }
276 int USB_int_AllocateAddress(tUSBHost *Host)
279 for( i = 1; i < 128; i ++ )
281 if(Host->AddressBitmap[i/8] & (1 << (i%8)))
283 Host->AddressBitmap[i/8] |= 1 << (i%8);
289 void USB_int_DeallocateAddress(tUSBHost *Host, int Address)
291 Host->AddressBitmap[Address/8] &= ~(1 << (Address%8));