21
|
1 using System; |
|
2 using System.Runtime.InteropServices; |
|
3 |
|
4 namespace UCIS.USBLib.Internal.Windows { |
|
5 class UsbApi { |
|
6 //public const int INVALID_HANDLE_VALUE = -1; |
|
7 |
|
8 public const int USBUSER_GET_CONTROLLER_INFO_0 = 0x00000001; |
|
9 public const int USBUSER_GET_CONTROLLER_DRIVER_KEY = 0x00000002; |
|
10 |
|
11 public const int IOCTL_GET_HCD_DRIVERKEY_NAME = 0x220424; |
|
12 public const int IOCTL_USB_GET_ROOT_HUB_NAME = 0x220408; |
|
13 public const int IOCTL_USB_GET_NODE_INFORMATION = 0x220408; |
|
14 public const int IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX = 0x220448; |
|
15 public const int IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION = 0x220410; |
|
16 public const int IOCTL_USB_GET_NODE_CONNECTION_NAME = 0x220414; |
|
17 public const int IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME = 0x220420; |
|
18 public const int IOCTL_STORAGE_GET_DEVICE_NUMBER = 0x2D1080; |
|
19 |
|
20 public const int USB_DEVICE_DESCRIPTOR_TYPE = 0x1; |
|
21 public const int USB_CONFIGURATION_DESCRIPTOR_TYPE = 0x2; |
|
22 public const int USB_STRING_DESCRIPTOR_TYPE = 0x3; |
|
23 public const int USB_INTERFACE_DESCRIPTOR_TYPE = 0x4; |
|
24 public const int USB_ENDPOINT_DESCRIPTOR_TYPE = 0x5; |
|
25 |
|
26 public const string GUID_DEVINTERFACE_HUBCONTROLLER = "3abf6f2d-71c4-462a-8a92-1e6861e6af27"; |
|
27 public const string GUID_DEVINTERFACE_USB_HOST_CONTROLLER = "{3ABF6F2D-71C4-462A-8A92-1E6861E6AF27}"; |
|
28 public const string GUID_DEVINTERFACE_USB_HUB = "{F18A0E88-C30C-11D0-8815-00A0C906BED8}"; |
|
29 public const string GUID_DEVINTERFACE_USB_DEVICE = "{A5DCBF10-6530-11D2-901F-00C04FB951ED}"; |
|
30 |
|
31 public const int MAX_BUFFER_SIZE = 2048; |
|
32 public const int MAXIMUM_USB_STRING_LENGTH = 255; |
|
33 public const string REGSTR_KEY_USB = "USB"; |
|
34 public const int REG_SZ = 1; |
|
35 public const int DIF_PROPERTYCHANGE = 0x00000012; |
|
36 public const int DICS_FLAG_GLOBAL = 0x00000001; |
|
37 |
|
38 |
|
39 //public const int SPDRP_DRIVER = 0x9; |
|
40 //public const int SPDRP_DEVICEDESC = 0x0; |
|
41 |
|
42 public const int DICS_ENABLE = 0x00000001; |
|
43 public const int DICS_DISABLE = 0x00000002; |
|
44 |
|
45 //#endregion |
|
46 } |
|
47 |
|
48 #region enumerations |
|
49 |
|
50 enum UsbDeviceClass : byte { |
|
51 UnspecifiedDevice = 0x00, |
|
52 AudioInterface = 0x01, |
|
53 CommunicationsAndCDCControlBoth = 0x02, |
|
54 HIDInterface = 0x03, |
|
55 PhysicalInterfaceDevice = 0x5, |
|
56 ImageInterface = 0x06, |
|
57 PrinterInterface = 0x07, |
|
58 MassStorageInterface = 0x08, |
|
59 HubDevice = 0x09, |
|
60 CDCDataInterface = 0x0A, |
|
61 SmartCardInterface = 0x0B, |
|
62 ContentSecurityInterface = 0x0D, |
|
63 VidioInterface = 0x0E, |
|
64 PersonalHeathcareInterface = 0x0F, |
|
65 DiagnosticDeviceBoth = 0xDC, |
|
66 WirelessControllerInterface = 0xE0, |
|
67 MiscellaneousBoth = 0xEF, |
|
68 ApplicationSpecificInterface = 0xFE, |
|
69 VendorSpecificBoth = 0xFF |
|
70 } |
|
71 |
|
72 enum HubCharacteristics : byte { |
|
73 GangedPowerSwitching = 0x00, |
|
74 IndividualPotPowerSwitching = 0x01, |
|
75 // to do |
|
76 } |
|
77 |
|
78 enum USB_HUB_NODE { |
|
79 UsbHub, |
|
80 UsbMIParent |
|
81 } |
|
82 |
|
83 enum USB_DESCRIPTOR_TYPE : byte { |
|
84 DeviceDescriptorType = 0x1, |
|
85 ConfigurationDescriptorType = 0x2, |
|
86 StringDescriptorType = 0x3, |
|
87 InterfaceDescriptorType = 0x4, |
|
88 EndpointDescriptorType = 0x5, |
|
89 HubDescriptor = 0x29 |
|
90 } |
|
91 |
|
92 [Flags] |
|
93 enum USB_CONFIGURATION : byte { |
|
94 RemoteWakeUp = 32, |
|
95 SelfPowered = 64, |
|
96 BusPowered = 128, |
|
97 RemoteWakeUp_BusPowered = 160, |
|
98 RemoteWakeUp_SelfPowered = 96 |
|
99 } |
|
100 |
|
101 enum USB_TRANSFER : byte { |
|
102 Control = 0x0, |
|
103 Isochronous = 0x1, |
|
104 Bulk = 0x2, |
|
105 Interrupt = 0x3 |
|
106 } |
|
107 |
|
108 enum USB_CONNECTION_STATUS : int { |
|
109 NoDeviceConnected, |
|
110 DeviceConnected, |
|
111 DeviceFailedEnumeration, |
|
112 DeviceGeneralFailure, |
|
113 DeviceCausedOvercurrent, |
|
114 DeviceNotEnoughPower, |
|
115 DeviceNotEnoughBandwidth, |
|
116 DeviceHubNestedTooDeeply, |
|
117 DeviceInLegacyHub |
|
118 } |
|
119 |
|
120 enum USB_DEVICE_SPEED : byte { |
|
121 UsbLowSpeed = 0, |
|
122 UsbFullSpeed, |
|
123 UsbHighSpeed |
|
124 } |
|
125 |
|
126 [Flags] |
|
127 enum DeviceInterfaceDataFlags : uint { |
|
128 Unknown = 0x00000000, |
|
129 Active = 0x00000001, |
|
130 Default = 0x00000002, |
|
131 Removed = 0x00000004 |
|
132 } |
|
133 |
|
134 [Flags] |
|
135 enum HubPortStatus : short { |
|
136 Connection = 0x0001, |
|
137 Enabled = 0x0002, |
|
138 Suspend = 0x0004, |
|
139 OverCurrent = 0x0008, |
|
140 BeingReset = 0x0010, |
|
141 Power = 0x0100, |
|
142 LowSpeed = 0x0200, |
|
143 HighSpeed = 0x0400, |
|
144 TestMode = 0x0800, |
|
145 Indicator = 0x1000, |
|
146 // these are the bits which cause the hub port state machine to keep moving |
|
147 //kHubPortStateChangeMask = kHubPortConnection | kHubPortEnabled | kHubPortSuspend | kHubPortOverCurrent | kHubPortBeingReset |
|
148 } |
|
149 |
|
150 enum HubStatus : byte { |
|
151 LocalPowerStatus = 1, |
|
152 OverCurrentIndicator = 2, |
|
153 LocalPowerStatusChange = 1, |
|
154 OverCurrentIndicatorChange = 2 |
|
155 } |
|
156 |
|
157 enum PortIndicatorSlectors : byte { |
|
158 IndicatorAutomatic = 0, |
|
159 IndicatorAmber, |
|
160 IndicatorGreen, |
|
161 IndicatorOff |
|
162 } |
|
163 |
|
164 enum PowerSwitching : byte { |
|
165 SupportsGangPower = 0, |
|
166 SupportsIndividualPortPower = 1, |
|
167 SetPowerOff = 0, |
|
168 SetPowerOn = 1 |
|
169 } |
|
170 |
|
171 #endregion |
|
172 |
|
173 #region structures |
|
174 |
|
175 [StructLayout(LayoutKind.Sequential)] |
|
176 struct SP_CLASSINSTALL_HEADER { |
|
177 public int cbSize; |
|
178 public int InstallFunction; //DI_FUNCTION InstallFunction; |
|
179 } |
|
180 |
|
181 [StructLayout(LayoutKind.Sequential)] |
|
182 struct SP_PROPCHANGE_PARAMS { |
|
183 public SP_CLASSINSTALL_HEADER ClassInstallHeader; |
|
184 public int StateChange; |
|
185 public int Scope; |
|
186 public int HwProfile; |
|
187 } |
|
188 |
|
189 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] |
|
190 struct USB_HCD_DRIVERKEY_NAME { |
|
191 public uint ActualLength; |
|
192 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = UsbApi.MAX_BUFFER_SIZE)] |
|
193 public string DriverKeyName; //WCHAR DriverKeyName[1]; |
|
194 } |
|
195 |
|
196 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] |
|
197 struct USB_ROOT_HUB_NAME { |
|
198 public uint ActualLength; |
|
199 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = UsbApi.MAX_BUFFER_SIZE)] |
|
200 public string RootHubName; //WCHAR RootHubName[1]; |
|
201 } |
|
202 |
|
203 [StructLayout(LayoutKind.Sequential, Pack = 1)] |
|
204 struct USB_HUB_DESCRIPTOR { |
|
205 public byte bDescriptorLength; |
|
206 public USB_DESCRIPTOR_TYPE bDescriptorType; |
|
207 public byte bNumberOfPorts; |
|
208 public ushort wHubCharacteristics; |
|
209 public byte bPowerOnToPowerGood; |
|
210 public byte bHubControlCurrent; |
|
211 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)] |
|
212 public byte[] bRemoveAndPowerMask; //UCHAR bRemoveAndPowerMask[64]; |
|
213 } |
|
214 |
|
215 [StructLayout(LayoutKind.Sequential)] |
|
216 struct USB_HUB_INFORMATION { |
|
217 public USB_HUB_DESCRIPTOR HubDescriptor; |
|
218 public bool HubIsBusPowered; |
|
219 } |
|
220 |
|
221 [StructLayout(LayoutKind.Sequential)] |
|
222 struct USB_NODE_INFORMATION { |
|
223 //public int NodeType; |
|
224 public USB_HUB_NODE NodeType; |
|
225 public USB_HUB_INFORMATION HubInformation; // union { USB_HUB_INFORMATION HubInformation; USB_MI_PARENT_INFORMATION MiParentInformation; } |
|
226 } |
|
227 |
|
228 [StructLayout(LayoutKind.Sequential, Pack = 1)] |
|
229 struct USB_NODE_CONNECTION_INFORMATION_EX { |
|
230 public uint ConnectionIndex; |
|
231 public USB_DEVICE_DESCRIPTOR DeviceDescriptor; |
|
232 public byte CurrentConfigurationValue; |
|
233 public USB_DEVICE_SPEED Speed; |
|
234 public byte DeviceIsHub; //BOOLEAN DeviceIsHub; |
|
235 public ushort DeviceAddress; |
|
236 public uint NumberOfOpenPipes; |
|
237 public USB_CONNECTION_STATUS ConnectionStatus; |
|
238 //public IntPtr PipeList; //USB_PIPE_INFO PipeList[0]; |
|
239 //[MarshalAs(UnmanagedType.ByValArray, SizeConst=100)] |
|
240 //Byte[] PipeList; |
|
241 } |
|
242 |
|
243 [StructLayout(LayoutKind.Sequential, Pack = 1)] |
|
244 struct USB_DESCRIPTOR { |
|
245 public byte bLength; |
|
246 public USB_DESCRIPTOR_TYPE bDescriptorType; |
|
247 } |
|
248 |
|
249 [StructLayout(LayoutKind.Sequential, Pack = 1)] |
|
250 class USB_DEVICE_DESCRIPTOR { |
|
251 public byte bLength; |
|
252 public USB_DESCRIPTOR_TYPE bDescriptorType; |
|
253 public ushort bcdUSB; |
|
254 public UsbDeviceClass bDeviceClass; |
|
255 public byte bDeviceSubClass; |
|
256 public byte bDeviceProtocol; |
|
257 public byte bMaxPacketSize0; |
|
258 public ushort idVendor; |
|
259 public ushort idProduct; |
|
260 public ushort bcdDevice; |
|
261 public byte iManufacturer; |
|
262 public byte iProduct; |
|
263 public byte iSerialNumber; |
|
264 public byte bNumConfigurations; |
|
265 } |
|
266 |
|
267 [StructLayout(LayoutKind.Sequential, Pack = 1)] |
|
268 struct USB_ENDPOINT_DESCRIPTOR { |
|
269 public byte bLength; |
|
270 public USB_DESCRIPTOR_TYPE bDescriptorType; |
|
271 public byte bEndpointAddress; |
|
272 public USB_TRANSFER bmAttributes; |
|
273 public short wMaxPacketSize; |
|
274 public byte bInterval; |
|
275 } |
|
276 |
|
277 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] |
|
278 struct USB_STRING_DESCRIPTOR { |
|
279 public byte bLength; |
|
280 public USB_DESCRIPTOR_TYPE bDescriptorType; |
|
281 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = UsbApi.MAXIMUM_USB_STRING_LENGTH)] |
|
282 public string bString; //WCHAR bString[1]; |
|
283 } |
|
284 |
|
285 [StructLayout(LayoutKind.Sequential, Pack = 1)] |
|
286 struct USB_INTERFACE_DESCRIPTOR { |
|
287 public byte bLength; |
|
288 public USB_DESCRIPTOR_TYPE bDescriptorType; |
|
289 public byte bInterfaceNumber; |
|
290 public byte bAlternateSetting; |
|
291 public byte bNumEndpoints; |
|
292 public byte bInterfaceClass; |
|
293 public byte bInterfaceSubClass; |
|
294 public byte bInterfaceProtocol; |
|
295 public byte Interface; |
|
296 } |
|
297 |
|
298 [StructLayout(LayoutKind.Sequential, Pack = 1)] |
|
299 struct USB_CONFIGURATION_DESCRIPTOR { |
|
300 public byte bLength; |
|
301 public USB_DESCRIPTOR_TYPE bDescriptorType; |
|
302 public ushort wTotalLength; |
|
303 public byte bNumInterface; |
|
304 public byte bConfigurationsValue; |
|
305 public byte iConfiguration; |
|
306 public USB_CONFIGURATION bmAttributes; |
|
307 public byte MaxPower; |
|
308 } |
|
309 |
|
310 [StructLayout(LayoutKind.Sequential)] |
|
311 struct HID_DESCRIPTOR_DESC_LIST { |
|
312 public byte bReportType; |
|
313 public short wReportLength; |
|
314 } |
|
315 |
|
316 [StructLayout(LayoutKind.Sequential, Pack = 1)] |
|
317 struct HID_DESCRIPTOR { |
|
318 public byte bLength; |
|
319 public USB_DESCRIPTOR_TYPE bDescriptorType; |
|
320 public ushort bcdHID; |
|
321 public byte bCountry; |
|
322 public byte bNumDescriptors; |
|
323 public HID_DESCRIPTOR_DESC_LIST hid_desclist; //DescriptorList [1]; |
|
324 } |
|
325 |
|
326 [StructLayout(LayoutKind.Sequential)] |
|
327 struct USB_SETUP_PACKET { |
|
328 public byte bmRequest; |
|
329 public byte bRequest; |
|
330 public ushort wValue; |
|
331 public ushort wIndex; |
|
332 public ushort wLength; |
|
333 } |
|
334 |
|
335 [StructLayout(LayoutKind.Sequential)] |
|
336 struct USB_DESCRIPTOR_REQUEST { |
|
337 public uint ConnectionIndex; |
|
338 public USB_SETUP_PACKET SetupPacket; |
|
339 //public byte[] Data; //UCHAR Data[0]; |
|
340 } |
|
341 |
|
342 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] |
|
343 struct USB_NODE_CONNECTION_NAME { |
|
344 public uint ConnectionIndex; |
|
345 public uint ActualLength; |
|
346 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = UsbApi.MAX_BUFFER_SIZE)] |
|
347 public string NodeName; //WCHAR NodeName[1]; |
|
348 } |
|
349 |
|
350 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] |
|
351 struct USB_NODE_CONNECTION_DRIVERKEY_NAME { |
|
352 public uint ConnectionIndex; |
|
353 public uint ActualLength; |
|
354 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = UsbApi.MAX_BUFFER_SIZE)] |
|
355 public string DriverKeyName; //WCHAR DriverKeyName[1]; |
|
356 } |
|
357 |
|
358 [StructLayout(LayoutKind.Sequential)] |
|
359 struct STORAGE_DEVICE_NUMBER { |
|
360 public int DeviceType; //DEVICE_TYPE DeviceType; |
|
361 public uint DeviceNumber; |
|
362 public uint PartitionNumber; |
|
363 } |
|
364 |
|
365 [StructLayout(LayoutKind.Sequential)] |
|
366 class SP_DEVINFO_DATA1 { |
|
367 public int cbSize; |
|
368 public Guid ClassGuid; |
|
369 public int DevInst; |
|
370 public ulong Reserved; |
|
371 }; |
|
372 |
|
373 [StructLayout(LayoutKind.Sequential)] |
|
374 class RAW_ROOTPORT_PARAMETERS { |
|
375 public ushort PortNumber; |
|
376 public ushort PortStatus; |
|
377 } |
|
378 |
|
379 [StructLayout(LayoutKind.Sequential)] |
|
380 class USB_UNICODE_NAME { |
|
381 public uint Length; |
|
382 public string str; //WCHAR String[1]; |
|
383 } |
|
384 |
|
385 #endregion |
|
386 } |