3 * - By John Hodge (thePowersGang)
6 * - USB Device Initialisation
13 #include "usb_proto.h"
14 #include "usb_lowlevel.h"
16 #define DUMP_DESCRIPTORS 1
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 void USB_int_DeallocateAddress(tUSBHost *Host, int Address);
27 void USB_DeviceConnected(tUSBHub *Hub, int Port)
30 tUSBDevice *dev = &tmpdev;
31 if( Port >= Hub->nPorts ) return ;
32 if( Hub->Devices[Port] ) return ;
34 ENTER("pHub iPort", Hub, Port);
36 // Device should be in 'Default' state
40 dev->Host = Hub->Interface->Dev->Host;
43 // 1. Assign an address
44 dev->Address = USB_int_AllocateAddress(dev->Host);
45 if(dev->Address == 0) {
46 Log_Error("USB", "No addresses avaliable on host %p", dev->Host);
51 USB_int_SendSetupSetAddress(dev->Host, dev->Address);
52 LOG("Assigned address %i", dev->Address);
54 // 2. Get device information
56 struct sDescriptor_Device desc;
58 LOG("Getting device descriptor");
59 // Endpoint 0, Desc Type 1, Index 0
60 USB_int_ReadDescriptor(dev, 0, 1, 0, sizeof(desc), &desc);
62 if( desc.Length < sizeof(desc) ) {
63 Log_Error("USB", "Device descriptor undersized (%i<%i)", desc.Length, sizeof(desc));
64 USB_int_DeallocateAddress(dev->Host, dev->Address);
68 if( desc.Type != 1 ) {
69 Log_Error("USB", "Device descriptor type invalid (%i!=1)", desc.Type);
70 USB_int_DeallocateAddress(dev->Host, dev->Address);
76 LOG("Device Descriptor = {");
77 LOG(" .Length = %i", desc.Length);
78 LOG(" .Type = %i", desc.Type);
79 LOG(" .USBVersion = 0x%04x", LittleEndian16(desc.USBVersion));
80 LOG(" .DeviceClass = 0x%02x", desc.DeviceClass);
81 LOG(" .DeviceSubClass = 0x%02x", desc.DeviceSubClass);
82 LOG(" .DeviceProtocol = 0x%02x", desc.DeviceProtocol);
83 LOG(" .MaxPacketSize = 0x%02x", desc.MaxPacketSize);
84 LOG(" .VendorID = 0x%04x", LittleEndian16(desc.VendorID));
85 LOG(" .ProductID = 0x%04x", LittleEndian16(desc.ProductID));
86 LOG(" .DeviceID = 0x%04x", LittleEndian16(desc.DeviceID));
87 LOG(" .ManufacturerStr = Str %i", desc.ManufacturerStr);
88 LOG(" .ProductStr = Str %i", desc.ProductStr);
89 LOG(" .SerialNumberStr = Str %i", desc.SerialNumberStr);
90 LOG(" .NumConfigurations = %i", desc.NumConfigurations);
94 if( desc.ManufacturerStr )
96 char *tmp = USB_int_GetDeviceString(dev, 0, desc.ManufacturerStr);
98 LOG("ManufacturerStr = '%s'", tmp);
102 if( desc.ProductStr )
104 char *tmp = USB_int_GetDeviceString(dev, 0, desc.ProductStr);
106 LOG("ProductStr = '%s'", tmp);
110 if( desc.SerialNumberStr )
112 char *tmp = USB_int_GetDeviceString(dev, 0, desc.SerialNumberStr);
114 LOG("SerialNumbertStr = '%s'", tmp);
121 memcpy(&dev->DevDesc, &desc, sizeof(desc));
124 // 3. Get configurations
125 // TODO: Support alternate configurations
126 for( int i = 0; i < 1; i ++ )
128 struct sDescriptor_Configuration desc;
133 USB_int_ReadDescriptor(dev, 0, 2, i, sizeof(desc), &desc);
134 if( desc.Length < sizeof(desc) ) {
137 if( desc.Type != 2 ) {
140 // TODO: Check return length? (Do we get a length?)
142 LOG("Configuration Descriptor %i = {", i);
143 LOG(" .Length = %i", desc.Length);
144 LOG(" .Type = %i", desc.Type);
145 LOG(" .TotalLength = 0x%x", LittleEndian16(desc.TotalLength));
146 LOG(" .NumInterfaces = %i", desc.NumInterfaces);
147 LOG(" .ConfigurationValue = %i", desc.ConfigurationValue);
148 LOG(" .ConfigurationStr = %i", desc.ConfigurationStr);
149 LOG(" .AttributesBmp = 0b%b", desc.AttributesBmp);
150 LOG(" .MaxPower = %i (*2mA)", desc.MaxPower);
152 if( desc.ConfigurationStr ) {
153 char *tmp = USB_int_GetDeviceString(dev, 0, desc.ConfigurationStr);
154 LOG("ConfigurationStr = '%s'", tmp);
159 if( desc.NumInterfaces == 0 ) {
160 Log_Notice("USB", "Device does not have any interfaces");
164 // TODO: Split here and allow some method of selection
166 // Allocate device now that we have the configuration
167 dev = malloc(sizeof(tUSBDevice) + desc.NumInterfaces * sizeof(void*));
168 memcpy(dev, &tmpdev, sizeof(tUSBDevice));
169 dev->nInterfaces = desc.NumInterfaces;
171 // Allocate a temp buffer for config info
172 total_length = LittleEndian16(desc.TotalLength);
173 full_buf = malloc( total_length );
174 USB_int_ReadDescriptor(dev, 0, 2, i, total_length, full_buf);
175 ptr_ofs += desc.Length;
179 for( int j = 0; ptr_ofs < total_length && j < desc.NumInterfaces; j ++ )
181 struct sDescriptor_Interface *iface;
182 tUSBInterface *dev_if;
183 size_t iface_base_ofs;
185 iface = (void*)(full_buf + ptr_ofs);
186 if( iface->Length == 0 ) {
187 Log_Warning("USB", "Bad USB descriptor (length = 0)");
190 ptr_ofs += iface->Length;
191 if( ptr_ofs > total_length ) {
195 iface_base_ofs = ptr_ofs;
197 if( iface->Type != 4 ) {
198 LOG("Not an interface (type = %i)", iface->Type);
199 j --; // Counteract j++ in loop
204 LOG("Interface %i/%i = {", i, j);
205 LOG(" .InterfaceNum = %i", iface->InterfaceNum);
206 LOG(" .NumEndpoints = %i", iface->NumEndpoints);
207 LOG(" .InterfaceClass = 0x%x", iface->InterfaceClass);
208 LOG(" .InterfaceSubClass = 0x%x", iface->InterfaceSubClass);
209 LOG(" .InterfaceProcol = 0x%x", iface->InterfaceProtocol);
210 if( iface->InterfaceStr ) {
211 char *tmp = USB_int_GetDeviceString(dev, 0, iface->InterfaceStr);
212 LOG(" .InterfaceStr = %i '%s'", iface->InterfaceStr, tmp);
219 sizeof(tUSBInterface)
220 + iface->NumEndpoints*sizeof(dev_if->Endpoints[0])
223 dev_if->Driver = NULL;
225 dev_if->nEndpoints = iface->NumEndpoints;
226 memcpy(&dev_if->IfaceDesc, iface, sizeof(*iface));
227 dev->Interfaces[j] = dev_if;
229 // Copy interface data
230 for( int k = 0; ptr_ofs < total_length && k < iface->NumEndpoints; k ++ )
232 struct sDescriptor_Endpoint *endpt;
234 endpt = (void*)(full_buf + ptr_ofs);
235 ptr_ofs += endpt->Length;
236 if( ptr_ofs > total_length ) {
242 if( endpt->Type != 5 ) {
244 LOG("Not endpoint, Type = %i", endpt->Type);
249 LOG("Endpoint %i/%i/%i = {", i, j, k);
250 LOG(" .Address = 0x%2x", endpt->Address);
251 LOG(" .Attributes = 0b%8b", endpt->Attributes);
252 LOG(" .MaxPacketSize = %i", LittleEndian16(endpt->MaxPacketSize));
253 LOG(" .PollingInterval = %i", endpt->PollingInterval);
256 dev_if->Endpoints[k].Next = NULL;
257 dev_if->Endpoints[k].Interface = dev_if;
258 dev_if->Endpoints[k].EndpointIdx = k;
259 dev_if->Endpoints[k].EndpointNum = endpt->Address & 0x7F;
260 dev_if->Endpoints[k].PollingPeriod = endpt->PollingInterval;
261 dev_if->Endpoints[k].MaxPacketSize = LittleEndian16(endpt->MaxPacketSize);
262 dev_if->Endpoints[k].Type = endpt->Attributes | (endpt->Address & 0x80);
263 dev_if->Endpoints[k].PollingAtoms = 0;
264 dev_if->Endpoints[k].InputData = NULL;
268 dev_if->Driver = USB_int_FindDriverByClass(
269 ((int)iface->InterfaceClass << 16)
270 |((int)iface->InterfaceSubClass << 8)
271 |((int)iface->InterfaceProtocol << 0)
273 if(!dev_if->Driver) {
274 Log_Notice("USB", "No driver for Class %02x:%02x:%02x",
275 iface->InterfaceClass, iface->InterfaceSubClass, iface->InterfaceProtocol
279 LOG("Driver '%s' in use", dev_if->Driver->Name);
280 dev_if->Driver->Connected(
282 full_buf + iface_base_ofs, ptr_ofs - iface_base_ofs
294 void USB_DeviceDisconnected(tUSBHub *Hub, int Port)
299 void *USB_GetDeviceDataPtr(tUSBInterface *Dev) { return Dev->Data; }
300 void USB_SetDeviceDataPtr(tUSBInterface *Dev, void *Ptr) { Dev->Data = Ptr; }
302 int USB_int_AllocateAddress(tUSBHost *Host)
305 for( i = 1; i < 128; i ++ )
307 if(Host->AddressBitmap[i/8] & (1 << (i%8)))
309 Host->AddressBitmap[i/8] |= 1 << (i%8);
315 void USB_int_DeallocateAddress(tUSBHost *Host, int Address)
317 Host->AddressBitmap[Address/8] &= ~(1 << (Address%8));