private void CloseDevice() { lock (UsbLock) { if (DriverAPI != null) { DriverAPI.Free(); // Problematic... DriverAPI = null; } } }
private void OnHotPlug(KHOT_HANDLE hotHandle, KLST_DEVINFO_HANDLE deviceInfo, KLST_SYNC_FLAG plugType) { string plugText; int totalPluggedDeviceCount = (int)hotHandle.GetContext().ToInt64(); if (totalPluggedDeviceCount == int.MaxValue) { Console.WriteLine("OnHotPlug is being called for the first time on handle:{0}", hotHandle.Pointer); totalPluggedDeviceCount = 0; } switch (plugType) { case KLST_SYNC_FLAG.ADDED: plugText = "Arrival"; totalPluggedDeviceCount++; if (deviceInfo.DeviceID.Contains("VID_2DC4&PID_0200")) { bool deviceFound = false; setUI(HUD_STATE.Application); DarwinDevice = new StmTestParameters(SIX15_VID, SIX15_PID, 0, 0x02, 512, null, -1, 4, 0); if (DarwinDevice != null) { // Find and configure the device. if (!DarwinDevice.ConfigureDevice(out pipeInfo, out usb, out interfaceDescriptor)) { DarwinDevice = new StmTestParameters(SIX15_VID, SIX15_PID, 0, 0x06, 128, null, -1, 4, 0); if (!DarwinDevice.ConfigureDevice(out pipeInfo, out usb, out interfaceDescriptor)) { Console.WriteLine("Device not connected"); setUI(HUD_STATE.NotDetected); } else { Console.WriteLine("IS NEXT GEN FIRMWARE"); isNextGen = true; deviceFound = true; } } else { deviceFound = true; } if (deviceFound) { if (DarwinDevice.TransferBufferSize == -1) { DarwinDevice.TransferBufferSize = pipeInfo.MaximumPacketSize * 512; } Console.WriteLine("Darwin Device Connected"); int[] pipeTimeoutMS = new[] { 0 }; usb.SetPipePolicy((byte)DarwinDevice.PipeId, (int)PipePolicyType.PIPE_TRANSFER_TIMEOUT, Marshal.SizeOf(typeof(int)), pipeTimeoutMS); } } } else if (deviceInfo.DeviceID.Contains("VID_0483&PID_5740")) { setUI(HUD_STATE.Bootloader); DarwinDevice = new StmTestParameters(STM_VID, STM_PID, 0, 0x01, 512, null, -1, 4, 0); if (DarwinDevice != null) { // Find and configure the device. if (!DarwinDevice.ConfigureDevice(out pipeInfo, out usb, out interfaceDescriptor)) { Console.WriteLine("Device not connected"); setUI(HUD_STATE.NotDetected); } else { if (DarwinDevice.TransferBufferSize == -1) { DarwinDevice.TransferBufferSize = pipeInfo.MaximumPacketSize * 512; } Console.WriteLine("Darwin Device Connected"); int[] pipeTimeoutMS = new[] { 0 }; usb.SetPipePolicy((byte)DarwinDevice.PipeId, (int)PipePolicyType.PIPE_TRANSFER_TIMEOUT, Marshal.SizeOf(typeof(int)), pipeTimeoutMS); } } } break; case KLST_SYNC_FLAG.REMOVED: plugText = "Removal"; totalPluggedDeviceCount--; if (deviceInfo.DeviceID.Contains("VID_2DC4&PID_0200")) { if (DarwinDevice != null) { if (isNextGen) { usb.FlushPipe(EP_DISP); } else { usb.FlushPipe(EP_CDC); } DarwinDevice.Free(); usb.Free(); DarwinDevice = null; setUI(HUD_STATE.NotDetected); } else if (deviceInfo.DeviceID.Contains("VID_0483&PID_5740")) { usb.FlushPipe(EP_CDC_BL); DarwinDevice.Free(); usb.Free(); DarwinDevice = null; setUI(HUD_STATE.NotDetected); } } break; default: throw new ArgumentOutOfRangeException("plugType"); } hotHandle.SetContext(new IntPtr(totalPluggedDeviceCount)); Console.WriteLine("\n[OnHotPlug] Device {0}:{1} \n", plugText, deviceInfo); Console.WriteLine("Total Plugged Device Count: {0}", totalPluggedDeviceCount); }
public bool ConfigureDevice(out WINUSB_PIPE_INFORMATION pipeInfo, out UsbK usb, out USB_INTERFACE_DESCRIPTOR interfaceDescriptor) { bool success; Console.WriteLine("Finding usb device by VID/PID.. ({0:X4}h / {1:X4}h)", Vid, Pid); // Use a patttern match to include only matching devices. // NOTE: You can use the '*' and '?' chars as wildcards for all chars or a single char (respectively). KLST_PATTERN_MATCH patternMatch = new KLST_PATTERN_MATCH(); if (MI != -1) { patternMatch.DeviceID = String.Format("USB\\VID_{0:X4}&PID_{1:X4}&MI_{2:X2}*", Vid, Pid, MI); } else { patternMatch.DeviceID = String.Format("USB\\VID_{0:X4}&PID_{1:X4}*", Vid, Pid); } LstK deviceList = new LstK(KLST_FLAG.NONE, ref patternMatch); KLST_DEVINFO_HANDLE deviceInfo; interfaceDescriptor = new USB_INTERFACE_DESCRIPTOR(); pipeInfo = new WINUSB_PIPE_INFORMATION(); usb = null; // Iterate the devices looking for a matching alt-interface and endpoint id. while (deviceList.MoveNext(out deviceInfo)) { // libusbK class contructors can throw exceptions; For instance, if the device is // using the WinUsb driver and already in-use by another application. Console.WriteLine("Opening usb device.."); usb = new UsbK(deviceInfo); Console.WriteLine("Finding interface and endpoint by PipeId.."); success = FindPipeAndInterface(usb, out interfaceDescriptor, out pipeInfo); if (success) { break; } Console.WriteLine("PipeId {0:X2}h not found on this device.", PipeId); usb.Free(); usb = null; } if (ReferenceEquals(usb, null)) { Console.WriteLine("Usb device not found: {0}", patternMatch.DeviceID); success = false; goto Done; } MI = interfaceDescriptor.bInterfaceNumber; AltInterfaceId = interfaceDescriptor.bAlternateSetting; PipeId = pipeInfo.PipeId; // Set interface alt setting. Console.WriteLine("Setting interface #{0} to bAlternateSetting #{1}..", interfaceDescriptor.bInterfaceNumber, interfaceDescriptor.bAlternateSetting); success = usb.SetAltInterface(interfaceDescriptor.bInterfaceNumber, false, interfaceDescriptor.bAlternateSetting); if (!success) { Console.WriteLine("SetAltInterface failed. ErrorCode={0:X8}h", Marshal.GetLastWin32Error()); Console.WriteLine(interfaceDescriptor.ToString()); } Done: deviceList.Free(); return(success); }
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 bool ConfigureDevice(out WINUSB_PIPE_INFORMATION pipeInfo, out UsbK usb, out USB_INTERFACE_DESCRIPTOR interfaceDescriptor) { bool success; Console.WriteLine("Finding usb device by VID/PID.. ({0:X4}h / {1:X4}h)", Vid, Pid); // Use a patttern match to include only matching devices. // NOTE: You can use the '*' and '?' chars as wildcards for all chars or a single char (respectively). KLST_PATTERN_MATCH patternMatch = new KLST_PATTERN_MATCH(); if (MI != -1) patternMatch.DeviceID = String.Format("USB\\VID_{0:X4}&PID_{1:X4}&MI_{2:X2}*", Vid, Pid, MI); else patternMatch.DeviceID = String.Format("USB\\VID_{0:X4}&PID_{1:X4}*", Vid, Pid); LstK deviceList = new LstK(KLST_FLAG.NONE, ref patternMatch); KLST_DEVINFO_HANDLE deviceInfo; interfaceDescriptor = new USB_INTERFACE_DESCRIPTOR(); pipeInfo = new WINUSB_PIPE_INFORMATION(); usb = null; // Iterate the devices looking for a matching alt-interface and endpoint id. while (deviceList.MoveNext(out deviceInfo)) { // libusbK class contructors can throw exceptions; For instance, if the device is // using the WinUsb driver and already in-use by another application. Console.WriteLine("Opening usb device.."); usb = new UsbK(deviceInfo); Console.WriteLine("Finding interface and endpoint by PipeId.."); success = FindPipeAndInterface(usb, out interfaceDescriptor, out pipeInfo); if (success) break; Console.WriteLine("PipeId {0:X2}h not found on this device.", PipeId); usb.Free(); usb = null; } if (ReferenceEquals(usb, null)) { Console.WriteLine("Usb device not found: {0}", patternMatch.DeviceID); success = false; goto Done; } MI = interfaceDescriptor.bInterfaceNumber; AltInterfaceId = interfaceDescriptor.bAlternateSetting; PipeId = pipeInfo.PipeId; // Set interface alt setting. Console.WriteLine("Setting interface #{0} to bAlternateSetting #{1}..", interfaceDescriptor.bInterfaceNumber, interfaceDescriptor.bAlternateSetting); success = usb.SetAltInterface(interfaceDescriptor.bInterfaceNumber, false, interfaceDescriptor.bAlternateSetting); if (!success) { Console.WriteLine("SetAltInterface failed. ErrorCode={0:X8}h", Marshal.GetLastWin32Error()); Console.WriteLine(interfaceDescriptor.ToString()); } Done: deviceList.Free(); return success; }