changeset 61:2b24666cd759

Fixed errors in Windows USB enumeration and LibUsbDotNet configuration descriptor code
author Ivo Smits <Ivo@UCIS.nl>
date Sun, 13 Oct 2013 00:48:28 +0200
parents 3424fa5a12c9
children edc41c861d96
files USBLib/Communication/LibUsbDotNet.cs USBLib/Windows/USB/UsbDevice.cs
diffstat 2 files changed, 10 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/USBLib/Communication/LibUsbDotNet.cs	Sat Oct 12 16:35:24 2013 +0200
+++ b/USBLib/Communication/LibUsbDotNet.cs	Sun Oct 13 00:48:28 2013 +0200
@@ -108,16 +108,24 @@
 		public IList<UsbConfigInfo> Configs {
 			get {
 				List<UsbConfigInfo> rtnConfigs = new List<UsbConfigInfo>();
-				byte[] cfgBuffer = new byte[UsbConstants.MAX_CONFIG_SIZE];
 				int iConfigs = Info.Descriptor.ConfigurationCount;
 				for (int iConfig = 0; iConfig < iConfigs; iConfig++) {
 					int iBytesTransmitted;
+					byte[] cfgBuffer = new byte[9];
 					if (!GetDescriptor((byte)UsbDescriptorType.Configuration, (byte)iConfig, 0, cfgBuffer, cfgBuffer.Length, out iBytesTransmitted))
 						throw new Exception("Could not read configuration descriptor");
 					if (iBytesTransmitted < UsbConfigDescriptor.Size || cfgBuffer[1] != (byte)UsbDescriptorType.Configuration)
 						throw new Exception("GetDeviceConfigs: USB config descriptor is invalid.");
 					UsbConfigDescriptor configDescriptor = new UsbConfigDescriptor();
 					Helper.BytesToObject(cfgBuffer, 0, Math.Min(UsbConfigDescriptor.Size, cfgBuffer[0]), configDescriptor);
+					if (configDescriptor.TotalLength > cfgBuffer.Length) {
+						cfgBuffer = new Byte[configDescriptor.TotalLength];
+						if (!GetDescriptor((byte)UsbDescriptorType.Configuration, (byte)iConfig, 0, cfgBuffer, cfgBuffer.Length, out iBytesTransmitted))
+							throw new Exception("Could not read configuration descriptor");
+						if (iBytesTransmitted < UsbConfigDescriptor.Size || cfgBuffer[1] != (byte)UsbDescriptorType.Configuration)
+							throw new Exception("GetDeviceConfigs: USB config descriptor is invalid.");
+						Helper.BytesToObject(cfgBuffer, 0, Math.Min(UsbConfigDescriptor.Size, cfgBuffer[0]), configDescriptor);
+					}
 					if (configDescriptor.TotalLength != iBytesTransmitted) throw new Exception("GetDeviceConfigs: USB config descriptor length doesn't match the length received.");
 					List<byte[]> rawDescriptorList = new List<byte[]>();
 					int iRawLengthPosition = configDescriptor.Length;
--- a/USBLib/Windows/USB/UsbDevice.cs	Sat Oct 12 16:35:24 2013 +0200
+++ b/USBLib/Windows/USB/UsbDevice.cs	Sun Oct 13 00:48:28 2013 +0200
@@ -142,6 +142,7 @@
 			}
 			isHostController = false;
 			DeviceNode parent = node.GetParent();
+			if (parent == null) return null;
 			Boolean isHostControllerA;
 			UsbDevice usbdev = GetUsbDevice(parent, out isHostControllerA);
 			if (isHostControllerA) return usbdev;