21
|
1 ???using System; |
|
2 using System.Runtime.InteropServices; |
|
3 |
|
4 namespace UCIS.USBLib.Communication.LibUsb { |
22
|
5 static class UsbConstants { |
|
6 public const int DEFAULT_TIMEOUT = 1000; |
21
|
7 } |
|
8 [Flags] |
|
9 enum UsbEndpointDirection : byte { |
|
10 EndpointIn = 0x80, |
|
11 EndpointOut = 0x00, |
|
12 } |
|
13 [StructLayout(LayoutKind.Sequential, Pack = 1)] |
|
14 struct UsbKernelVersion { |
22
|
15 public bool IsEmpty { get { return Major == 0 && Minor == 0 && Micro == 0 && Nano == 0; } } |
|
16 public UsbKernelVersion(int major, int minor, int micro, int nano, int bcdLibUsbDotNetKernelMod) { |
21
|
17 Major = major; |
|
18 Minor = minor; |
|
19 Micro = micro; |
|
20 Nano = nano; |
|
21 BcdLibUsbDotNetKernelMod = bcdLibUsbDotNetKernelMod; |
|
22 } |
|
23 public readonly int Major; |
|
24 public readonly int Minor; |
|
25 public readonly int Micro; |
|
26 public readonly int Nano; |
|
27 public readonly int BcdLibUsbDotNetKernelMod; |
|
28 public override string ToString() { return string.Format("{0}.{1}.{2}.{3}", Major, Minor, Micro, Nano); } |
|
29 } |
|
30 [StructLayout(LayoutKind.Explicit, Pack = 1, Size = sizeof(int) * 6)] |
|
31 struct LibUsbRequest { |
|
32 public static int Size = Marshal.SizeOf(typeof(LibUsbRequest)); |
|
33 [FieldOffset(0)] |
22
|
34 public int Timeout; |
21
|
35 [FieldOffset(sizeof(int))] |
|
36 public Control Control; |
|
37 [FieldOffset(sizeof(int))] |
|
38 public Config Config; |
|
39 [FieldOffset(sizeof(int))] |
|
40 public Debug Debug; |
|
41 [FieldOffset(sizeof(int))] |
|
42 public Descriptor Descriptor; |
|
43 [FieldOffset(sizeof(int))] |
|
44 public Endpoint Endpoint; |
|
45 [FieldOffset(sizeof(int))] |
|
46 public Feature Feature; |
|
47 [FieldOffset(sizeof(int))] |
|
48 public Iface Iface; |
|
49 [FieldOffset(sizeof(int))] |
|
50 public Status Status; |
|
51 [FieldOffset(sizeof(int))] |
|
52 public Vendor Vendor; |
|
53 [FieldOffset(sizeof(int))] |
|
54 public UsbKernelVersion Version; |
|
55 [FieldOffset(sizeof(int))] |
|
56 public DeviceProperty DeviceProperty; |
|
57 [FieldOffset(sizeof(int))] |
|
58 public DeviceRegKey DeviceRegKey; |
|
59 [FieldOffset(sizeof(int))] |
|
60 public BusQueryID BusQueryID; |
|
61 } |
|
62 [StructLayout(LayoutKind.Sequential, Pack = 1)] |
|
63 struct Descriptor { |
|
64 public int Type; |
|
65 public int Index; |
|
66 public int LangID; |
|
67 public int Recipient; |
|
68 } |
|
69 [StructLayout(LayoutKind.Sequential, Pack = 1)] |
|
70 struct Config { |
|
71 public int ID; |
|
72 } |
|
73 [StructLayout(LayoutKind.Sequential, Pack = 1)] |
|
74 struct Control { |
|
75 public byte RequestType; |
|
76 public byte Request; |
|
77 public ushort Value; |
|
78 public ushort Index; |
|
79 public ushort Length; |
|
80 } |
|
81 [StructLayout(LayoutKind.Sequential, Pack = 1)] |
|
82 struct DeviceProperty { |
|
83 public int ID; |
|
84 } |
|
85 [StructLayout(LayoutKind.Sequential, Pack = 1)] |
|
86 struct Iface { |
|
87 public int ID; |
|
88 public int AlternateID; |
|
89 } |
|
90 [StructLayout(LayoutKind.Sequential, Pack = 1)] |
|
91 struct Endpoint { |
|
92 public int ID; |
|
93 public int PacketSize; |
|
94 } |
|
95 [StructLayout(LayoutKind.Sequential, Pack = 1)] |
|
96 struct Vendor { |
|
97 public int Type; |
|
98 public int Recipient; |
|
99 public int Request; |
|
100 public int ID; |
|
101 public int Index; |
|
102 } |
|
103 [StructLayout(LayoutKind.Sequential, Pack = 1)] |
|
104 struct Feature { |
|
105 public int Recipient; |
|
106 public int ID; |
|
107 public int Index; |
|
108 } |
|
109 [StructLayout(LayoutKind.Sequential, Pack = 1)] |
|
110 struct Status { |
|
111 public int Recipient; |
|
112 public int Index; |
|
113 public int ID; |
|
114 } |
|
115 [StructLayout(LayoutKind.Sequential, Pack = 1)] |
|
116 struct Debug { |
|
117 public int Level; |
|
118 } |
|
119 [StructLayout(LayoutKind.Sequential, Pack = 1)] |
|
120 struct DeviceRegKey { |
|
121 public int KeyType; |
|
122 public int NameOffset; |
|
123 public int ValueOffset; |
|
124 public int ValueLength; |
|
125 } |
|
126 [StructLayout(LayoutKind.Sequential, Pack = 1)] |
|
127 struct BusQueryID { |
|
128 public ushort IDType; |
|
129 } |
|
130 static class LibUsbIoCtl { |
|
131 private const int FILE_ANY_ACCESS = 0; |
|
132 private const int FILE_DEVICE_UNKNOWN = 0x00000022; |
|
133 |
|
134 private const int METHOD_BUFFERED = 0; |
|
135 private const int METHOD_IN_DIRECT = 1; |
|
136 private const int METHOD_OUT_DIRECT = 2; |
|
137 private const int METHOD_NEITHER = 3; |
|
138 |
|
139 public static readonly int ABORT_ENDPOINT = CTL_CODE(FILE_DEVICE_UNKNOWN, 0x80F, METHOD_BUFFERED, FILE_ANY_ACCESS); |
|
140 public static readonly int CLAIM_INTERFACE = CTL_CODE(FILE_DEVICE_UNKNOWN, 0x815, METHOD_BUFFERED, FILE_ANY_ACCESS); |
|
141 public static readonly int CLEAR_FEATURE = CTL_CODE(FILE_DEVICE_UNKNOWN, 0x806, METHOD_BUFFERED, FILE_ANY_ACCESS); |
|
142 public static readonly int CONTROL_TRANSFER = CTL_CODE(FILE_DEVICE_UNKNOWN, 0x903, METHOD_BUFFERED, FILE_ANY_ACCESS); |
|
143 |
|
144 public static readonly int GET_CONFIGURATION = CTL_CODE(FILE_DEVICE_UNKNOWN, 0x802, METHOD_BUFFERED, FILE_ANY_ACCESS); |
|
145 public static readonly int GET_CUSTOM_REG_PROPERTY = CTL_CODE(FILE_DEVICE_UNKNOWN, 0x901, METHOD_BUFFERED, FILE_ANY_ACCESS); |
|
146 public static readonly int GET_DESCRIPTOR = CTL_CODE(FILE_DEVICE_UNKNOWN, 0x809, METHOD_BUFFERED, FILE_ANY_ACCESS); |
|
147 public static readonly int GET_INTERFACE = CTL_CODE(FILE_DEVICE_UNKNOWN, 0x804, METHOD_BUFFERED, FILE_ANY_ACCESS); |
|
148 public static readonly int GET_STATUS = CTL_CODE(FILE_DEVICE_UNKNOWN, 0x807, METHOD_BUFFERED, FILE_ANY_ACCESS); |
|
149 public static readonly int GET_VERSION = CTL_CODE(FILE_DEVICE_UNKNOWN, 0x812, METHOD_BUFFERED, FILE_ANY_ACCESS); |
|
150 public static readonly int GET_REG_PROPERTY = CTL_CODE(FILE_DEVICE_UNKNOWN, 0x900, METHOD_BUFFERED, FILE_ANY_ACCESS); |
|
151 public static readonly int INTERRUPT_OR_BULK_READ = CTL_CODE(FILE_DEVICE_UNKNOWN, 0x80B, METHOD_OUT_DIRECT, FILE_ANY_ACCESS); |
|
152 public static readonly int INTERRUPT_OR_BULK_WRITE = CTL_CODE(FILE_DEVICE_UNKNOWN, 0x80A, METHOD_IN_DIRECT, FILE_ANY_ACCESS); |
|
153 public static readonly int ISOCHRONOUS_READ = CTL_CODE(FILE_DEVICE_UNKNOWN, 0x814, METHOD_OUT_DIRECT, FILE_ANY_ACCESS); |
|
154 public static readonly int ISOCHRONOUS_WRITE = CTL_CODE(FILE_DEVICE_UNKNOWN, 0x813, METHOD_IN_DIRECT, FILE_ANY_ACCESS); |
|
155 public static readonly int RELEASE_INTERFACE = CTL_CODE(FILE_DEVICE_UNKNOWN, 0x816, METHOD_BUFFERED, FILE_ANY_ACCESS); |
|
156 public static readonly int RESET_DEVICE = CTL_CODE(FILE_DEVICE_UNKNOWN, 0x810, METHOD_BUFFERED, FILE_ANY_ACCESS); |
|
157 public static readonly int RESET_ENDPOINT = CTL_CODE(FILE_DEVICE_UNKNOWN, 0x80E, METHOD_BUFFERED, FILE_ANY_ACCESS); |
|
158 public static readonly int SET_CONFIGURATION = CTL_CODE(FILE_DEVICE_UNKNOWN, 0x801, METHOD_BUFFERED, FILE_ANY_ACCESS); |
|
159 public static readonly int SET_DEBUG_LEVEL = CTL_CODE(FILE_DEVICE_UNKNOWN, 0x811, METHOD_BUFFERED, FILE_ANY_ACCESS); |
|
160 public static readonly int SET_DESCRIPTOR = CTL_CODE(FILE_DEVICE_UNKNOWN, 0x808, METHOD_BUFFERED, FILE_ANY_ACCESS); |
|
161 public static readonly int SET_FEATURE = CTL_CODE(FILE_DEVICE_UNKNOWN, 0x805, METHOD_BUFFERED, FILE_ANY_ACCESS); |
|
162 public static readonly int SET_INTERFACE = CTL_CODE(FILE_DEVICE_UNKNOWN, 0x803, METHOD_BUFFERED, FILE_ANY_ACCESS); |
|
163 public static readonly int VENDOR_READ = CTL_CODE(FILE_DEVICE_UNKNOWN, 0x80D, METHOD_BUFFERED, FILE_ANY_ACCESS); |
|
164 public static readonly int VENDOR_WRITE = CTL_CODE(FILE_DEVICE_UNKNOWN, 0x80C, METHOD_BUFFERED, FILE_ANY_ACCESS); |
|
165 |
|
166 private static int CTL_CODE(int DeviceType, int Function, int Method, int Access) { return ((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method); } |
|
167 } |
|
168 } |