Esempio n. 1
0
        private void GetUsbNodeInformation(IntPtr hubHandle)
        {
            var usbNodeInformation =
                new UsbIoControl.UsbNodeInformation {
                NodeType = UsbIoControl.UsbHubNode.UsbHub
            };

            int    nBytes      = usbNodeInformation.SizeOf;
            IntPtr ptrNodeInfo = IntPtr.Zero;

            try
            {
                ptrNodeInfo = Marshal.AllocHGlobal(nBytes);
                usbNodeInformation.MarshalTo(ptrNodeInfo, true);

                if (KernelApi.DeviceIoControl(hubHandle, UsbIoControl.IoctlUsbGetNodeInformation, ptrNodeInfo, nBytes, ptrNodeInfo,
                                              nBytes, out _, IntPtr.Zero))
                {
                    usbNodeInformation.MarshalFrom(ptrNodeInfo);
                    NodeInformation = usbNodeInformation;
                }
                else
                {
                    CoreTraceSource.Source.TraceEvent(TraceEventType.Error, CoreTraceSource.UsbHubSourceId,
                                                      $"[{nameof(KernelApi.DeviceIoControl)}] [{nameof(UsbIoControl.IoctlUsbGetNodeInformation)}] Result: [{KernelApi.GetLastError():X}]");
                }
            }
            finally
            {
                if (ptrNodeInfo != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(ptrNodeInfo);
                }
            }
        }
Esempio n. 2
0
        private void GetRootHubName(IntPtr hostControllerHandle)
        {
            UsbApi.UsbRootHubName rootHubName = new UsbApi.UsbRootHubName();
            int    nBytes         = Marshal.SizeOf(rootHubName);
            IntPtr ptrRootHubName = IntPtr.Zero;

            try
            {
                ptrRootHubName = Marshal.AllocHGlobal(nBytes);

                // Get the root hub name.
                if (KernelApi.DeviceIoControl(hostControllerHandle, UsbIoControl.IoctlUsbGetRootHubName, ptrRootHubName, nBytes,
                                              ptrRootHubName, nBytes, out _, IntPtr.Zero))
                {
                    rootHubName = (UsbApi.UsbRootHubName)Marshal.PtrToStructure(ptrRootHubName, typeof(UsbApi.UsbRootHubName));

                    if (rootHubName.ActualLength > 0)
                    {
                        IsRootHub         = true;
                        DeviceDescription = "RootHub";
                        DevicePath        = @"\\?\" + rootHubName.RootHubName;
                    }
                }
                else
                {
                    CoreTraceSource.Source.TraceEvent(TraceEventType.Error, CoreTraceSource.UsbHubSourceId,
                                                      $"[{nameof(KernelApi.DeviceIoControl)}] [{nameof(UsbIoControl.IoctlUsbGetRootHubName)}] Result: [{KernelApi.GetLastError():X}]");
                }
            }
            finally
            {
                if (ptrRootHubName != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(ptrRootHubName);
                }
            }
        }
Esempio n. 3
0
        private void GetHubCapabilities(IntPtr hubHandle)
        {
            UsbApi.UsbHubCapabilitiesEx usbHubCapabilitiesEx = new UsbApi.UsbHubCapabilitiesEx();
            int    nBytes = Marshal.SizeOf(usbHubCapabilitiesEx);
            IntPtr ptrUsbHubCapabilitiesEx = IntPtr.Zero;

            try
            {
                ptrUsbHubCapabilitiesEx = Marshal.AllocHGlobal(nBytes);
                Marshal.StructureToPtr(usbHubCapabilitiesEx, ptrUsbHubCapabilitiesEx, true);

                if (KernelApi.DeviceIoControl(hubHandle, UsbIoControl.IoctlUsbGetHubCapabilitiesEx, ptrUsbHubCapabilitiesEx, nBytes,
                                              ptrUsbHubCapabilitiesEx, nBytes, out _, IntPtr.Zero))
                {
                    UsbHubCapabilitiesEx =
                        (UsbApi.UsbHubCapabilitiesEx)Marshal.PtrToStructure(ptrUsbHubCapabilitiesEx,
                                                                            typeof(UsbApi.UsbHubCapabilitiesEx));
                }
                else
                {
                    CoreTraceSource.Source.TraceEvent(TraceEventType.Error, CoreTraceSource.UsbHubSourceId,
                                                      $"[{nameof(KernelApi.DeviceIoControl)}] [{nameof(UsbIoControl.IoctlUsbGetHubCapabilitiesEx)}] Result: [{KernelApi.GetLastError():X}]");
                }
            }
            finally
            {
                if (ptrUsbHubCapabilitiesEx != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(ptrUsbHubCapabilitiesEx);
                }
            }
        }
Esempio n. 4
0
        private void GetUsbHubInformation(IntPtr hubHandle)
        {
            UsbIoControl.UsbHubInformationEx hubInfoEx =
                new UsbIoControl.UsbHubInformationEx();
            int    nBytes     = hubInfoEx.SizeOf;
            IntPtr ptrHubInfo = IntPtr.Zero;

            try
            {
                ptrHubInfo = Marshal.AllocHGlobal(nBytes);

                if (KernelApi.DeviceIoControl(hubHandle, UsbIoControl.IoctlUsbGetHubInformationEx, ptrHubInfo, nBytes, ptrHubInfo,
                                              nBytes, out _, IntPtr.Zero))
                {
                    hubInfoEx.MarshalFrom(ptrHubInfo);
                    HubInformation = hubInfoEx;
                }
                else
                {
                    CoreTraceSource.Source.TraceEvent(TraceEventType.Error, CoreTraceSource.UsbHubSourceId,
                                                      $"[{nameof(KernelApi.DeviceIoControl)}] [{nameof(UsbIoControl.IoctlUsbGetNodeInformation)}] Result: [{KernelApi.GetLastError():X}]");
                }
            }
            finally
            {
                if (ptrHubInfo != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(ptrHubInfo);
                }
            }
        }
Esempio n. 5
0
        private static void GetNodeConnectionInformationExV2(uint portCount, IntPtr deviceHandle,
                                                             out UsbIoControl.UsbNodeConnectionInformationExV2 usbNodeConnectionInformationExV2)
        {
            IntPtr structPtr = IntPtr.Zero;

            try
            {
                int bytesRequested = Marshal.SizeOf(typeof(UsbIoControl.UsbNodeConnectionInformationExV2));

                structPtr = Marshal.AllocHGlobal(bytesRequested);
                usbNodeConnectionInformationExV2 =
                    new UsbIoControl.UsbNodeConnectionInformationExV2
                {
                    ConnectionIndex       = portCount,
                    SupportedUsbProtocols = UsbIoControl.UsbProtocols.Usb300,
                    Length = (uint)bytesRequested
                };

                Marshal.StructureToPtr(usbNodeConnectionInformationExV2, structPtr, true);

                if (KernelApi.DeviceIoControl(deviceHandle, UsbIoControl.IoctlUsbGetNodeConnectionInformationExV2,
                                              structPtr, bytesRequested, structPtr, bytesRequested, out _, IntPtr.Zero))
                {
                    usbNodeConnectionInformationExV2 =
                        (UsbIoControl.UsbNodeConnectionInformationExV2)Marshal.PtrToStructure(structPtr,
                                                                                              typeof(UsbIoControl.UsbNodeConnectionInformationExV2));
                }
                else
                {
                    CoreTraceSource.Source.TraceEvent(TraceEventType.Error, CoreTraceSource.DeviceFactorySourceId,
                                                      $"[{nameof(KernelApi.DeviceIoControl)}] [{nameof(UsbIoControl.IoctlUsbGetNodeConnectionInformationExV2)}] Result: [{KernelApi.GetLastError():X}]");
                }
            }
            finally
            {
                if (structPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(structPtr);
                }
            }
        }
Esempio n. 6
0
        private static bool GetUsbNodeConnectionName(uint portCount, IntPtr deviceHandle,
                                                     out UsbIoControl.UsbNodeConnectionName nodeConnectionName)
        {
            IntPtr structPtr = IntPtr.Zero;

            try
            {
                int bytesRequested = Marshal.SizeOf(typeof(UsbIoControl.UsbNodeConnectionName));

                structPtr          = Marshal.AllocHGlobal(bytesRequested);
                nodeConnectionName = new UsbIoControl.UsbNodeConnectionName()
                {
                    ConnectionIndex = portCount
                };

                Marshal.StructureToPtr(nodeConnectionName, structPtr, true);

                if (KernelApi.DeviceIoControl(deviceHandle, UsbIoControl.IoctlUsbGetNodeConnectionName,
                                              structPtr, bytesRequested, structPtr, bytesRequested, out _, IntPtr.Zero))
                {
                    nodeConnectionName =
                        (UsbIoControl.UsbNodeConnectionName)Marshal.PtrToStructure(structPtr,
                                                                                   typeof(UsbIoControl.UsbNodeConnectionName));
                }
                else
                {
                    CoreTraceSource.Source.TraceEvent(TraceEventType.Error, CoreTraceSource.DeviceFactorySourceId,
                                                      $"[{nameof(KernelApi.DeviceIoControl)}] [{nameof(UsbIoControl.IoctlUsbGetNodeConnectionName)}] Result: [{KernelApi.GetLastError():X}]");

                    return(false);
                }
            }
            finally
            {
                if (structPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(structPtr);
                }
            }

            return(true);
        }
Esempio n. 7
0
        private static void GetUsbPortConnectorProperties(uint portCount, IntPtr deviceHandle,
                                                          out UsbIoControl.UsbPortConnectorProperties usbPortConnectorProperties)
        {
            IntPtr structPtr = IntPtr.Zero;

            try
            {
                int bytesRequested = Marshal.SizeOf(typeof(UsbIoControl.UsbPortConnectorProperties));

                structPtr = Marshal.AllocHGlobal(bytesRequested);
                usbPortConnectorProperties =
                    new UsbIoControl.UsbPortConnectorProperties()
                {
                    ConnectionIndex = portCount
                };
                Marshal.StructureToPtr(usbPortConnectorProperties, structPtr, true);

                if (KernelApi.DeviceIoControl(deviceHandle, UsbIoControl.IoctlUsbGetPortConnectorProperties,
                                              structPtr, bytesRequested, structPtr, bytesRequested, out _, IntPtr.Zero))
                {
                    usbPortConnectorProperties =
                        (UsbIoControl.UsbPortConnectorProperties)Marshal.PtrToStructure(structPtr,
                                                                                        typeof(UsbIoControl.UsbPortConnectorProperties));
                }
                else
                {
                    CoreTraceSource.Source.TraceEvent(TraceEventType.Error, CoreTraceSource.DeviceFactorySourceId,
                                                      $"[{nameof(KernelApi.DeviceIoControl)}] [{nameof(UsbIoControl.IoctlUsbGetPortConnectorProperties)}] Result: [{KernelApi.GetLastError():X}]");
                }
            }
            finally
            {
                if (structPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(structPtr);
                }
            }
        }
        private void GetHostControllerPowerMap(IntPtr handle)
        {
            UsbUser.WdmusbPowerState powerState = UsbUser.WdmusbPowerState.WdmUsbPowerSystemWorking;

            for (; powerState <= UsbUser.WdmusbPowerState.WdmUsbPowerSystemShutdown; powerState++)
            {
                UsbUser.UsbuserPowerInfoRequest powerInfoRequest = new UsbUser.UsbuserPowerInfoRequest
                {
                    Header =
                    {
                        UsbUserRequest = UsbUser.UsbuserGetPowerStateMap,
                    },
                    PowerInformation = { SystemState = powerState }
                };

                powerInfoRequest.Header.RequestBufferLength = (uint)Marshal.SizeOf(powerInfoRequest);

                IntPtr ptrPowerInfoRequest = IntPtr.Zero;

                try
                {
                    //
                    // Now query USBHUB for the USB_POWER_INFO structure for this hub.
                    // For Selective Suspend support
                    //
                    int nBytes = Marshal.SizeOf(powerInfoRequest);
                    ptrPowerInfoRequest = Marshal.AllocHGlobal(nBytes);

                    Marshal.StructureToPtr(powerInfoRequest, ptrPowerInfoRequest, true);

                    var success = KernelApi.DeviceIoControl(handle,
                                                            UsbUser.IoctlUsbUserRequest,
                                                            ptrPowerInfoRequest,
                                                            nBytes,
                                                            ptrPowerInfoRequest,
                                                            nBytes,
                                                            out _,
                                                            IntPtr.Zero);

                    if (!success)
                    {
                        CoreTraceSource.Source.TraceEvent(TraceEventType.Error, CoreTraceSource.UsbControllerSourceId,
                                                          $"[{nameof(KernelApi.DeviceIoControl)}] Returned Error Code: [{KernelApi.GetLastError():X}]");
                    }
                    else
                    {
                        powerInfoRequest = (UsbUser.UsbuserPowerInfoRequest)Marshal.PtrToStructure(ptrPowerInfoRequest,
                                                                                                   typeof(UsbUser.UsbuserPowerInfoRequest));
                        PowerInfo.Add(powerInfoRequest.PowerInformation);
                    }
                }
                finally
                {
                    if (ptrPowerInfoRequest != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(ptrPowerInfoRequest);
                    }
                }
            }
        }
        private void GetHostControllerInfo(IntPtr handle)
        {
            IntPtr ptrUsbControllerInfo0 = IntPtr.Zero;

            try
            {
                // set the header and request sizes
                UsbUser.UsbuserControllerInfo0 usbControllerInfo0 =
                    new UsbUser.UsbuserControllerInfo0 {
                    Header = { UsbUserRequest = UsbUser.UsbuserGetControllerInfo0 }
                };

                usbControllerInfo0.Header.RequestBufferLength = (uint)Marshal.SizeOf(usbControllerInfo0);

                //
                // Query for the USB_CONTROLLER_INFO_0 structure
                //
                int bytesRequested = Marshal.SizeOf(usbControllerInfo0);

                ptrUsbControllerInfo0 = Marshal.AllocHGlobal(bytesRequested);
                Marshal.StructureToPtr(usbControllerInfo0, ptrUsbControllerInfo0, true);

                if (KernelApi.DeviceIoControl(handle,
                                              UsbUser.IoctlUsbUserRequest,
                                              ptrUsbControllerInfo0,
                                              bytesRequested,
                                              ptrUsbControllerInfo0,
                                              bytesRequested,
                                              out _,
                                              IntPtr.Zero))
                {
                    CoreTraceSource.Source.TraceEvent(TraceEventType.Error, CoreTraceSource.UsbControllerSourceId,
                                                      $"[{nameof(KernelApi.DeviceIoControl)}] [{nameof(UsbUser.IoctlUsbUserRequest)}] Result: [{KernelApi.GetLastError():X}]");
                }
                else
                {
                    usbControllerInfo0 = (UsbUser.UsbuserControllerInfo0)Marshal.PtrToStructure(ptrUsbControllerInfo0,
                                                                                                typeof(UsbUser.UsbuserControllerInfo0));
                    ControllerInfo = usbControllerInfo0.Info0;
                }
            }
            finally
            {
                if (ptrUsbControllerInfo0 != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(ptrUsbControllerInfo0);
                }
            }
        }