protected static List <USBPort> GetHubPorts(USBHub hub) { List <USBPort> ports = new List <USBPort>(); // Collect the node information IOCTLcommand ioctl = new IOCTLcommand(); ioctl.ioctlNo = UsbApi.CTL_CODE(0x00000022, (uint)UsbApi.UsbIoctlFunction.USB_GET_NODE_INFORMATION, 0, 0x0); ioctl.inputBuffer = null; ioctl.outputMaxSize = 128; try { // Open Root hub IntPtr hHub = UsbApi.OpenDevice(hub.devicePath, true); IoStatus status = UsbApi.DeviceIoControl(hHub, ioctl); UsbApi.UsbNativeType.USB_NODE_INFORMATION hubInfo = (UsbApi.UsbNativeType.USB_NODE_INFORMATION)UsbApi.UsbNativeType.Deserialize( status.buffer, 0, typeof(UsbApi.UsbNativeType.USB_NODE_INFORMATION)); UsbApi.CloseDevice(hHub); // Now Iterate through all the ports for (uint i = 1; i <= hubInfo.NodeInfo.hubDescriptor.bNumberOfPorts; i++) { ports.Add(new USBPort(hub, i)); } } catch (Exception) {} return(ports); }
protected unsafe static void GetPortInformation(USBPort port) { // Collect the node information IOCTLcommand ioctl = new IOCTLcommand(); ioctl.ioctlNo = UsbApi.CTL_CODE(0x00000022, (uint)UsbApi.UsbIoctlFunction.USB_GET_NODE_CONNECTION_INFORMATION_EX, 0, 0x0); port.connInfo = new UsbApi.UsbNativeType.USB_NODE_CONNECTION_INFORMATION(); port.connInfo.ConnectionIndex = port.portIndex; ioctl.inputBuffer = UsbApi.UsbNativeType.Serialize(port.connInfo); ioctl.outputMaxSize = (uint)Marshal.SizeOf(port.connInfo); try { // Open Root hub IntPtr hHub = UsbApi.OpenDevice(port.rootHub.devicePath, true); IoStatus status = UsbApi.DeviceIoControl(hHub, ioctl); UsbApi.CloseDevice(hHub); port.connInfo = (UsbApi.UsbNativeType.USB_NODE_CONNECTION_INFORMATION)UsbApi.UsbNativeType.Deserialize( status.buffer, 0, typeof(UsbApi.UsbNativeType.USB_NODE_CONNECTION_INFORMATION)); } catch (Exception) {} }
protected static string GetHCDDriverKeyName(string devicePath) { char[] decoded = null; IOCTLcommand ioctl = new IOCTLcommand(); ioctl.ioctlNo = UsbApi.CTL_CODE(0x00000022, (uint)UsbApi.UsbIoctlFunction.HCD_GET_DRIVERKEY_NAME, 0, 0x0); ioctl.inputBuffer = null; ioctl.outputMaxSize = 260; try { IntPtr hDevice = UsbApi.OpenDevice(devicePath, true); IoStatus status = UsbApi.DeviceIoControl(hDevice, ioctl); UsbApi.CloseDevice(hDevice); // struct {ULONG Length; WCHAR Name[256];} DriverKeyName; decoded = Encoding.Unicode.GetChars(status.buffer, IntPtr.Size, (int)status.size - IntPtr.Size); } catch (Exception) { } return((decoded == null) ? null : new String(decoded)); }
public unsafe static IoStatus DeviceIoControl(IntPtr handle, IOCTLcommand command) { GCHandle hInput = new GCHandle(); GCHandle hOutput = new GCHandle(); IoStatus status = new IoStatus(); status.buffer = new byte[command.outputMaxSize]; status.size = 0; status.error = USBError.SUCCESS; try { // pin the buffers into place hInput = GCHandle.Alloc(command.inputBuffer, GCHandleType.Pinned); hOutput = GCHandle.Alloc(status.buffer, GCHandleType.Pinned); if (UsbNativeApi.DeviceIoControl( handle, command.ioctlNo, hInput.AddrOfPinnedObject(), (command.inputBuffer == null) ? 0 : (uint)command.inputBuffer.Length, hOutput.AddrOfPinnedObject(), (uint)command.outputMaxSize, ref status.size, (IntPtr)0) != 0) { status.error = USBError.WIN_API_SPECIFIC; } } finally { if (hInput.IsAllocated) { hInput.Free(); } if (hOutput.IsAllocated) { hOutput.Free(); } } return(status); }
protected static string GetRootHubDevicePath(USBController controller) { // Now get the system name of it's root hub for interrogation char[] decoded = null; IOCTLcommand ioctl = new IOCTLcommand(); ioctl.ioctlNo = UsbApi.CTL_CODE(0x00000022, (uint)UsbApi.UsbIoctlFunction.HCD_GET_ROOT_HUB_NAME, 0, 0x0); ioctl.inputBuffer = null; ioctl.outputMaxSize = 260; try { IntPtr hDevice = UsbApi.OpenDevice(controller.devicePath, true); IoStatus status = UsbApi.DeviceIoControl(hDevice, ioctl); UsbApi.CloseDevice(hDevice); // struct {ULONG Length; WCHAR Name[256];} DriverKeyName; decoded = Encoding.Unicode.GetChars(status.buffer, IntPtr.Size, (int)status.size - IntPtr.Size); } catch (Exception) { } return((decoded == null) ? null : "\\\\.\\" + new String(decoded)); }
protected unsafe static string GetStringDescriptor(USBPort port, Byte stringIndex) { UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST descriptor = new UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST(); descriptor.ConnectionIndex = port.portIndex; // USBD will automatically initialize these fields: // bmRequest = 0x80 // bRequest = 0x06 // wValue = Descriptor Type (high) and Descriptor Index (low byte) descriptor.wValue = (ushort)(((ushort)UsbApi.UsbNativeType.UsbDescriptorType.USB_STRING_DESCRIPTOR_TYPE << 8) | (ushort)stringIndex); // wIndex = Zero (or Language ID for String Descriptors) descriptor.wIndex = (ushort)0; // wLength = Length of descriptor buffer descriptor.wLength = (ushort)Marshal.SizeOf(typeof(UsbApi.UsbNativeType.USB_STRING_DESCRIPTOR)); // Collect the information string IOCTLcommand ioctl = new IOCTLcommand(); ioctl.ioctlNo = UsbApi.CTL_CODE(0x00000022, (uint)UsbApi.UsbIoctlFunction.USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, 0, 0x0); ioctl.inputBuffer = UsbApi.UsbNativeType.Serialize(descriptor); ioctl.outputMaxSize = (uint)Marshal.SizeOf(descriptor); string result = null; try { // Open Root hub IntPtr hHub = UsbApi.OpenDevice(port.rootHub.devicePath, true); IoStatus status = UsbApi.DeviceIoControl(hHub, ioctl); UsbApi.CloseDevice(hHub); descriptor = (UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST)UsbApi.UsbNativeType.Deserialize( status.buffer, 0, typeof(UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST)); UsbApi.UsbNativeType.USB_STRING_DESCRIPTOR strDescr = (UsbApi.UsbNativeType.USB_STRING_DESCRIPTOR)UsbApi.UsbNativeType.Deserialize( descriptor.Data, 0, typeof(UsbApi.UsbNativeType.USB_STRING_DESCRIPTOR)); result = new String(Encoding.Unicode.GetChars(strDescr.bString, 0, strDescr.bLength)); } catch (Exception) { result = "Invalid string value"; } return(result); }
public IoStatus DeviceIoControl(IOCTLcommand command) { return(UsbApi.DeviceIoControl(outHandle, command)); }
internal unsafe static List <UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR> GetConfigurationDescriptors(USBPort port, Byte configIndex) { UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST descriptor = new UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST(); descriptor.ConnectionIndex = port.portIndex; // USBD will automatically initialize these fields: // bmRequest = 0x80 // bRequest = 0x06 // wValue = Descriptor Type (high) and Descriptor Index (low byte) descriptor.wValue = (ushort)(((ushort)UsbApi.UsbNativeType.UsbDescriptorType.USB_CONFIGURATION_DESCRIPTOR_TYPE << 8) | (ushort)configIndex); // wIndex = Zero (or Language ID for String Descriptors) descriptor.wIndex = (ushort)0; // wLength = Length of descriptor buffer descriptor.wLength = (ushort)512; // Collect the information string IOCTLcommand ioctl = new IOCTLcommand(); ioctl.ioctlNo = UsbApi.CTL_CODE(0x00000022, (uint)UsbApi.UsbIoctlFunction.USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, 0, 0x0); ioctl.inputBuffer = UsbApi.UsbNativeType.Serialize(descriptor); ioctl.outputMaxSize = (uint)Marshal.SizeOf(descriptor); List <UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR> descriptorList = new List <UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR>(); try { // Open Root hub IntPtr hHub = UsbApi.OpenDevice(port.rootHub.devicePath, true); IoStatus status = UsbApi.DeviceIoControl(hHub, ioctl); UsbApi.CloseDevice(hHub); descriptor = (UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST)UsbApi.UsbNativeType.Deserialize( status.buffer, 0, typeof(UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST)); UsbApi.UsbNativeType.USB_CONFIGURATION_DESCRIPTOR confDescr = (UsbApi.UsbNativeType.USB_CONFIGURATION_DESCRIPTOR)UsbApi.UsbNativeType.Deserialize( descriptor.Data, 0, typeof(UsbApi.UsbNativeType.USB_CONFIGURATION_DESCRIPTOR)); descriptorList.Add(confDescr); // Now deserialize all returned descriptors int descrStart = confDescr.bLength; UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR common; while (descrStart < confDescr.wTotalLength) { common = (UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR)UsbApi.UsbNativeType.Deserialize( descriptor.Data, descrStart, typeof(UsbApi.UsbNativeType.USB_COMMON_DESCRIPTOR)); switch (common.DescriptorType) { case UsbApi.UsbNativeType.UsbDescriptorType.USB_CONFIGURATION_DESCRIPTOR_TYPE: common = (UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR)UsbApi.UsbNativeType.Deserialize( descriptor.Data, descrStart, typeof(UsbApi.UsbNativeType.USB_CONFIGURATION_DESCRIPTOR)); break; case UsbApi.UsbNativeType.UsbDescriptorType.USB_INTERFACE_DESCRIPTOR_TYPE: common = (UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR)UsbApi.UsbNativeType.Deserialize( descriptor.Data, descrStart, typeof(UsbApi.UsbNativeType.USB_INTERFACE_DESCRIPTOR)); break; case UsbApi.UsbNativeType.UsbDescriptorType.USB_ENDPOINT_DESCRIPTOR_TYPE: common = (UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR)UsbApi.UsbNativeType.Deserialize( descriptor.Data, descrStart, typeof(UsbApi.UsbNativeType.USB_ENDPOINT_DESCRIPTOR)); break; } descrStart += common.Length; descriptorList.Add(common); } } catch (Exception) {} return(descriptorList); }