public static unsafe UsbInterfaceInfo FromUsbInterfaceDescriptor(LibUsb.UsbDevice device, InterfaceDescriptor descriptor) { Debug.Assert(descriptor.DescriptorType == (int)DescriptorType.Interface, "A config descriptor was expected"); UsbInterfaceInfo value = new UsbInterfaceInfo(); value.AlternateSetting = descriptor.AlternateSetting; var endpoints = (EndpointDescriptor *)descriptor.Endpoint; for (int i = 0; i < descriptor.NumEndpoints; i++) { if (endpoints[i].DescriptorType != 0) { value.endpoints.Add(UsbEndpointInfo.FromUsbEndpointDescriptor(endpoints[i])); } } value.RawDescriptors = new byte[descriptor.ExtraLength]; if (descriptor.ExtraLength > 0) { Span <byte> extra = new Span <byte>(descriptor.Extra, descriptor.ExtraLength); extra.CopyTo(value.RawDescriptors); } value.Interface = device.GetStringDescriptor(descriptor.Interface, failSilently: true); value.Class = (ClassCode)descriptor.InterfaceClass; value.Number = descriptor.InterfaceNumber; value.Protocol = descriptor.InterfaceProtocol; value.SubClass = descriptor.InterfaceSubClass; return(value); }
internal static unsafe UsbConfigInfo FromUsbConfigDescriptor(global::LibUsbDotNet.LibUsb.UsbDevice device, ConfigDescriptor descriptor) { Debug.Assert(descriptor.DescriptorType == (int)DescriptorType.Config, "A config descriptor was expected"); UsbConfigInfo value = new UsbConfigInfo(); value.Attributes = descriptor.Attributes; value.Configuration = device.GetStringDescriptor(descriptor.Configuration, failSilently: true); value.ConfigurationValue = descriptor.ConfigurationValue; value.RawDescriptors = new byte[descriptor.ExtraLength]; if (descriptor.ExtraLength > 0) { Span <byte> extra = new Span <byte>(descriptor.Extra, descriptor.ExtraLength); extra.CopyTo(value.RawDescriptors); } var interfaces = (Interface *)descriptor.Interface; for (int i = 0; i < descriptor.NumInterfaces; i++) { var values = UsbInterfaceInfo.FromUsbInterface(device, interfaces[i]); value.interfaces.AddRange(values); } value.MaxPower = descriptor.MaxPower; return(value); }
internal UsbConfigInfo(MonoUsbDevice usbDevice, MonoUsbConfigDescriptor configDescriptor) { mUsbDevice = usbDevice; mUsbConfigDescriptor = new UsbConfigDescriptor(configDescriptor); List <MonoUsbInterface> monoUSBInterfaces = configDescriptor.InterfaceList; foreach (MonoUsbInterface usbInterface in monoUSBInterfaces) { List <MonoUsbAltInterfaceDescriptor> monoUSBAltInterfaces = usbInterface.AltInterfaceList; foreach (MonoUsbAltInterfaceDescriptor monoUSBAltInterface in monoUSBAltInterfaces) { UsbInterfaceInfo usbInterfaceInfo = new UsbInterfaceInfo(mUsbDevice, monoUSBAltInterface); mInterfaceList.Add(usbInterfaceInfo); } } }
internal UsbConfigInfo(UsbDevice usbDevice, UsbConfigDescriptor descriptor, ref List <byte[]> rawDescriptors) { mUsbDevice = usbDevice; mUsbConfigDescriptor = descriptor; mRawDescriptors = rawDescriptors; UsbInterfaceInfo currentInterface = null; for (int iRawDescriptor = 0; iRawDescriptor < rawDescriptors.Count; iRawDescriptor++) { byte[] bytesRawDescriptor = rawDescriptors[iRawDescriptor]; switch (bytesRawDescriptor[1]) { case (byte)DescriptorType.Interface: currentInterface = new UsbInterfaceInfo(usbDevice, bytesRawDescriptor); mRawDescriptors.RemoveAt(iRawDescriptor); mInterfaceList.Add(currentInterface); iRawDescriptor--; break; case (byte)DescriptorType.Endpoint: if (currentInterface == null) { throw new UsbException(this, "Recieved and endpoint descriptor before receiving an interface descriptor."); } currentInterface.mEndpointInfo.Add(new UsbEndpointInfo(bytesRawDescriptor)); mRawDescriptors.RemoveAt(iRawDescriptor); iRawDescriptor--; break; default: if (currentInterface != null) { currentInterface.mRawDescriptors.Add(bytesRawDescriptor); mRawDescriptors.RemoveAt(iRawDescriptor); iRawDescriptor--; } break; } } }
internal UsbConfigInfo(UsbDevice usbDevice, UsbConfigDescriptor descriptor, ref List<byte[]> rawDescriptors) { mUsbDevice = usbDevice; mUsbConfigDescriptor = descriptor; mRawDescriptors = rawDescriptors; UsbInterfaceInfo currentInterface = null; for (int iRawDescriptor = 0; iRawDescriptor < rawDescriptors.Count; iRawDescriptor++) { byte[] bytesRawDescriptor = rawDescriptors[iRawDescriptor]; switch (bytesRawDescriptor[1]) { case (byte) DescriptorType.Interface: currentInterface = new UsbInterfaceInfo(usbDevice, bytesRawDescriptor); mRawDescriptors.RemoveAt(iRawDescriptor); mInterfaceList.Add(currentInterface); iRawDescriptor--; break; case (byte) DescriptorType.Endpoint: if (currentInterface == null) throw new UsbException(this, "Recieved and endpoint descriptor before receiving an interface descriptor."); currentInterface.mEndpointInfo.Add(new UsbEndpointInfo(bytesRawDescriptor)); mRawDescriptors.RemoveAt(iRawDescriptor); iRawDescriptor--; break; default: if (currentInterface != null) { currentInterface.mRawDescriptors.Add(bytesRawDescriptor); mRawDescriptors.RemoveAt(iRawDescriptor); iRawDescriptor--; } break; } } }
private static UsbInterfaceInfo usb_find_interface(UsbConfigInfo config_descriptor, int interface_number, out UsbInterfaceInfo first_interface) { first_interface = null; if (ReferenceEquals(config_descriptor, null)) return null; ReadOnlyCollection<UsbInterfaceInfo> interfaces = config_descriptor.InterfaceInfoList; for (int intfIndex = 0; intfIndex < interfaces.Count; intfIndex++) { if (ReferenceEquals(first_interface, null)) first_interface = interfaces[intfIndex]; if (interfaces[intfIndex].Descriptor.InterfaceID == interface_number) { return interfaces[intfIndex]; } } return null; }
/// <summary> /// Looks up endpoint/interface information in a configuration. /// </summary> /// <param name="currentConfigInfo">The config to seach.</param> /// <param name="endpointAddress">The endpoint address to look for.</param> /// <param name="usbInterfaceInfo">On success, the <see cref="UsbInterfaceInfo"/> class for this endpoint.</param> /// <param name="usbEndpointInfo">On success, the <see cref="UsbEndpointInfo"/> class for this endpoint.</param> /// <returns>True of the endpoint was found, otherwise false.</returns> public static bool LookupEndpointInfo(UsbConfigInfo currentConfigInfo, byte endpointAddress, out UsbInterfaceInfo usbInterfaceInfo, out UsbEndpointInfo usbEndpointInfo) { bool found = false; usbInterfaceInfo = null; usbEndpointInfo = null; foreach (UsbInterfaceInfo interfaceInfo in currentConfigInfo.InterfaceInfoList) { foreach (UsbEndpointInfo endpointInfo in interfaceInfo.EndpointInfoList) { if ((endpointAddress & UsbConstants.ENDPOINT_NUMBER_MASK) == 0) { // find first read/write endpoint if ((endpointAddress & UsbConstants.ENDPOINT_DIR_MASK) == 0 && (endpointInfo.Descriptor.EndpointID & UsbConstants.ENDPOINT_DIR_MASK) == 0) { // first write endpoint found = true; } if ((endpointAddress & UsbConstants.ENDPOINT_DIR_MASK) != 0 && (endpointInfo.Descriptor.EndpointID & UsbConstants.ENDPOINT_DIR_MASK) != 0) { // first read endpoint found = true; } } else if (endpointInfo.Descriptor.EndpointID == endpointAddress) { found = true; } if (found) { usbInterfaceInfo = interfaceInfo; usbEndpointInfo = endpointInfo; return true; } } } return false; }
internal UsbConfigInfo(MonoUsbDevice usbDevice, MonoUsbConfigDescriptor configDescriptor) { mUsbDevice = usbDevice; mUsbConfigDescriptor = new UsbConfigDescriptor(configDescriptor); List<MonoUsbInterface> monoUSBInterfaces = configDescriptor.InterfaceList; foreach (MonoUsbInterface usbInterface in monoUSBInterfaces) { List<MonoUsbAltInterfaceDescriptor> monoUSBAltInterfaces = usbInterface.AltInterfaceList; foreach (MonoUsbAltInterfaceDescriptor monoUSBAltInterface in monoUSBAltInterfaces) { UsbInterfaceInfo usbInterfaceInfo = new UsbInterfaceInfo(mUsbDevice, monoUSBAltInterface); mInterfaceList.Add(usbInterfaceInfo); } } }
/// <summary> /// Looks up endpoint/interface information in a configuration. /// </summary> /// <param name="currentConfigInfo">The config to seach.</param> /// <param name="endpointAddress">The endpoint address to look for.</param> /// <param name="usbInterfaceInfo">On success, the <see cref="UsbInterfaceInfo"/> class for this endpoint.</param> /// <param name="usbEndpointInfo">On success, the <see cref="UsbEndpointInfo"/> class for this endpoint.</param> /// <returns>True of the endpoint was found, otherwise false.</returns> public static bool LookupEndpointInfo(UsbConfigInfo currentConfigInfo, byte endpointAddress, out UsbInterfaceInfo usbInterfaceInfo, out UsbEndpointInfo usbEndpointInfo) { return LookupEndpointInfo(currentConfigInfo, -1, endpointAddress, out usbInterfaceInfo, out usbEndpointInfo); }
private bool openAsTestDevice(UsbRegistry usbRegistry) { if (!ReferenceEquals(mUsbDevice, null)) closeTestDevice(mUsbDevice); mUsbDevice = null; try { if (usbRegistry.Open(out mUsbDevice)) { UsbInterfaceInfo readInterfaceInfo; UsbInterfaceInfo writeInterfaceInfo; UsbDevice.UsbErrorEvent += OnUsbError; if (!UsbEndpointBase.LookupEndpointInfo(mUsbDevice.Configs[0], 0x80, out readInterfaceInfo, out mReadEndpointInfo)) { throw new Exception("failed locating read endpoint."); } mBenchMarkParameters.BufferSize -= (mBenchMarkParameters.BufferSize%(mReadEndpointInfo.Descriptor.MaxPacketSize)); if (!UsbEndpointBase.LookupEndpointInfo(mUsbDevice.Configs[0], 0x00, out writeInterfaceInfo, out mWriteEndpointInfo)) { throw new Exception("failed locating write endpoint."); } if (((mWriteEndpointInfo.Descriptor.Attributes & 3)==(int) EndpointType.Isochronous) || ((mReadEndpointInfo.Descriptor.Attributes & 3)==(int) EndpointType.Isochronous)) { throw new Exception("buenchmark GUI application does not support ISO endpoints. Use BenchmarkCon instead."); } mBenchMarkParameters.BufferSize -= (mBenchMarkParameters.BufferSize%(mWriteEndpointInfo.Descriptor.MaxPacketSize)); if (writeInterfaceInfo.Descriptor.InterfaceID != readInterfaceInfo.Descriptor.InterfaceID) throw new Exception("read/write endpoints must be on the same interface."); mEP1Reader = mUsbDevice.OpenEndpointReader( (ReadEndpointID)mReadEndpointInfo.Descriptor.EndpointID, mBenchMarkParameters.BufferSize, (EndpointType)(mReadEndpointInfo.Descriptor.Attributes & 3)); mEP1Writer = mUsbDevice.OpenEndpointWriter( (WriteEndpointID) mWriteEndpointInfo.Descriptor.EndpointID, (EndpointType) (mWriteEndpointInfo.Descriptor.Attributes & 3)); mInterfaceInfo = writeInterfaceInfo; mEP1Reader.ReadThreadPriority = mBenchMarkParameters.Priority; mEP1Reader.DataReceived += OnDataReceived; makeTestBytes(out loopTestBytes, mBenchMarkParameters.BufferSize); // If this is a "whole" usb device (libusb-win32, linux libusb) // it will have an IUsbDevice interface. If not (WinUSB) the // variable will be null indicating this is an interface of a // device. IUsbDevice wholeUsbDevice = mUsbDevice as IUsbDevice; if (!ReferenceEquals(wholeUsbDevice, null)) { // This is a "whole" USB device. Before it can be used, // the desired configuration and interface must be selected. // Select config #1 wholeUsbDevice.SetConfiguration(1); // Claim interface #0. wholeUsbDevice.ClaimInterface(mInterfaceInfo.Descriptor.InterfaceID); } return true; } } catch(Exception ex) { SetStatus(ex.Message, true); } if (!ReferenceEquals(mUsbDevice,null)) { try { closeTestDevice(mUsbDevice); } finally { mUsbDevice = null; mEP1Reader = null; mEP1Writer = null; } } return false; }