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); }
// Return Root Hub for this Controller public USBHub GetRootHub() { var Root = new USBHub { HubIsRootHub = true, HubDeviceDesc = "Root Hub" }; // Open a handle to the Host Controller var h = CreateFile(ControllerDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); if (h.ToInt32() == INVALID_HANDLE_VALUE) { return(Root); } int nBytesReturned; var HubName = new USB_ROOT_HUB_NAME(); var nBytes = Marshal.SizeOf(HubName); var ptrHubName = Marshal.AllocHGlobal(nBytes); // get the Hub Name if (DeviceIoControl(h, IOCTL_USB_GET_ROOT_HUB_NAME, ptrHubName, nBytes, ptrHubName, nBytes, out nBytesReturned, IntPtr.Zero)) { HubName = (USB_ROOT_HUB_NAME)Marshal.PtrToStructure(ptrHubName, typeof(USB_ROOT_HUB_NAME)); Root.HubDevicePath = @"\\.\" + HubName.RootHubName; } // TODO: Get DriverKeyName for Root Hub // Now let's open the Hub (based upon the HubName we got above) var h2 = CreateFile(Root.HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); if (h2.ToInt32() != INVALID_HANDLE_VALUE) { var NodeInfo = new USB_NODE_INFORMATION { NodeType = (int)USB_HUB_NODE.UsbHub }; nBytes = Marshal.SizeOf(NodeInfo); var ptrNodeInfo = Marshal.AllocHGlobal(nBytes); Marshal.StructureToPtr(NodeInfo, ptrNodeInfo, true); // get the Hub Information if (DeviceIoControl(h2, IOCTL_USB_GET_NODE_INFORMATION, ptrNodeInfo, nBytes, ptrNodeInfo, nBytes, out nBytesReturned, IntPtr.Zero)) { NodeInfo = (USB_NODE_INFORMATION)Marshal.PtrToStructure(ptrNodeInfo, typeof(USB_NODE_INFORMATION)); Root.HubIsBusPowered = Convert.ToBoolean(NodeInfo.HubInformation.HubIsBusPowered); Root.HubPortCount = NodeInfo.HubInformation.HubDescriptor.bNumberOfPorts; } Marshal.FreeHGlobal(ptrNodeInfo); CloseHandle(h2); } Marshal.FreeHGlobal(ptrHubName); CloseHandle(h); return(Root); }
public USBHub GetHub() { if (!this.PortIsHub) { return(null); } USBHub uSBHub = new USBHub(); uSBHub.HubIsRootHub = false; uSBHub.HubDeviceDesc = "External Hub"; IntPtr intPtr = Acer_USB_Library.CreateFile(this.PortHubDevicePath, 1073741824, 2, IntPtr.Zero, 3, 0, IntPtr.Zero); if (intPtr.ToInt32() != -1) { USB_NODE_CONNECTION_NAME uSB_NODE_CONNECTION_NAME = default(USB_NODE_CONNECTION_NAME); uSB_NODE_CONNECTION_NAME.ConnectionIndex = this.PortPortNumber; int num = Marshal.SizeOf(uSB_NODE_CONNECTION_NAME); IntPtr intPtr2 = Marshal.AllocHGlobal(num); Marshal.StructureToPtr(uSB_NODE_CONNECTION_NAME, intPtr2, true); int num2 = default(int); if (Acer_USB_Library.DeviceIoControl(intPtr, 2229268, intPtr2, num, intPtr2, num, out num2, IntPtr.Zero)) { uSB_NODE_CONNECTION_NAME = (USB_NODE_CONNECTION_NAME)Marshal.PtrToStructure(intPtr2, typeof(USB_NODE_CONNECTION_NAME)); uSBHub.HubDevicePath = "\\\\.\\" + uSB_NODE_CONNECTION_NAME.NodeName; } IntPtr intPtr3 = Acer_USB_Library.CreateFile(uSBHub.HubDevicePath, 1073741824, 2, IntPtr.Zero, 3, 0, IntPtr.Zero); if (intPtr3.ToInt32() != -1) { USB_NODE_INFORMATION uSB_NODE_INFORMATION = default(USB_NODE_INFORMATION); uSB_NODE_INFORMATION.NodeType = 0; num = Marshal.SizeOf(uSB_NODE_INFORMATION); IntPtr intPtr4 = Marshal.AllocHGlobal(num); Marshal.StructureToPtr(uSB_NODE_INFORMATION, intPtr4, true); if (Acer_USB_Library.DeviceIoControl(intPtr3, 2229256, intPtr4, num, intPtr4, num, out num2, IntPtr.Zero)) { uSB_NODE_INFORMATION = (USB_NODE_INFORMATION)Marshal.PtrToStructure(intPtr4, typeof(USB_NODE_INFORMATION)); uSBHub.HubIsBusPowered = Convert.ToBoolean(uSB_NODE_INFORMATION.HubInformation.HubIsBusPowered); uSBHub.HubPortCount = uSB_NODE_INFORMATION.HubInformation.HubDescriptor.bNumberOfPorts; } Marshal.FreeHGlobal(intPtr4); Acer_USB_Library.CloseHandle(intPtr3); } USBDevice device = this.GetDevice(); uSBHub.HubInstanceID = device.DeviceInstanceID; uSBHub.HubManufacturer = device.Manufacturer; uSBHub.HubProduct = device.Product; uSBHub.HubSerialNumber = device.SerialNumber; uSBHub.HubDriverKey = device.DriverKey; Marshal.FreeHGlobal(intPtr2); Acer_USB_Library.CloseHandle(intPtr); } return(uSBHub); }
/// <summary> /// Initializes USB /// </summary> private static void initUSB() { USB.USB.Init(); USBDrivers.Init(); USBHub.Init(); USBHIDMouse.Init(); USBHIDKeyboard.Init(); USBMSC.Init(); UHCI.Init(); EHCI.Init(); }
// private routine for enumerating a hub static void ListHub(USBHub Hub, List <USBDevice> DevList) { foreach (USBPort Port in Hub.GetPorts()) { if (Port.IsHub) { // recursive ListHub(Port.GetHub(), DevList); } else { if (Port.IsDeviceConnected) { DevList.Add(Port.GetDevice()); } } } }
public USBHub GetRootHub() { USBHub uSBHub = new USBHub(); uSBHub.HubIsRootHub = true; uSBHub.HubDeviceDesc = "Root Hub"; IntPtr intPtr = Acer_USB_Library.CreateFile(this.ControllerDevicePath, 1073741824, 2, IntPtr.Zero, 3, 0, IntPtr.Zero); if (intPtr.ToInt32() != -1) { int num = Marshal.SizeOf(default(USB_ROOT_HUB_NAME)); IntPtr intPtr2 = Marshal.AllocHGlobal(num); int num2 = default(int); if (Acer_USB_Library.DeviceIoControl(intPtr, 2229256, intPtr2, num, intPtr2, num, out num2, IntPtr.Zero)) { USB_ROOT_HUB_NAME uSB_ROOT_HUB_NAME = (USB_ROOT_HUB_NAME)Marshal.PtrToStructure(intPtr2, typeof(USB_ROOT_HUB_NAME)); uSBHub.HubDevicePath = "\\\\.\\" + uSB_ROOT_HUB_NAME.RootHubName; } IntPtr intPtr3 = Acer_USB_Library.CreateFile(uSBHub.HubDevicePath, 1073741824, 2, IntPtr.Zero, 3, 0, IntPtr.Zero); if (intPtr3.ToInt32() != -1) { USB_NODE_INFORMATION uSB_NODE_INFORMATION = default(USB_NODE_INFORMATION); uSB_NODE_INFORMATION.NodeType = 0; num = Marshal.SizeOf(uSB_NODE_INFORMATION); IntPtr intPtr4 = Marshal.AllocHGlobal(num); Marshal.StructureToPtr(uSB_NODE_INFORMATION, intPtr4, true); if (Acer_USB_Library.DeviceIoControl(intPtr3, 2229256, intPtr4, num, intPtr4, num, out num2, IntPtr.Zero)) { uSB_NODE_INFORMATION = (USB_NODE_INFORMATION)Marshal.PtrToStructure(intPtr4, typeof(USB_NODE_INFORMATION)); uSBHub.HubIsBusPowered = Convert.ToBoolean(uSB_NODE_INFORMATION.HubInformation.HubIsBusPowered); uSBHub.HubPortCount = uSB_NODE_INFORMATION.HubInformation.HubDescriptor.bNumberOfPorts; } Marshal.FreeHGlobal(intPtr4); Acer_USB_Library.CloseHandle(intPtr3); } Marshal.FreeHGlobal(intPtr2); Acer_USB_Library.CloseHandle(intPtr); } return(uSBHub); }
// private routine for enumerating a hub static void SearchHubDriverKeyName(USBHub Hub, ref USBDevice FoundDevice, string DriverKeyName) { foreach (USBPort Port in Hub.GetPorts()) { if (Port.IsHub) { // recursive SearchHubDriverKeyName(Port.GetHub(), ref FoundDevice, DriverKeyName); } else { if (Port.IsDeviceConnected) { USBDevice Device = Port.GetDevice(); if (Device.DeviceDriverKey == DriverKeyName) { FoundDevice = Device; break; } } } } }
// private routine for enumerating a hub static void SearchHubInstanceID(USBHub Hub, ref USBDevice FoundDevice, string InstanceID) { foreach (USBPort Port in Hub.GetPorts()) { if (Port.IsHub) { // recursive SearchHubInstanceID(Port.GetHub(), ref FoundDevice, InstanceID); } else { if (Port.IsDeviceConnected) { USBDevice Device = Port.GetDevice(); if (Device.InstanceID == InstanceID) { FoundDevice = Device; break; } } } } }
// Return Root Hub for this Controller public USBHub GetRootHub() { IntPtr h, h2; USBHub Root = new USBHub(); Root.HubIsRootHub = true; Root.HubDeviceDesc = "Root Hub"; // Open a handle to the Host Controller h = CreateFile(ControllerDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); if (h.ToInt32() != INVALID_HANDLE_VALUE) { int nBytesReturned; USB_ROOT_HUB_NAME HubName = new USB_ROOT_HUB_NAME(); int nBytes = Marshal.SizeOf(HubName); IntPtr ptrHubName = Marshal.AllocHGlobal(nBytes); // get the Hub Name if (DeviceIoControl(h, IOCTL_USB_GET_ROOT_HUB_NAME, ptrHubName, nBytes, ptrHubName, nBytes, out nBytesReturned, IntPtr.Zero)) { HubName = (USB_ROOT_HUB_NAME)Marshal.PtrToStructure(ptrHubName, typeof(USB_ROOT_HUB_NAME)); Root.HubDevicePath = @"\\.\" + HubName.RootHubName; } // TODO: Get DriverKeyName for Root Hub // Now let's open the Hub (based upon the HubName we got above) h2 = CreateFile(Root.HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); if (h2.ToInt32() != INVALID_HANDLE_VALUE) { USB_NODE_INFORMATION NodeInfo = new USB_NODE_INFORMATION(); NodeInfo.NodeType = (int)USB_HUB_NODE.UsbHub; nBytes = Marshal.SizeOf(NodeInfo); IntPtr ptrNodeInfo = Marshal.AllocHGlobal(nBytes); Marshal.StructureToPtr(NodeInfo, ptrNodeInfo, true); // get the Hub Information if (DeviceIoControl(h2, IOCTL_USB_GET_NODE_INFORMATION, ptrNodeInfo, nBytes, ptrNodeInfo, nBytes, out nBytesReturned, IntPtr.Zero)) { NodeInfo = (USB_NODE_INFORMATION)Marshal.PtrToStructure(ptrNodeInfo, typeof(USB_NODE_INFORMATION)); Root.HubIsBusPowered = Convert.ToBoolean(NodeInfo.HubInformation.HubIsBusPowered); Root.HubPortCount = NodeInfo.HubInformation.HubDescriptor.bNumberOfPorts; } Marshal.FreeHGlobal(ptrNodeInfo); CloseHandle(h2); } Marshal.FreeHGlobal(ptrHubName); CloseHandle(h); } return Root; }
// return a down stream external hub public USBHub GetHub() { if (!PortIsHub) { return null; } USBHub Hub = new USBHub(); IntPtr h, h2; Hub.HubIsRootHub = false; Hub.HubDeviceDesc = "External Hub"; // Open a handle to the Host Controller h = CreateFile(PortHubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); if (h.ToInt32() != INVALID_HANDLE_VALUE) { // Get the DevicePath for downstream hub int nBytesReturned; USB_NODE_CONNECTION_NAME NodeName = new USB_NODE_CONNECTION_NAME(); NodeName.ConnectionIndex = PortPortNumber; int nBytes = Marshal.SizeOf(NodeName); IntPtr ptrNodeName = Marshal.AllocHGlobal(nBytes); Marshal.StructureToPtr(NodeName, ptrNodeName, true); // Use an IOCTL call to request the Node Name if (DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_NAME, ptrNodeName, nBytes, ptrNodeName, nBytes, out nBytesReturned, IntPtr.Zero)) { NodeName = (USB_NODE_CONNECTION_NAME)Marshal.PtrToStructure(ptrNodeName, typeof(USB_NODE_CONNECTION_NAME)); Hub.HubDevicePath = @"\\.\" + NodeName.NodeName; } // Now let's open the Hub (based upon the HubName we got above) h2 = CreateFile(Hub.HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); if (h2.ToInt32() != INVALID_HANDLE_VALUE) { USB_NODE_INFORMATION NodeInfo = new USB_NODE_INFORMATION(); NodeInfo.NodeType = (int)USB_HUB_NODE.UsbHub; nBytes = Marshal.SizeOf(NodeInfo); IntPtr ptrNodeInfo = Marshal.AllocHGlobal(nBytes); Marshal.StructureToPtr(NodeInfo, ptrNodeInfo, true); // get the Hub Information if (DeviceIoControl(h2, IOCTL_USB_GET_NODE_INFORMATION, ptrNodeInfo, nBytes, ptrNodeInfo, nBytes, out nBytesReturned, IntPtr.Zero)) { NodeInfo = (USB_NODE_INFORMATION)Marshal.PtrToStructure(ptrNodeInfo, typeof(USB_NODE_INFORMATION)); Hub.HubIsBusPowered = Convert.ToBoolean(NodeInfo.HubInformation.HubIsBusPowered); Hub.HubPortCount = NodeInfo.HubInformation.HubDescriptor.bNumberOfPorts; } Marshal.FreeHGlobal(ptrNodeInfo); CloseHandle(h2); } // Fill in the missing Manufacture, Product, and SerialNumber values // values by just creating a Device instance and copying the values USBDevice Device = GetDevice(); Hub.HubInstanceID = Device.DeviceInstanceID; Hub.HubManufacturer = Device.Manufacturer; Hub.HubProduct = Device.Product; Hub.HubSerialNumber = Device.SerialNumber; Hub.HubDriverKey = Device.DriverKey; Marshal.FreeHGlobal(ptrNodeName); CloseHandle(h); } return Hub; }
public USBPort(USBHub hub, uint index) { rootHub = hub; portIndex = index; GetPortInformation(this); }
public USBController(string systemPath) { devicePath = systemPath; systemClass = GetHCDDriverKeyName(devicePath); rootHub = new USBHub(this); }
// return a down stream external hub public USBHub GetHub() { if (!PortIsHub) { return(null); } var Hub = new USBHub { HubIsRootHub = false, HubDeviceDesc = "External Hub" }; // Open a handle to the Host Controller var h = CreateFile(PortHubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); if (h.ToInt32() == INVALID_HANDLE_VALUE) { return(Hub); } // Get the DevicePath for downstream hub int nBytesReturned; var NodeName = new USB_NODE_CONNECTION_NAME { ConnectionIndex = PortPortNumber }; var nBytes = Marshal.SizeOf(NodeName); var ptrNodeName = Marshal.AllocHGlobal(nBytes); Marshal.StructureToPtr(NodeName, ptrNodeName, true); // Use an IOCTL call to request the Node Name if (DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_NAME, ptrNodeName, nBytes, ptrNodeName, nBytes, out nBytesReturned, IntPtr.Zero)) { NodeName = (USB_NODE_CONNECTION_NAME)Marshal.PtrToStructure(ptrNodeName, typeof(USB_NODE_CONNECTION_NAME)); Hub.HubDevicePath = @"\\.\" + NodeName.NodeName; } // Now let's open the Hub (based upon the HubName we got above) var h2 = CreateFile(Hub.HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); if (h2.ToInt32() != INVALID_HANDLE_VALUE) { var NodeInfo = new USB_NODE_INFORMATION { NodeType = (int)USB_HUB_NODE.UsbHub }; nBytes = Marshal.SizeOf(NodeInfo); var ptrNodeInfo = Marshal.AllocHGlobal(nBytes); Marshal.StructureToPtr(NodeInfo, ptrNodeInfo, true); // get the Hub Information if (DeviceIoControl(h2, IOCTL_USB_GET_NODE_INFORMATION, ptrNodeInfo, nBytes, ptrNodeInfo, nBytes, out nBytesReturned, IntPtr.Zero)) { NodeInfo = (USB_NODE_INFORMATION)Marshal.PtrToStructure(ptrNodeInfo, typeof(USB_NODE_INFORMATION)); Hub.HubIsBusPowered = Convert.ToBoolean(NodeInfo.HubInformation.HubIsBusPowered); Hub.HubPortCount = NodeInfo.HubInformation.HubDescriptor.bNumberOfPorts; } Marshal.FreeHGlobal(ptrNodeInfo); CloseHandle(h2); } // Fill in the missing Manufacture, Product, and SerialNumber values // values by just creating a Device instance and copying the values var Device = GetDevice(); Hub.HubInstanceID = Device.DeviceInstanceID; Hub.HubManufacturer = Device.Manufacturer; Hub.HubProduct = Device.Product; Hub.HubSerialNumber = Device.SerialNumber; Hub.HubDriverKey = Device.DriverKey; Marshal.FreeHGlobal(ptrNodeName); CloseHandle(h); return(Hub); }