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--; } } } }
ushort _wTotalLength; // Needed in case Intfc has additional (non-endpt) descriptors #endregion Fields #region Constructors internal unsafe CyUSBInterface(IntPtr handle, byte* DescrData, CyControlEndPoint ctlEndPt) { 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++) { USB_ENDPOINT_DESCRIPTOR* endPtDesc = (USB_ENDPOINT_DESCRIPTOR*)desc; _wTotalLength += endPtDesc->bLength; 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; } desc += endPtDesc->bLength; } 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--; } } } }
// Opens a handle to the devTH device attached the CYUSB3.SYS driver internal override bool Open(byte dev) { // If this CCyUSBDevice object already has the driver open, close it. if (_hDevice != CyConst.INVALID_HANDLE) Close(); _devices = DeviceCount; if (_devices == 0) return false; if (dev > (_devices - 1)) return false; _path = PInvoke.GetDevicePath(_drvGuid, dev); _hDevice = PInvoke.GetDeviceHandle(_path, true); if (_hDevice == CyConst.INVALID_HANDLE) return false; _devNum = dev; GetDevDescriptor(); SetStringDescrLanguage(); _manufacturer = GetString(_usbDeviceDescriptor.iManufacturer); _product = GetString(_usbDeviceDescriptor.iProduct); _serialNumber = GetString(_usbDeviceDescriptor.iSerialNumber); // Get BOS descriptor if ((_bcdUSB & CyConst.bcdUSBJJMask) == CyConst.USB30MajorVer) { GetBosDescriptor(); // USB3.0 specific descriptor try { USBBos = new CyUSBBOS(_hDevice, _usb30BosDescriptors); } catch (Exception exc) { //Just to remove warning exc.ToString(); _nullEndpointFlag = true; MessageBox.Show("Please correct the firmware BOS descriptor table", "Invalid BOS Descriptor"); Close(); // Close the device handle ,as the device configuration is incorrect. return false; } } GetUSBAddress(); GetDeviceName(); GetFriendlyName(); GetDriverVer(); GetUSBDIVer(); GetSpeed(); // Search the registry for this device - This must follow GetFriendlyName GetDriverName(); // Create the Control Endpoint (EPT 0) ControlEndPt = new CyControlEndPoint(_hDevice, _maxPacketSize); // Gets and parses the config (including interface and endpoint) descriptors from the device for (int i = 0; i < _configs; i++) { GetCfgDescriptor(i); try { if ((_bcdUSB & CyConst.bcdUSBJJMask) == CyConst.USB20MajorVer) USBCfgs[i] = new CyUSBConfig(_hDevice, _usbConfigDescriptors[i], ControlEndPt); else { byte usb30Dummy = 1; // it's dummy variable to call the usb3.0 specific constructor USBCfgs[i] = new CyUSBConfig(_hDevice, _usbConfigDescriptors[i], ControlEndPt, usb30Dummy); } } catch (Exception exc) { //Just to remove warning exc.ToString(); _nullEndpointFlag = true; MessageBox.Show("Please correct the firmware descriptor table", "Invalid Device Configuration"); Close(); return false; } } // We succeeded in openning a handle to the device. But, the device // is not returning descriptors properly. We don't call Close( ) because // we want to leave the hDevice intact, giving the user the opportunity // to call the Reset( ) method if ((USBCfgs[0] == null) || (USBCfgs[0].Interfaces[0] == null)) return false; if (!_nullEndpointFlag) { try { // This property assignment sets values for ConfigVal, ConfigAttrib, MaxPower, etc. Config = 0; } catch (Exception exc) { //Just to remove warning exc.ToString(); _nullEndpointFlag = true; MessageBox.Show("Please Check the Device Configuration and try again.", "Invalid Firmware"); } } if (_nullEndpointFlag) return false; return true; }
internal unsafe 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 */ }