unsafe internal CyUSBConfig(IntPtr handle, byte[] DescrData, CyControlEndPoint ctlEndPt) {// This contructore is to initialize usb2.0 device fixed(byte *buf = DescrData) { USB_CONFIGURATION_DESCRIPTOR *ConfigDescr = (USB_CONFIGURATION_DESCRIPTOR *)buf; _bLength = ConfigDescr->bLength; _bDescriptorType = ConfigDescr->bDescriptorType; _wTotalLength = ConfigDescr->wTotalLength; _bNumInterfaces = ConfigDescr->bNumInterfaces; _AltInterfaces = 0; _bConfigurationValue = ConfigDescr->bConfigurationValue; _iConfiguration = ConfigDescr->iConfiguration; _bmAttributes = ConfigDescr->bmAttributes; _MaxPower = ConfigDescr->MaxPower; int tLen = ConfigDescr->wTotalLength; byte *desc = (byte *)(buf + ConfigDescr->bLength); int bytesConsumed = ConfigDescr->bLength; Interfaces = new CyUSBInterface[CyConst.MAX_INTERFACES]; int i = 0; do { USB_INTERFACE_DESCRIPTOR *interfaceDesc = (USB_INTERFACE_DESCRIPTOR *)desc; if (interfaceDesc->bDescriptorType == CyConst.USB_INTERFACE_DESCRIPTOR_TYPE) { Interfaces[i] = new CyUSBInterface(handle, desc, ctlEndPt); i++; _AltInterfaces++; // Actually the total number of interfaces for the config bytesConsumed += Interfaces[i - 1].wTotalLength; } else { // Unexpected descriptor type // Just skip it and go on - could have thrown an exception instead // since this indicates that the descriptor structure is invalid. bytesConsumed += interfaceDesc->bLength; } desc = (byte *)(buf + bytesConsumed); } while ((bytesConsumed < tLen) && (i < CyConst.MAX_INTERFACES)); // Count the alt interfaces for each interface number for (i = 0; i < _AltInterfaces; i++) { Interfaces[i]._bAltSettings = 0; for (int j = 0; j < AltInterfaces; j++) // Walk the list looking for identical bInterfaceNumbers { if (Interfaces[i].bInterfaceNumber == Interfaces[j].bInterfaceNumber) { Interfaces[i]._bAltSettings++; } } } // Create the Interface Container (this is done only for Tree view purpose). IntfcContainer = new CyUSBInterfaceContainer[bNumInterfaces]; Dictionary <int, bool> altDict = new Dictionary <int, bool>(); int intfcCount = 0; for (i = 0; i < _AltInterfaces; i++) { if (altDict.ContainsKey(Interfaces[i].bInterfaceNumber) == false) { int altIntfcCount = 0; IntfcContainer[intfcCount] = new CyUSBInterfaceContainer(Interfaces[i].bInterfaceNumber, Interfaces[i].bAltSettings); for (int j = i; j < AltInterfaces; j++) { if (Interfaces[i].bInterfaceNumber == Interfaces[j].bInterfaceNumber) { IntfcContainer[intfcCount].Interfaces[altIntfcCount] = Interfaces[j]; altIntfcCount++; } } intfcCount++; altDict.Add(Interfaces[i].bInterfaceNumber, true); } } } /* end of fixed loop */ }
unsafe internal CyUSBInterface(IntPtr handle, byte *DescrData, CyControlEndPoint ctlEndPt, byte usb30dummy) { USB_INTERFACE_DESCRIPTOR *pIntfcDescriptor = (USB_INTERFACE_DESCRIPTOR *)DescrData; _bLength = pIntfcDescriptor->bLength; _bDescriptorType = pIntfcDescriptor->bDescriptorType; _bInterfaceNumber = pIntfcDescriptor->bInterfaceNumber; _bAlternateSetting = pIntfcDescriptor->bAlternateSetting; _bNumEndpoints = pIntfcDescriptor->bNumEndpoints; _bInterfaceClass = pIntfcDescriptor->bInterfaceClass; _bInterfaceSubClass = pIntfcDescriptor->bInterfaceSubClass; _bInterfaceProtocol = pIntfcDescriptor->bInterfaceProtocol; _iInterface = pIntfcDescriptor->iInterface; _bAltSettings = 0; _wTotalLength = bLength; byte *desc = (byte *)(DescrData + pIntfcDescriptor->bLength); int i; int unexpected = 0; EndPoints = new CyUSBEndPoint[bNumEndpoints + 1]; EndPoints[0] = ctlEndPt; for (i = 1; i <= bNumEndpoints; i++) { bool bSSDec = false; USB_ENDPOINT_DESCRIPTOR *endPtDesc = (USB_ENDPOINT_DESCRIPTOR *)desc; desc += endPtDesc->bLength; USB_SUPERSPEED_ENDPOINT_COMPANION_DESCRIPTOR *ssendPtDesc = (USB_SUPERSPEED_ENDPOINT_COMPANION_DESCRIPTOR *)desc; _wTotalLength += endPtDesc->bLength; if (ssendPtDesc != null) { bSSDec = (ssendPtDesc->bDescriptorType == CyConst.USB_SUPERSPEED_ENDPOINT_COMPANION); } if ((endPtDesc->bDescriptorType == CyConst.USB_ENDPOINT_DESCRIPTOR_TYPE) && bSSDec) { switch (endPtDesc->bmAttributes) { case 0: EndPoints[i] = ctlEndPt; break; case 1: EndPoints[i] = new CyIsocEndPoint(handle, endPtDesc, ssendPtDesc); break; case 2: EndPoints[i] = new CyBulkEndPoint(handle, endPtDesc, ssendPtDesc); break; case 3: EndPoints[i] = new CyInterruptEndPoint(handle, endPtDesc, ssendPtDesc); break; } _wTotalLength += ssendPtDesc->bLength; desc += ssendPtDesc->bLength; } else if ((endPtDesc->bDescriptorType == CyConst.USB_ENDPOINT_DESCRIPTOR_TYPE)) { switch (endPtDesc->bmAttributes) { case 0: EndPoints[i] = ctlEndPt; break; case 1: EndPoints[i] = new CyIsocEndPoint(handle, endPtDesc); break; case 2: EndPoints[i] = new CyBulkEndPoint(handle, endPtDesc); break; case 3: EndPoints[i] = new CyInterruptEndPoint(handle, endPtDesc); break; } } else { unexpected++; if (unexpected < 12) { // Sanity check - prevent infinite loop // This may have been a class-specific descriptor (like HID). Skip it. desc += endPtDesc->bLength; // Stay in the loop, grabbing the next descriptor i--; } } } }