Esempio n. 1
0
 internal unsafe VBoxUSB(USBRegistry devreg)
 {
     this.Registry = devreg;
     hDev          = Kernel32.CreateFile(devreg.DevicePath, Kernel32.GENERIC_READ | Kernel32.GENERIC_WRITE, Kernel32.FILE_SHARE_WRITE | Kernel32.FILE_SHARE_READ, IntPtr.Zero, Kernel32.OPEN_EXISTING, Kernel32.FILE_ATTRIBUTE_SYSTEM | Kernel32.FILE_FLAG_OVERLAPPED, IntPtr.Zero);
     if (hDev.IsInvalid)
     {
         throw new Win32Exception();
     }
     try {
         USBSUP_VERSION version = new USBSUP_VERSION();
         SyncIoControl(hDev, SUPUSB_IOCTL_GET_VERSION, null, 0, &version, sizeof(USBSUP_VERSION));
         if (version.u32Major != USBDRV_MAJOR_VERSION || version.u32Minor < USBDRV_MINOR_VERSION)
         {
             throw new InvalidOperationException("Unsupported USBDRV version");
         }
         USBSUP_CLAIMDEV claim = new USBSUP_CLAIMDEV()
         {
             bInterfaceNumber = 0
         };
         SyncIoControl(hDev, SUPUSB_IOCTL_USB_CLAIM_DEVICE, &claim, sizeof(USBSUP_CLAIMDEV), &claim, sizeof(USBSUP_CLAIMDEV));
         if (claim.fClaimed == 0)
         {
             throw new InvalidOperationException("Claim failed");
         }
     } catch {
         hDev.Close();
         throw;
     }
 }
Esempio n. 2
0
        internal LibUsb1Device(libusb_device device, LibUsb1Registry registry)
        {
            this.Device   = device;
            this.Registry = registry;
            int ret = libusb1.libusb_open(Device, out Handle);

            if (ret != 0)
            {
                throw new LibUsb1Exception("libusb_open", ret);
            }
        }
Esempio n. 3
0
        public WinUsbDevice(String path, WinUsbRegistry registry)
        {
            this.Registry = registry;
            DeviceHandle  = Kernel32.CreateFile(path,
                                                NativeFileAccess.FILE_GENERIC_WRITE | NativeFileAccess.FILE_GENERIC_READ,
                                                NativeFileShare.FILE_SHARE_WRITE | NativeFileShare.FILE_SHARE_READ,
                                                IntPtr.Zero,
                                                NativeFileMode.OPEN_EXISTING,
                                                NativeFileFlag.FILE_ATTRIBUTE_NORMAL | NativeFileFlag.FILE_FLAG_OVERLAPPED,
                                                IntPtr.Zero);
            if (DeviceHandle.IsInvalid || DeviceHandle.IsClosed)
            {
                throw new Win32Exception();
            }
            ThreadPool.BindHandle(DeviceHandle);
            SafeWinUsbInterfaceHandle InterfaceHandle;

            if (!WinUsb_Initialize(DeviceHandle, out InterfaceHandle))
            {
                throw new Win32Exception();
            }
            if (InterfaceHandle.IsInvalid || InterfaceHandle.IsClosed)
            {
                throw new Win32Exception();
            }
            InterfaceHandles = new SafeWinUsbInterfaceHandle[1] {
                InterfaceHandle
            };
            foreach (UsbInterfaceInfo ifinfo in UsbDeviceInfo.FromDevice(this).FindConfiguration(Configuration).Interfaces)
            {
                foreach (UsbEndpointDescriptor epinfo in ifinfo.Endpoints)
                {
                    int epidx = epinfo.EndpointAddress & 0x7F;
                    if ((epinfo.EndpointAddress & 0x80) != 0)
                    {
                        if (EndpointToInterfaceIn.Length <= epidx)
                        {
                            Array.Resize(ref EndpointToInterfaceIn, epidx + 1);
                        }
                        EndpointToInterfaceIn[epidx] = ifinfo.Descriptor.InterfaceNumber;
                    }
                    else
                    {
                        if (EndpointToInterfaceOut.Length <= epidx)
                        {
                            Array.Resize(ref EndpointToInterfaceOut, epidx + 1);
                        }
                        EndpointToInterfaceOut[epidx] = ifinfo.Descriptor.InterfaceNumber;
                    }
                }
            }
        }
Esempio n. 4
0
 public LibUsb0Device(String path, LibUsb0Registry registry)
 {
     DeviceFilename = path;
     this.Registry  = registry;
     DeviceHandle   = Kernel32.CreateFile(DeviceFilename,
                                          NativeFileAccess.SPECIAL,
                                          NativeFileShare.NONE,
                                          IntPtr.Zero,
                                          NativeFileMode.OPEN_EXISTING,
                                          NativeFileFlag.FILE_FLAG_OVERLAPPED,
                                          IntPtr.Zero);
     if (DeviceHandle.IsInvalid || DeviceHandle.IsClosed)
     {
         throw new Win32Exception();
     }
     ThreadPool.BindHandle(DeviceHandle);
 }
Esempio n. 5
0
 public USBIODevice(String path, USBIORegistry registry)
 {
     DeviceFilename = path;
     this.Registry  = registry;
     DeviceHandle   = OpenHandle();
 }