Esempio n. 1
0
        private bool InitializeDevice()
        {
            try
            {
                //Try to initialize the device
                if (!WinUsb_Initialize(FileHandle, out WinUsbHandle))
                {
                    Debug.WriteLine("Failed to initialize winusb device: " + DevicePath);
                    Initialized = false;
                    return(false);
                }

                WINUSB_PIPE_INFORMATION  pipeInformation     = new WINUSB_PIPE_INFORMATION();
                USB_INTERFACE_DESCRIPTOR interfaceDescriptor = new USB_INTERFACE_DESCRIPTOR();

                if (WinUsb_QueryInterfaceSettings(WinUsbHandle, 0, ref interfaceDescriptor))
                {
                    for (byte i = 0; i < interfaceDescriptor.bNumEndpoints; i++)
                    {
                        WinUsb_QueryPipe(WinUsbHandle, 0, i, ref pipeInformation);
                        if (pipeInformation.PipeType == USBD_PIPE_TYPE.Bulk && UsbEndpointDirectionIn(pipeInformation.PipeId))
                        {
                            BulkIn = pipeInformation.PipeId;
                            WinUsb_FlushPipe(WinUsbHandle, BulkIn);
                        }
                        else if (pipeInformation.PipeType == USBD_PIPE_TYPE.Bulk && UsbEndpointDirectionOut(pipeInformation.PipeId))
                        {
                            BulkOut = pipeInformation.PipeId;
                            WinUsb_FlushPipe(WinUsbHandle, BulkOut);
                        }
                        else if (pipeInformation.PipeType == USBD_PIPE_TYPE.Interrupt && UsbEndpointDirectionIn(pipeInformation.PipeId))
                        {
                            IntIn = pipeInformation.PipeId;
                            WinUsb_FlushPipe(WinUsbHandle, IntIn);
                        }
                        else if (pipeInformation.PipeType == USBD_PIPE_TYPE.Interrupt && UsbEndpointDirectionOut(pipeInformation.PipeId))
                        {
                            IntOut = pipeInformation.PipeId;
                            WinUsb_FlushPipe(WinUsbHandle, IntOut);
                        }
                    }
                    //Debug.WriteLine("Initialized winusb device: " + DevicePath);
                    Initialized = true;
                    return(true);
                }
                //Debug.WriteLine("Failed to initialize winusb device: " + DevicePath);
                Initialized = false;
                return(false);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed to initialize winusb device: " + ex.Message);
                Initialized = false;
                return(false);
            }
        }
Esempio n. 2
0
        protected virtual bool InitializeDevice()
        {
            try
            {
                var ifaceDescriptor = new USB_INTERFACE_DESCRIPTOR();
                var pipeInfo        = new WINUSB_PIPE_INFORMATION();

                if (Usb.QueryInterfaceSettings(_winUsbHandle, 0, ref ifaceDescriptor))
                {
                    for (var i = 0; i < ifaceDescriptor.bNumEndpoints; i++)
                    {
                        Usb.QueryPipe(_winUsbHandle, 0, Convert.ToByte(i), ref pipeInfo);

                        if (((pipeInfo.PipeType == USBD_PIPE_TYPE.UsbdPipeTypeBulk) &
                             UsbEndpointDirectionIn(pipeInfo.PipeId)))
                        {
                            BulkIn = pipeInfo.PipeId;
                            Usb.FlushPipe(_winUsbHandle, BulkIn);
                        }
                        else if (((pipeInfo.PipeType == USBD_PIPE_TYPE.UsbdPipeTypeBulk) &
                                  UsbEndpointDirectionOut(pipeInfo.PipeId)))
                        {
                            BulkOut = pipeInfo.PipeId;
                            Usb.FlushPipe(_winUsbHandle, BulkOut);
                        }
                        else if ((pipeInfo.PipeType == USBD_PIPE_TYPE.UsbdPipeTypeInterrupt) &
                                 UsbEndpointDirectionIn(pipeInfo.PipeId))
                        {
                            IntIn = pipeInfo.PipeId;
                            Usb.FlushPipe(_winUsbHandle, IntIn);
                        }
                        else if ((pipeInfo.PipeType == USBD_PIPE_TYPE.UsbdPipeTypeInterrupt) &
                                 UsbEndpointDirectionOut(pipeInfo.PipeId))
                        {
                            IntOut = pipeInfo.PipeId;
                            Usb.FlushPipe(_winUsbHandle, IntOut);
                        }
                    }

                    return(true);
                }

                return(false);
            }
            catch (Exception ex)
            {
                Log.ErrorFormat("{0} {1}", ex.HelpLink, ex.Message);
                throw;
            }
        }
Esempio n. 3
0
        public static USBPipeInformation WinUsb_QueryPipe(IntPtr InterfaceHandle, byte AlternateInterfaceNumber, byte PipeIndex)
        {
            WINUSB_PIPE_INFORMATION PipeInformation = new WINUSB_PIPE_INFORMATION();

            if (NativeMethods.WinUsb_QueryPipe(InterfaceHandle, AlternateInterfaceNumber, PipeIndex, ref PipeInformation) == false)
            {
                return(null);
            }
            return(new USBPipeInformation()
            {
                PipeType = (USBPipeType)PipeInformation.PipeType,
                PipeId = PipeInformation.PipeId,
                MaximumPacketSize = PipeInformation.MaximumPacketSize,
                Interval = PipeInformation.Interval
            });
        }
Esempio n. 4
0
        public bool FindPipeAndInterface(UsbK usb,
                                         out USB_INTERFACE_DESCRIPTOR interfaceDescriptor,
                                         out WINUSB_PIPE_INFORMATION pipeInfo)
        {
            if (FindPipeAndInterface(usb,
                                     out interfaceDescriptor,
                                     out pipeInfo,
                                     AltInterfaceId,
                                     PipeId))
            {
                AltInterfaceId = interfaceDescriptor.bAlternateSetting;
                PipeId         = pipeInfo.PipeId;

                return(true);
            }
            return(false);
        }
Esempio n. 5
0
        protected virtual Boolean InitializeDevice()
        {
            try
            {
                USB_INTERFACE_DESCRIPTOR ifaceDescriptor = new USB_INTERFACE_DESCRIPTOR();
                WINUSB_PIPE_INFORMATION  pipeInfo        = new WINUSB_PIPE_INFORMATION();

                if (WinUsb_QueryInterfaceSettings(m_WinUsbHandle, 0, ref ifaceDescriptor))
                {
                    for (Int32 i = 0; i < ifaceDescriptor.bNumEndpoints; i++)
                    {
                        WinUsb_QueryPipe(m_WinUsbHandle, 0, System.Convert.ToByte(i), ref pipeInfo);

                        if (((pipeInfo.PipeType == USBD_PIPE_TYPE.UsbdPipeTypeBulk) & UsbEndpointDirectionIn(pipeInfo.PipeId)))
                        {
                            m_BulkIn = pipeInfo.PipeId;
                            WinUsb_FlushPipe(m_WinUsbHandle, m_BulkIn);
                        }
                        else if (((pipeInfo.PipeType == USBD_PIPE_TYPE.UsbdPipeTypeBulk) & UsbEndpointDirectionOut(pipeInfo.PipeId)))
                        {
                            m_BulkOut = pipeInfo.PipeId;
                            WinUsb_FlushPipe(m_WinUsbHandle, m_BulkOut);
                        }
                        else if ((pipeInfo.PipeType == USBD_PIPE_TYPE.UsbdPipeTypeInterrupt) & UsbEndpointDirectionIn(pipeInfo.PipeId))
                        {
                            m_IntIn = pipeInfo.PipeId;
                            WinUsb_FlushPipe(m_WinUsbHandle, m_IntIn);
                        }
                        else if ((pipeInfo.PipeType == USBD_PIPE_TYPE.UsbdPipeTypeInterrupt) & UsbEndpointDirectionOut(pipeInfo.PipeId))
                        {
                            m_IntOut = pipeInfo.PipeId;
                            WinUsb_FlushPipe(m_WinUsbHandle, m_IntOut);
                        }
                    }

                    return(true);
                }

                return(false);
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0} {1}", ex.HelpLink, ex.Message);
                throw;
            }
        }
Esempio n. 6
0
        public static bool FindPipeAndInterface(UsbK usb,
                                                out USB_INTERFACE_DESCRIPTOR interfaceDescriptor,
                                                out WINUSB_PIPE_INFORMATION pipeInfo,
                                                int altInterfaceId,
                                                int pipeId)
        {
            byte interfaceIndex = 0;

            interfaceDescriptor = new USB_INTERFACE_DESCRIPTOR();
            pipeInfo            = new WINUSB_PIPE_INFORMATION();
            while (usb.SelectInterface(interfaceIndex,
                                       true))
            {
                byte altSettingNumber = 0;
                while (usb.QueryInterfaceSettings(altSettingNumber,
                                                  out interfaceDescriptor))
                {
                    if (altInterfaceId == -1 || altInterfaceId == altSettingNumber)
                    {
                        byte pipeIndex = 0;
                        while (usb.QueryPipe(altSettingNumber,
                                             pipeIndex++,
                                             out pipeInfo))
                        {
                            if ((pipeInfo.MaximumPacketSize > 0) &&
                                pipeId == -1 || pipeInfo.PipeId == pipeId ||
                                ((pipeId & 0xF) == 0 && ((pipeId & 0x80) == (pipeInfo.PipeId & 0x80))))
                            {
                                goto FindInterfaceDone;
                            }
                            pipeInfo.PipeId = 0;
                        }
                    }
                    altSettingNumber++;
                }
                interfaceIndex++;
            }

FindInterfaceDone:
            return(pipeInfo.PipeId != 0);
        }
Esempio n. 7
0
        public ReadIsoTransferQueue(UsbK usb, ref WINUSB_PIPE_INFORMATION pipeInfo, int maxPendingTransfers, int numberOfPackets)
        {
            PipeInfo       = pipeInfo;
            Usb            = usb;
            OvlPool        = new OvlK(usb.Handle, maxPendingTransfers, KOVL_POOL_FLAG.NONE);
            DataBufferSize = (pipeInfo.MaximumPacketSize * numberOfPackets);
            for (int pos = 0; pos < maxPendingTransfers; pos++)
            {
                IsoTransferItem isoTransferItem = new IsoTransferItem();

                isoTransferItem.Buffer   = new byte[pipeInfo.MaximumPacketSize * numberOfPackets];
                isoTransferItem.BufferGC = GCHandle.Alloc(isoTransferItem.Buffer, GCHandleType.Pinned);

                isoTransferItem.Iso = new IsoK(numberOfPackets, 0);
                isoTransferItem.Iso.SetPackets(pipeInfo.MaximumPacketSize);

                OvlPool.Acquire(out isoTransferItem.Ovl);

                Master.AddLast(isoTransferItem);
                Completed.AddLast(isoTransferItem);
            }
        }
        public void SetupEndPoints()
        {
            USB_INTERFACE_DESCRIPTOR usbInterfaceDescriptor = new USB_INTERFACE_DESCRIPTOR();
            if (!WinUsb_QueryInterfaceSettings(m_usbInferface,
                                            0,
                                            usbInterfaceDescriptor))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            for (int i = 0; i < usbInterfaceDescriptor.bNumEndpoints; i++)
            {
                WINUSB_PIPE_INFORMATION pipeInfo = new WINUSB_PIPE_INFORMATION();
                if (!WinUsb_QueryPipe(m_usbInferface,
                                 0,
                                 (sbyte)i,
                                 pipeInfo))
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }

                if (pipeInfo.pipeType == USBD_PIPE_TYPE.UsbdPipeTypeInterrupt)
                {
                    m_pipeID = pipeInfo.pipeId;
                }

            }

            if (m_pipeID == 0)
            {
                throw new Exception("interupt pipe no found");
            }
        }
Esempio n. 9
0
 protected static extern Boolean WinUsb_QueryPipe(IntPtr InterfaceHandle, Byte AlternateInterfaceNumber, Byte PipeIndex, ref WINUSB_PIPE_INFORMATION PipeInformation);
Esempio n. 10
0
        protected virtual Boolean InitializeDevice() 
        {
            try
            {
                USB_INTERFACE_DESCRIPTOR ifaceDescriptor = new USB_INTERFACE_DESCRIPTOR();
                WINUSB_PIPE_INFORMATION  pipeInfo = new WINUSB_PIPE_INFORMATION();

                if (WinUsb_QueryInterfaceSettings(m_WinUsbHandle, 0, ref ifaceDescriptor))
                {
                    for (Int32 i = 0; i < ifaceDescriptor.bNumEndpoints; i++)
                    {
                        WinUsb_QueryPipe(m_WinUsbHandle, 0, System.Convert.ToByte(i), ref pipeInfo);

                        if (((pipeInfo.PipeType == USBD_PIPE_TYPE.UsbdPipeTypeBulk) & UsbEndpointDirectionIn(pipeInfo.PipeId)))
                        {
                            m_BulkIn = pipeInfo.PipeId;
                            WinUsb_FlushPipe(m_WinUsbHandle, m_BulkIn);
                        }
                        else if (((pipeInfo.PipeType == USBD_PIPE_TYPE.UsbdPipeTypeBulk) & UsbEndpointDirectionOut(pipeInfo.PipeId)))
                        {
                            m_BulkOut = pipeInfo.PipeId;
                            WinUsb_FlushPipe(m_WinUsbHandle, m_BulkOut);
                        }
                        else if ((pipeInfo.PipeType == USBD_PIPE_TYPE.UsbdPipeTypeInterrupt) & UsbEndpointDirectionIn(pipeInfo.PipeId))
                        {
                            m_IntIn = pipeInfo.PipeId;
                            WinUsb_FlushPipe(m_WinUsbHandle, m_IntIn);
                        }
                        else if ((pipeInfo.PipeType == USBD_PIPE_TYPE.UsbdPipeTypeInterrupt) & UsbEndpointDirectionOut(pipeInfo.PipeId))
                        {
                            m_IntOut = pipeInfo.PipeId;
                            WinUsb_FlushPipe(m_WinUsbHandle, m_IntOut);
                        }
                    }

                    return true;
                }

                return false;
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0} {1}", ex.HelpLink, ex.Message);
                throw;
            }
        }
Esempio n. 11
0
 public static extern bool WinUsb_QueryPipe(IntPtr interfaceHandle, byte alternateInterfaceNumber, byte PipeIndex, ref WINUSB_PIPE_INFORMATION pipeInformation);
Esempio n. 12
0
   public static extern bool WinUsb_QueryPipe(IntPtr InterfaceHandle, Byte AlternateInterfaceNumber, Byte PipeIndex,
 out WINUSB_PIPE_INFORMATION PipeInformation);
Esempio n. 13
0
        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);
        }
Esempio n. 14
0
 private static extern bool WinUsb_QueryPipe(
     IntPtr usbInferfaceHandle,
     sbyte alternateSettingNumber,
     sbyte pipeIndex,
     WINUSB_PIPE_INFORMATION usbAltInterfaceDescriptor);
Esempio n. 15
0
 public static extern bool WinUsb_QueryPipe(IntPtr InterfaceHandle, Byte AlternateInterfaceNumber, Byte PipeIndex,
                                            out WINUSB_PIPE_INFORMATION PipeInformation);
Esempio n. 16
0
        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;
        }
Esempio n. 17
0
        public static bool FindPipeAndInterface(UsbK usb,
                                                out USB_INTERFACE_DESCRIPTOR interfaceDescriptor,
                                                out WINUSB_PIPE_INFORMATION pipeInfo,
                                                int altInterfaceId,
                                                int pipeId)
        {
            byte interfaceIndex = 0;
            interfaceDescriptor = new USB_INTERFACE_DESCRIPTOR();
            pipeInfo = new WINUSB_PIPE_INFORMATION();
            while (usb.SelectInterface(interfaceIndex,
                                       true))
            {
                byte altSettingNumber = 0;
                while (usb.QueryInterfaceSettings(altSettingNumber,
                                                  out interfaceDescriptor))
                {
                    if (altInterfaceId == -1 || altInterfaceId == altSettingNumber)
                    {
                        byte pipeIndex = 0;
                        while (usb.QueryPipe(altSettingNumber,
                                             pipeIndex++,
                                             out pipeInfo))
                        {
                            if ((pipeInfo.MaximumPacketSize > 0) &&
                                pipeId == -1 || pipeInfo.PipeId == pipeId ||
                                ((pipeId & 0xF) == 0 && ((pipeId & 0x80) == (pipeInfo.PipeId & 0x80))))
                            {
                                goto FindInterfaceDone;
                            }
                            pipeInfo.PipeId = 0;
                        }
                    }
                    altSettingNumber++;
                }
                interfaceIndex++;
            }

            FindInterfaceDone:
            return pipeInfo.PipeId != 0;
        }
Esempio n. 18
0
        public bool FindPipeAndInterface(UsbK usb,
                                         out USB_INTERFACE_DESCRIPTOR interfaceDescriptor,
                                         out WINUSB_PIPE_INFORMATION pipeInfo)
        {
            if (FindPipeAndInterface(usb,
                                     out interfaceDescriptor,
                                     out pipeInfo,
                                     AltInterfaceId,
                                     PipeId))
            {
                AltInterfaceId = interfaceDescriptor.bAlternateSetting;
                PipeId = pipeInfo.PipeId;

                return true;
            }
            return false;
        }
Esempio n. 19
0
 public static unsafe extern bool WinUsb_QueryPipe(IntPtr hInterface, byte AlternateInterfaceNumber, byte PipeIndex, ref WINUSB_PIPE_INFORMATION PipeInformation);
Esempio n. 20
0
 internal static extern Boolean WinUsb_QueryPipe(SafeWinUsbHandle InterfaceHandle, Byte AlternateInterfaceNumber, Byte PipeIndex, ref WINUSB_PIPE_INFORMATION PipeInformation);
Esempio n. 21
0
 public static bool QueryPipe(
     WinUsbHandle InterfaceHandle, Byte AlternateInterfaceNumber, Byte PipeIndex, out WINUSB_PIPE_INFORMATION PipeInformation)
 {
     return(WinUsb_QueryPipe(InterfaceHandle, AlternateInterfaceNumber, PipeIndex, out PipeInformation));
 }
Esempio n. 22
0
 protected static extern Boolean WinUsb_QueryPipe(IntPtr InterfaceHandle, Byte AlternateInterfaceNumber, Byte PipeIndex, ref WINUSB_PIPE_INFORMATION PipeInformation);
Esempio n. 23
0
        /// <summary>
        ///  Initializes a device interface and obtains information about it.
        /// </summary>
        /// 
        ///  <returns> True on success. </returns>
        internal Boolean InitializeDevice()
        {
            WINUSB_PIPE_INFORMATION pipeInfo = new WINUSB_PIPE_INFORMATION();
            Boolean success;

            usbInterfaceDescriptor = default(USB_INTERFACE_DESCRIPTOR);

            try
            {
                // Let's try to initialize the device. If it works we can access the devica via winUsbHandle
                success = WinUsb_Initialize(devInfo.deviceHandle, ref devInfo.winUsbHandle);

                if (success)
                {
                    // Lets request an interface descriptor.
                    success = WinUsb_QueryInterfaceSettings(devInfo.winUsbHandle, 0, ref usbInterfaceDescriptor);

                    if (success)
                    {
                        // For each endpoint we can query the pipe to learn the endpoint's transfer type & direction and save it to devInfo.
                        for (Int32 i = 0; i <= (usbInterfaceDescriptor.bNumEndpoints - 1); i++)
                        {
                            WinUsb_QueryPipe(devInfo.winUsbHandle, 0, System.Convert.ToByte(i), ref pipeInfo);

                            if (((pipeInfo.PipeType == USBD_PIPE_TYPE.UsbdPipeTypeBulk) & UsbEndpointDirectionIn(pipeInfo.PipeId)))
                            {
                                devInfo.bulkInPipe = pipeInfo.PipeId;
                                SetPipePolicy(devInfo.bulkInPipe, Convert.ToUInt32(POLICY_TYPE.SHORT_PACKET_TERMINATE), Convert.ToByte(false));
                                SetPipePolicy(devInfo.bulkInPipe, Convert.ToUInt32(POLICY_TYPE.AUTO_CLEAR_STALL), Convert.ToByte(false));
                                SetPipePolicy(devInfo.bulkInPipe, Convert.ToUInt32(POLICY_TYPE.PIPE_TRANSFER_TIMEOUT), Convert.ToUInt32(pipeTimeout));
                                SetPipePolicy(devInfo.bulkInPipe, Convert.ToUInt32(POLICY_TYPE.IGNORE_SHORT_PACKETS), Convert.ToByte(false));
                                SetPipePolicy(devInfo.bulkInPipe, Convert.ToUInt32(POLICY_TYPE.ALLOW_PARTIAL_READS), Convert.ToByte(true));
                                SetPipePolicy(devInfo.bulkInPipe, Convert.ToUInt32(POLICY_TYPE.AUTO_FLUSH), Convert.ToByte(false));
                                SetPipePolicy(devInfo.bulkInPipe, Convert.ToUInt32(POLICY_TYPE.RAW_IO), Convert.ToByte(false));
                                SetPipePolicy(devInfo.bulkInPipe, Convert.ToUInt32(POLICY_TYPE.RESET_PIPE_ON_RESUME), Convert.ToByte(false));
                            }
                            else if (((pipeInfo.PipeType == USBD_PIPE_TYPE.UsbdPipeTypeBulk) & UsbEndpointDirectionOut(pipeInfo.PipeId)))
                            {
                                devInfo.bulkOutPipe = pipeInfo.PipeId;
                                SetPipePolicy(devInfo.bulkOutPipe, Convert.ToUInt32(POLICY_TYPE.SHORT_PACKET_TERMINATE), Convert.ToByte(false));
                                SetPipePolicy(devInfo.bulkOutPipe, Convert.ToUInt32(POLICY_TYPE.AUTO_CLEAR_STALL), Convert.ToByte(false));
                                SetPipePolicy(devInfo.bulkOutPipe, Convert.ToUInt32(POLICY_TYPE.PIPE_TRANSFER_TIMEOUT), Convert.ToUInt32(pipeTimeout));
                                SetPipePolicy(devInfo.bulkOutPipe, Convert.ToUInt32(POLICY_TYPE.IGNORE_SHORT_PACKETS), Convert.ToByte(false));
                                SetPipePolicy(devInfo.bulkOutPipe, Convert.ToUInt32(POLICY_TYPE.ALLOW_PARTIAL_READS), Convert.ToByte(true));
                                SetPipePolicy(devInfo.bulkOutPipe, Convert.ToUInt32(POLICY_TYPE.AUTO_FLUSH), Convert.ToByte(false));
                                SetPipePolicy(devInfo.bulkOutPipe, Convert.ToUInt32(POLICY_TYPE.RAW_IO), Convert.ToByte(false));
                                SetPipePolicy(devInfo.bulkOutPipe, Convert.ToUInt32(POLICY_TYPE.RESET_PIPE_ON_RESUME), Convert.ToByte(false));
                            }
                            else if ((pipeInfo.PipeType == USBD_PIPE_TYPE.UsbdPipeTypeInterrupt) & UsbEndpointDirectionIn(pipeInfo.PipeId))
                            {
                                devInfo.interruptInPipe = pipeInfo.PipeId;
                                SetPipePolicy(devInfo.interruptInPipe, Convert.ToUInt32(POLICY_TYPE.SHORT_PACKET_TERMINATE), Convert.ToByte(false));
                                SetPipePolicy(devInfo.interruptInPipe, Convert.ToUInt32(POLICY_TYPE.AUTO_CLEAR_STALL), Convert.ToByte(false));
                                SetPipePolicy(devInfo.interruptInPipe, Convert.ToUInt32(POLICY_TYPE.PIPE_TRANSFER_TIMEOUT), Convert.ToUInt32(pipeTimeout));
                                SetPipePolicy(devInfo.interruptInPipe, Convert.ToUInt32(POLICY_TYPE.IGNORE_SHORT_PACKETS), Convert.ToByte(false));
                                SetPipePolicy(devInfo.interruptInPipe, Convert.ToUInt32(POLICY_TYPE.ALLOW_PARTIAL_READS), Convert.ToByte(true));
                                SetPipePolicy(devInfo.interruptInPipe, Convert.ToUInt32(POLICY_TYPE.AUTO_FLUSH), Convert.ToByte(false));
                                SetPipePolicy(devInfo.interruptInPipe, Convert.ToUInt32(POLICY_TYPE.RAW_IO), Convert.ToByte(false));
                                SetPipePolicy(devInfo.interruptInPipe, Convert.ToUInt32(POLICY_TYPE.RESET_PIPE_ON_RESUME), Convert.ToByte(false));
                            }
                            else if ((pipeInfo.PipeType == USBD_PIPE_TYPE.UsbdPipeTypeInterrupt) & UsbEndpointDirectionOut(pipeInfo.PipeId))
                            {
                                devInfo.interruptOutPipe = pipeInfo.PipeId;
                                SetPipePolicy(devInfo.interruptOutPipe, Convert.ToUInt32(POLICY_TYPE.SHORT_PACKET_TERMINATE), Convert.ToByte(false));
                                SetPipePolicy(devInfo.interruptOutPipe, Convert.ToUInt32(POLICY_TYPE.AUTO_CLEAR_STALL), Convert.ToByte(false));
                                SetPipePolicy(devInfo.interruptOutPipe, Convert.ToUInt32(POLICY_TYPE.PIPE_TRANSFER_TIMEOUT), Convert.ToUInt32(pipeTimeout));
                                SetPipePolicy(devInfo.interruptOutPipe, Convert.ToUInt32(POLICY_TYPE.IGNORE_SHORT_PACKETS), Convert.ToByte(false));
                                SetPipePolicy(devInfo.interruptOutPipe, Convert.ToUInt32(POLICY_TYPE.PIPE_TRANSFER_TIMEOUT), Convert.ToByte(true));
                                SetPipePolicy(devInfo.interruptOutPipe, Convert.ToUInt32(POLICY_TYPE.AUTO_FLUSH), Convert.ToByte(false));
                                SetPipePolicy(devInfo.interruptOutPipe, Convert.ToUInt32(POLICY_TYPE.RAW_IO), Convert.ToByte(false));
                                SetPipePolicy(devInfo.interruptOutPipe, Convert.ToUInt32(POLICY_TYPE.RESET_PIPE_ON_RESUME), Convert.ToByte(false));
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }

            return success;
        }