Ejemplo n.º 1
0
        /// <summary>
        /// Builds the device.
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <param name="portCount">The port count.</param>
        /// <param name="devicePath">The device path.</param>
        /// <returns>The device.</returns>
        public static Device BuildDevice(Device parent, uint portCount, string devicePath)
        {
            Device device = null;

            // Open a handle to the Hub device
            IntPtr deviceHandle = KernelApi.CreateFile(devicePath, UsbApi.GenericWrite, UsbApi.FileShareWrite,
                                                       IntPtr.Zero,
                                                       UsbApi.OpenExisting, 0, IntPtr.Zero);

            try
            {
                if (deviceHandle.ToInt64() != UsbApi.InvalidHandleValue)
                {
                    if (GetNodeConnectionInformationEx(portCount, deviceHandle, out var nodeConnection))
                    {
                        if (nodeConnection.ConnectionStatus == UsbIoControl.UsbConnectionStatus.DeviceConnected)
                        {
                            GetUsbPortConnectorProperties(portCount, deviceHandle,
                                                          out var portConnectorProperties);

                            GetNodeConnectionInformationExV2(portCount, deviceHandle, out var nodeConnectionV2);

                            if (nodeConnection.DeviceDescriptor.bDeviceClass == UsbDesc.DeviceClassType.UsbHubDevice)
                            {
                                if (GetUsbNodeConnectionName(portCount, deviceHandle, out var connectionName))
                                {
                                    string name = @"\\?\" + connectionName.NodeName;

                                    device = new UsbHub(nodeConnection.DeviceDescriptor, name)
                                    {
                                        NodeConnectionInfo         = nodeConnection,
                                        NodeConnectionInfoV2       = nodeConnectionV2,
                                        UsbPortConnectorProperties = portConnectorProperties
                                    };
                                }
                            }
                            else
                            {
                                device = new UsbDevice(nodeConnection.DeviceDescriptor, portCount, devicePath)
                                {
                                    NodeConnectionInfo         = nodeConnection,
                                    NodeConnectionInfoV2       = nodeConnectionV2,
                                    UsbPortConnectorProperties = portConnectorProperties
                                };
                            }
                        }
                        else
                        {
                            device = new UsbDevice(null, portCount)
                            {
                                NodeConnectionInfo = nodeConnection
                            };
                        }
                    }
                }
            }
            finally
            {
                if (deviceHandle.ToInt64() != UsbApi.InvalidHandleValue)
                {
                    KernelApi.CloseHandle(deviceHandle);
                }
            }

            return(device);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="UsbHub"/> class.
        /// </summary>
        /// <param name="deviceDescriptor">The device descriptor.</param>
        /// <param name="devicePath">The device path.</param>
        public UsbHub(UsbSpec.UsbDeviceDescriptor deviceDescriptor, string devicePath)
            : base(deviceDescriptor, 0, devicePath)
        {
            DeviceDescription = "Standard-USB-Hub";
            DevicePath        = devicePath;

            IntPtr hostControllerHandle = IntPtr.Zero;

            try
            {
                hostControllerHandle = KernelApi.CreateFile(devicePath, UsbApi.GenericWrite, UsbApi.FileShareWrite, IntPtr.Zero, UsbApi.OpenExisting, 0, IntPtr.Zero);

                if (hostControllerHandle.ToInt64() != UsbApi.InvalidHandleValue)
                {
                    GetRootHubName(hostControllerHandle);

                    // TODO: Get the driver key name for the root hub.

                    IntPtr hubHandle = IntPtr.Zero;

                    try
                    {
                        // Now let's open the hub (based upon the hub name we got above).
                        hubHandle = KernelApi.CreateFile(DevicePath, UsbApi.GenericWrite, UsbApi.FileShareWrite, IntPtr.Zero, UsbApi.OpenExisting, 0, IntPtr.Zero);

                        if (hubHandle.ToInt64() != UsbApi.InvalidHandleValue)
                        {
                            GetUsbNodeInformation(hubHandle);

                            GetUsbHubInformation(hubHandle);

                            GetHubCapabilities(hubHandle);
                        }
                    }
                    finally
                    {
                        if (hubHandle != IntPtr.Zero)
                        {
                            KernelApi.CloseHandle(hubHandle);
                        }
                    }
                }
                else
                {
                    throw new UsbHubException("No port found!");
                }
            }
            finally
            {
                if (hostControllerHandle != IntPtr.Zero)
                {
                    KernelApi.CloseHandle(hostControllerHandle);
                }
            }

            for (uint index = 1; index <= PortCount; index++)
            {
                // Initialize a new port and save the port.
                try
                {
                    Devices.Add(DeviceFactory.BuildDevice(this, index, DevicePath));
                }
                catch (Exception e)
                {
                    CoreTraceSource.Source.TraceEvent(TraceEventType.Error, CoreTraceSource.UsbHubSourceId,
                                                      "Unhandled exception occurred: {0}", e);
                }
            }
        }
Ejemplo n.º 3
0
        private void Initalise(uint index)
        {
            IntPtr ptr = IntPtr.Zero;
            IntPtr deviceInfoHandle = IntPtr.Zero;

            try
            {
                ptr = Marshal.AllocHGlobal(UsbApi.MaxBufferSize);
                deviceInfoHandle = UsbApi.SetupDiGetClassDevs(ref _guid, 0, IntPtr.Zero,
                                                              UsbApi.DigcfPresent | UsbApi.DigcfDeviceinterface);

                // Create a device interface data structure
                UsbApi.SpDeviceInterfaceData deviceInterfaceData = new UsbApi.SpDeviceInterfaceData();
                deviceInterfaceData.CbSize = Marshal.SizeOf(deviceInterfaceData);

                // Start the enumeration.
                Boolean success =
                    UsbApi.SetupDiEnumDeviceInterfaces(deviceInfoHandle, IntPtr.Zero, ref _guid, (int)index,
                                                       ref deviceInterfaceData);
                if (success)
                {
                    // Build a DevInfo data structure.
                    UsbApi.SpDevinfoData deviceInfoData = new UsbApi.SpDevinfoData();
                    deviceInfoData.CbSize = Marshal.SizeOf(deviceInfoData);

                    // Build a device interface detail data structure.
                    UsbApi.SpDeviceInterfaceDetailData deviceInterfaceDetailData =
                        new UsbApi.SpDeviceInterfaceDetailData
                    {
                        CbSize = UIntPtr.Size == 8 ? 8 : (int)(4 + (uint)Marshal.SystemDefaultCharSize)
                    };

                    // Now we can get some more detailed informations.
                    int nRequiredSize = 0;
                    int nBytes        = UsbApi.MaxBufferSize;

                    if (UsbApi.SetupDiGetDeviceInterfaceDetail(deviceInfoHandle, ref deviceInterfaceData,
                                                               ref deviceInterfaceDetailData, nBytes, ref nRequiredSize, ref deviceInfoData))
                    {
                        DevicePath = deviceInterfaceDetailData.DevicePath;

                        ParseDevicePath(DevicePath);

                        // Get the device description and driver key name.
                        int requiredSize = 0;
                        int regType      = UsbApi.RegSz;

                        if (UsbApi.SetupDiGetDeviceRegistryProperty(deviceInfoHandle, ref deviceInfoData,
                                                                    (int)UsbApi.Spdrp.SpdrpDevicedesc,
                                                                    ref regType,
                                                                    ptr,
                                                                    UsbApi.MaxBufferSize, ref requiredSize))
                        {
                            DeviceDescription = Marshal.PtrToStringAuto(ptr);
                        }

                        if (UsbApi.SetupDiGetDeviceRegistryProperty(
                                deviceInfoHandle,
                                ref deviceInfoData,
                                (int)UsbApi.Spdrp.SpdrpDriver,
                                ref regType,
                                ptr,
                                UsbApi.MaxBufferSize,
                                ref requiredSize))
                        {
                            DriverKey = Marshal.PtrToStringAuto(ptr);
                        }
                    }

                    IntPtr hostControllerHandle = IntPtr.Zero;

                    try
                    {
                        hostControllerHandle = KernelApi.CreateFile(deviceInterfaceDetailData.DevicePath,
                                                                    UsbApi.GenericWrite,
                                                                    UsbApi.FileShareWrite, IntPtr.Zero,
                                                                    UsbApi.OpenExisting, 0, IntPtr.Zero);

                        if (deviceInfoHandle.ToInt64() != UsbApi.InvalidHandleValue)
                        {
                            GetHostControllerPowerMap(hostControllerHandle);

                            GetHostControllerInfo(hostControllerHandle);
                        }
                    }
                    finally
                    {
                        if (hostControllerHandle != IntPtr.Zero ||
                            deviceInfoHandle.ToInt64() != UsbApi.InvalidHandleValue)
                        {
                            KernelApi.CloseHandle(hostControllerHandle);
                        }
                    }

                    try
                    {
                        Devices.Add(new UsbHub(null, DevicePath));
                    }
                    catch (Exception ex)
                    {
                        CoreTraceSource.Source.TraceEvent(TraceEventType.Error, CoreTraceSource.UsbControllerSourceId,
                                                          "Unhandled exception occurred: {0}", ex);

                        throw new UsbControllerException("Unhandled exception occurred", ex);
                    }
                }
                else
                {
                    throw new UsbControllerException("No usb controller found!");
                }
            }
            finally
            {
                if (ptr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(ptr);
                }

                if (deviceInfoHandle != IntPtr.Zero)
                {
                    UsbApi.SetupDiDestroyDeviceInfoList(deviceInfoHandle);
                }
            }
        }