USB_CONFIGURATION_DESCRIPTOR ByteToConfigDescriptor(byte[] packet) { GCHandle pinnedPacket = GCHandle.Alloc(packet, GCHandleType.Pinned); USB_CONFIGURATION_DESCRIPTOR descriptor = (USB_CONFIGURATION_DESCRIPTOR)Marshal.PtrToStructure( pinnedPacket.AddrOfPinnedObject(), typeof(USB_CONFIGURATION_DESCRIPTOR)); pinnedPacket.Free(); return(descriptor); }
internal static extern Boolean WinUsb_GetDescriptor(IntPtr InterfaceHandle, Byte DescriptorType, Byte Index, UInt32 LanguageID, ref USB_CONFIGURATION_DESCRIPTOR Buffer, UInt32 BufferLength, ref UInt32 LengthTransfered);
private bool SelectConfiguration(EusbDeviceContext device, byte configIndex) { // 6. Sends TS_URB_CONTROL_DESCRIPTOR_REQUEST with the descriptor type of USB_DEVICE_DESCRIPTOR_TYPE. uint requestId = IdGenerator.NewId(); TS_URB_CONTROL_DESCRIPTOR_REQUEST des = new UrbBuilder( URB_FUNCTIONID.URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE, requestId, 0).BuildDeviceDescriptorRequest(); rdpeusbAdapter.TransferInRequest(device, des, USB_DEVICE_DESCRIPTOR.DefaultSize); // 7. Receives a completion message with the result for USB_DEVICE_DESCRIPTOR."); EusbUrbCompletionPdu pdu = (EusbUrbCompletionPdu)rdpeusbAdapter.ExpectCompletion(device.VirtualChannel); if (null == pdu || pdu.HResult != (uint)HRESULT_FROM_WIN32.ERROR_SUCCESS) { return(false); } USB_DEVICE_DESCRIPTOR desDevice = UsbStructParser.Parse <USB_DEVICE_DESCRIPTOR>(pdu); // 8. Sends TS_URB_CONTROL_DESCRIPTOR_REQUEST to retrieve the total length of the configuration. requestId = IdGenerator.NewId(); des = new UrbBuilder( URB_FUNCTIONID.URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE, requestId, 0).BuildConfigurationDescriptorRequest(configIndex); rdpeusbAdapter.TransferInRequest(device, des, USB_CONFIGURATION_DESCRIPTOR.DefaultSize); // 9. Receives a completion message with the result for USB_CONFIGURATION_DESCRIPTOR."); pdu = (EusbUrbCompletionPdu)rdpeusbAdapter.ExpectCompletion(device.VirtualChannel); if (null == pdu || pdu.HResult != (uint)HRESULT_FROM_WIN32.ERROR_SUCCESS) { return(false); } USB_CONFIGURATION_DESCRIPTOR desConfig = UsbStructParser.Parse <USB_CONFIGURATION_DESCRIPTOR>(pdu); // 10. Sends TS_URB_CONTROL_DESCRIPTOR_REQUEST with the actual length of USB_CONFIGURATION_DESCRIPTOR result. requestId = IdGenerator.NewId(); des = new UrbBuilder( URB_FUNCTIONID.URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE, requestId, 0).BuildConfigurationDescriptorRequest(configIndex); rdpeusbAdapter.TransferInRequest(device, des, desConfig.wTotalLength); // 11. Receives a completion message with the complete result for USB_CONFIGURATION_DESCRIPTOR."); pdu = (EusbUrbCompletionPdu)rdpeusbAdapter.ExpectCompletion(device.VirtualChannel); // 12. Sends TS_URB_SELECT_CONFIGURATION URB request. UsbConfigurationParser configParser = new UsbConfigurationParser(); configParser.ParseAll(pdu); requestId = IdGenerator.NewId(); TS_URB_SELECT_CONFIGURATION sel = new UrbBuilder( URB_FUNCTIONID.URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE, requestId, 0).BuildSelectConfigRequest(configParser.Interfaces, configParser.configDescriptor); rdpeusbAdapter.TransferInRequest(device, sel, 0); // 13. Receives a completion message with the result for configuration selection."); EusbUrbCompletionNoDataPdu pduRes = (EusbUrbCompletionNoDataPdu)rdpeusbAdapter.ExpectCompletion(device.VirtualChannel); if (null == pduRes || pduRes.HResult != (uint)HRESULT_FROM_WIN32.ERROR_SUCCESS) { return(false); } TS_URB_SELECT_CONFIGURATION_RESULT urb = new TS_URB_SELECT_CONFIGURATION_RESULT(); if (!PduMarshaler.Unmarshal(pduRes.TsUrbResult, urb)) { return(false); } context.SelectedConfig = urb; return(true); }
public bool InitializeDevice() { if (deviceList != null) { deviceList.Free(); } deviceList = null; if (usb != null) { usb.Free(); } usb = null; feature_value_dict.Clear(); feature_value_lookup_dict.Clear(); foreach (Control ctrl in this.Controls) { if (ctrl is ComboBox) { ComboBox box = (ComboBox)ctrl; box.Items.Clear(); } } deviceList = new LstK(KLST_FLAG.NONE); KLST_DEVINFO_HANDLE deviceInfo; DeviceInfo.Text = ""; bool success = false; deviceList.MoveReset(); while (deviceList.MoveNext(out deviceInfo)) { if ( // BSB 20120928 revised test // (deviceInfo.Common.Vid == vendorid1 || deviceInfo.Common.Vid == vendorid2) && // (deviceInfo.Common.Pid == productid1 || deviceInfo.Common.Pid == productid2 || deviceInfo.Common.Pid == productid3 || deviceInfo.Common.Pid == productid4) && // deviceInfo.DeviceInterfaceGUID.ToUpper() == "{D49AB938-53BA-498A-A848-8E2780A4A75F}" (deviceInfo.DeviceInterfaceGUID.ToUpper() == "{D49AB938-53BA-498A-A848-8E2780A4A75F}") && ( ( (deviceInfo.Common.Vid == vendorid1 || deviceInfo.Common.Vid == vendorid2) && (deviceInfo.Common.Pid == productid1 || deviceInfo.Common.Pid == productid2 || deviceInfo.Common.Pid == productid3 || deviceInfo.Common.Pid == productid4) ) || ( (deviceInfo.Common.Vid == audio_vendor_id) && ( (deviceInfo.Common.Pid == audio_product_id_1) || (deviceInfo.Common.Pid == audio_product_id_2) || (deviceInfo.Common.Pid == audio_product_id_3) || (deviceInfo.Common.Pid == audio_product_id_4) || (deviceInfo.Common.Pid == audio_product_id_5) || (deviceInfo.Common.Pid == audio_product_id_6) || (deviceInfo.Common.Pid == audio_product_id_7) || (deviceInfo.Common.Pid == audio_product_id_8) || (deviceInfo.Common.Pid == audio_product_id_9) || (deviceInfo.Common.Pid == audio_product_id_10) || (deviceInfo.Common.Pid == audio_product_id_11) || (deviceInfo.Common.Pid == audio_product_id_12) ) ) ) ) { if (deviceInfo.Connected) { success = true; break; } } } if (!success) { DeviceInfo.Text += String.Format("Audio-Widget device not found!\r\n"); if (deviceList != null) { deviceList.Free(); } deviceList = null; return(false); } usb = new UsbK(deviceInfo); DeviceInfo.Text += String.Format("Opening usb device OK\r\n"); usb.GetDescriptor((byte)USB_DESCRIPTOR_TYPE.DEVICE, 0, 0, globalBuffer, globalBufferLength, out LengthTransferred); USB_DEVICE_DESCRIPTOR deviceDescriptor = ByteToDeviceDescriptor(globalBuffer); usb.GetDescriptor((byte)USB_DESCRIPTOR_TYPE.CONFIGURATION, 0, 0, globalBuffer, globalBufferLength, out LengthTransferred); USB_CONFIGURATION_DESCRIPTOR configurationDescriptor = ByteToConfigDescriptor(globalBuffer); string product = GetStringDescriptor(deviceDescriptor.iProduct); string manufacturer = GetStringDescriptor(deviceDescriptor.iManufacturer); string serial = GetStringDescriptor(deviceDescriptor.iSerialNumber); DeviceInfo.Text += String.Format("Device: VID=0x{0:X04}/PID=0x{1:X04}\r\n", deviceDescriptor.idVendor, deviceDescriptor.idProduct); DeviceInfo.Text += String.Format("Product: {0}\r\n", product); DeviceInfo.Text += String.Format("Manufacturer: {0}\r\n", manufacturer); DeviceInfo.Text += String.Format("Serial number: {0}\r\n", serial); success = SendUsbControl(interfaceNumber, (byte)BMREQUEST_DIR.DEVICE_TO_HOST, (byte)BMREQUEST_TYPE.VENDOR, (byte)BMREQUEST_RECIPIENT.DEVICE, 0x71, 4, 1, globalBuffer, globalBufferLength, out LengthTransferred); ushort max_feature_value_index = globalBuffer[0]; ushort feature_index = 0; ComboBox control = null; for (ushort i = 0; i < max_feature_value_index; i++) { success = SendUsbControl(interfaceNumber, (byte)BMREQUEST_DIR.DEVICE_TO_HOST, (byte)BMREQUEST_TYPE.VENDOR, (byte)BMREQUEST_RECIPIENT.DEVICE, 0x71, 8, i, globalBuffer, globalBufferLength, out LengthTransferred); if (globalBuffer[0] == 63) { break; } string output_str = ""; for (int s = (int)LengthTransferred - 1; s >= 0; s--) { output_str += (char)globalBuffer[s]; } if (output_str == "end") { feature_index++; control = null; } else { if (control == null) { control = FindFeatureControl(feature_index); } if (control != null) { control.Items.Add(output_str); feature_value_dict[(int)i] = output_str; feature_value_lookup_dict[feature_index.ToString() + output_str] = (int)i; } } if (i > 100) { break; } } foreach (Control ctrl in this.Controls) { if (ctrl is ComboBox) { int index = Convert.ToInt32(ctrl.Tag); ComboBox box = (ComboBox)ctrl; success = SendUsbControl(interfaceNumber, (byte)BMREQUEST_DIR.DEVICE_TO_HOST, (byte)BMREQUEST_TYPE.VENDOR, (byte)BMREQUEST_RECIPIENT.DEVICE, 0x71, 4, (byte)(2 + index), globalBuffer, globalBufferLength, out LengthTransferred); int feature_value_index = (int)globalBuffer[0]; if (feature_value_dict.ContainsKey(feature_value_index)) { string text = feature_value_dict[feature_value_index].ToString(); box.SelectedIndex = box.Items.IndexOf(text); } } } return(true); }
public static extern bool WinUsb_GetDescriptor(IntPtr InterfaceHandle, byte DescriptorType, byte Index, UInt16 LanguageID, out USB_CONFIGURATION_DESCRIPTOR deviceDesc, UInt32 BufferLength, out UInt32 LengthTransfered);
public static extern bool WinUsb_GetDescriptor(IntPtr InterfaceHandle, byte DescriptorType, byte Index, UInt16 LanguageID, out USB_CONFIGURATION_DESCRIPTOR deviceDesc, UInt32 BufferLength, out UInt32 LengthTransfered);
public static IntPtr WinUsb_ParseConfigurationDescriptor(ref USB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor, IntPtr StartPosition, int InterfaceNumber, int AlternateSetting, int InterfaceClass, int InterfaceSubClass, int InterfaceProtocol) { return(NativeMethods.WinUsb_ParseConfigurationDescriptor(ref ConfigurationDescriptor, StartPosition, InterfaceNumber, AlternateSetting, InterfaceClass, InterfaceSubClass, InterfaceProtocol)); }
public static extern IntPtr WinUsb_ParseConfigurationDescriptor(ref USB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor, IntPtr StartPosition, int InterfaceNumber, int AlternateSetting, int InterfaceClass, int InterfaceSubClass, int InterfaceProtocol);