Files
DP44/Common/DTS.Common.IConnection/USBConnection/WINUSBConnection/WINUSBDeviceApi.cs
2026-04-17 14:55:32 -04:00

133 lines
5.2 KiB
C#

using System;
using Microsoft.Win32.SafeHandles;
using System.Runtime.InteropServices;
namespace DTS.Common.WINUSBConnection
{
/// <summary>
/// These declarations are translated from the C declarations in various files
/// in the Windows DDK. The files are:
///
/// winddk\6001\inc\api\usb.h
/// winddk\6001\inc\api\usb100.h
/// winddk\6001\inc\api\winusbio.h
///
/// (your home directory and release number may vary)
/// </summary>
internal sealed partial class WinUsbDevice
{
internal const uint DEVICE_SPEED = 1;
internal const byte USB_ENDPOINT_DIRECTION_MASK = 0X80;
internal enum PolicyType
{
ShortPacketTerminate = 1,
AutoClearStall,
PipeTransferTimeout,
IgnoreShortPackets,
AllowPartialReads,
AutoFlush,
RawIO,
}
internal enum USBDPipeTypes
{
UsbdPipeTypeControl,
UsbdPipeTypeIsochronous,
UsbdPipeTypeBulk,
UsbdPipeTypeInterrupt,
}
internal enum USBDeviceSpeeds
{
UsbLowSpeed = 1,
UsbFullSpeed,
UsbHighSpeed,
}
[StructLayout(LayoutKind.Sequential)]
internal struct USBConfigurationDescriptor
{
internal byte bLength;
internal byte bDescriptorType;
internal ushort wTotalLength;
internal byte bNumInterfaces;
internal byte bConfigurationValue;
internal byte iConfiguration;
internal byte bmAttributes;
internal byte MaxPower;
}
[StructLayout(LayoutKind.Sequential)]
internal struct USBInterfaceDescriptor
{
internal byte bLength;
internal byte bDescriptorType;
internal byte bInterfaceNumber;
internal byte bAlternateSetting;
internal byte bNumEndpoints;
internal byte bInterfaceClass;
internal byte bInterfaceSubClass;
internal byte bInterfaceProtocol;
internal byte iInterface;
}
[StructLayout(LayoutKind.Sequential)]
internal struct WinUSBPipeInformation
{
internal USBDPipeTypes PipeTypes;
internal byte PipeId;
internal ushort MaximumPacketSize;
internal byte Interval;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct WinUSBSetupPacket
{
internal byte RequestType;
internal byte Request;
internal ushort Value;
internal ushort Index;
internal ushort Length;
}
[DllImport("winusb.dll", SetLastError = true)]
internal static extern bool WinUsb_ControlTransfer(IntPtr interfaceHandle, WinUSBSetupPacket setupPacket, byte[] buffer, uint bufferLength, ref uint lengthTransferred, IntPtr overlapped);
[DllImport("winusb.dll", SetLastError = true)]
internal static extern bool WinUsb_Free(IntPtr interfaceHandle);
[DllImport("winusb.dll", SetLastError = true)]
internal static extern bool WinUsb_Initialize(SafeFileHandle deviceHandle, ref IntPtr interfaceHandle);
// Use this declaration to retrieve DEVICE_SPEED (the only currently defined InformationType).
[DllImport("winusb.dll", SetLastError = true)]
internal static extern bool WinUsb_QueryDeviceInformation(IntPtr interfaceHandle, uint informationType, ref uint bufferLength, ref byte buffer);
[DllImport("winusb.dll", SetLastError = true)]
internal static extern bool WinUsb_QueryInterfaceSettings(IntPtr interfaceHandle, byte alternateInterfaceNumber, ref USBInterfaceDescriptor usbAltInterfaceDescriptor);
[DllImport("winusb.dll", SetLastError = true)]
internal static extern bool WinUsb_QueryPipe(IntPtr interfaceHandle, byte alternateInterfaceNumber, byte pipeIndex, ref WinUSBPipeInformation pipeInformation);
[DllImport("winusb.dll", SetLastError = true)]
internal static extern bool WinUsb_ReadPipe(IntPtr interfaceHandle, byte pipeId, byte[] buffer, uint bufferLength, ref uint lengthTransferred, IntPtr overlapped);
// Two declarations for WinUsb_SetPipePolicy.
// Use this one when the returned Value is a Byte (all except PIPE_TRANSFER_TIMEOUT):
[DllImport("winusb.dll", SetLastError = true)]
internal static extern bool WinUsb_SetPipePolicy(IntPtr interfaceHandle, byte pipeId, uint policyType, uint valueLength, ref byte Value);
// Use this alias when the returned Value is a UInt32 (PIPE_TRANSFER_TIMEOUT only):
[DllImport("winusb.dll", SetLastError = true, EntryPoint = "WinUsb_SetPipePolicy")]
internal static extern bool WinUsb_SetPipePolicy1(IntPtr interfaceHandle, byte pipeId, uint policyType, uint valueLength, ref uint Value);
[DllImport("winusb.dll", SetLastError = true)]
internal static extern bool WinUsb_WritePipe(IntPtr interfaceHandle, Byte pipeId, byte[] buffer, uint bufferLength, ref uint lengthTransferred, IntPtr overlapped);
}
}