Mercurial > hg > ucis.core
annotate USBLib/Internal/Windows/UsbApi.cs @ 62:edc41c861d96
Updates and cleanup in Windows USB enumeration code
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Sun, 13 Oct 2013 02:47:08 +0200 |
parents | fd63c453ff65 |
children | 309c705d7460 |
rev | line source |
---|---|
21 | 1 using System; |
2 using System.Runtime.InteropServices; | |
62
edc41c861d96
Updates and cleanup in Windows USB enumeration code
Ivo Smits <Ivo@UCIS.nl>
parents:
58
diff
changeset
|
3 using UCIS.USBLib.Communication; |
edc41c861d96
Updates and cleanup in Windows USB enumeration code
Ivo Smits <Ivo@UCIS.nl>
parents:
58
diff
changeset
|
4 using UCIS.USBLib.Communication.WinUsb; |
edc41c861d96
Updates and cleanup in Windows USB enumeration code
Ivo Smits <Ivo@UCIS.nl>
parents:
58
diff
changeset
|
5 using UCIS.USBLib.Descriptor; |
21 | 6 |
7 namespace UCIS.USBLib.Internal.Windows { | |
8 class UsbApi { | |
9 public const int IOCTL_GET_HCD_DRIVERKEY_NAME = 0x220424; | |
10 public const int IOCTL_USB_GET_ROOT_HUB_NAME = 0x220408; | |
11 public const int IOCTL_USB_GET_NODE_INFORMATION = 0x220408; | |
12 public const int IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX = 0x220448; | |
13 public const int IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION = 0x220410; | |
14 public const int IOCTL_USB_GET_NODE_CONNECTION_NAME = 0x220414; | |
15 public const int IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME = 0x220420; | |
16 public const int IOCTL_STORAGE_GET_DEVICE_NUMBER = 0x2D1080; | |
17 | |
18 public const string GUID_DEVINTERFACE_USB_HOST_CONTROLLER = "{3ABF6F2D-71C4-462A-8A92-1E6861E6AF27}"; | |
19 public const string GUID_DEVINTERFACE_USB_HUB = "{F18A0E88-C30C-11D0-8815-00A0C906BED8}"; | |
20 public const string GUID_DEVINTERFACE_USB_DEVICE = "{A5DCBF10-6530-11D2-901F-00C04FB951ED}"; | |
21 | |
22 public const int MAX_BUFFER_SIZE = 2048; | |
23 public const int DIF_PROPERTYCHANGE = 0x00000012; | |
24 public const int DICS_FLAG_GLOBAL = 0x00000001; | |
25 | |
26 public const int DICS_ENABLE = 0x00000001; | |
27 public const int DICS_DISABLE = 0x00000002; | |
28 } | |
29 | |
30 #region enumerations | |
31 | |
32 enum UsbDeviceClass : byte { | |
33 UnspecifiedDevice = 0x00, | |
34 AudioInterface = 0x01, | |
35 CommunicationsAndCDCControlBoth = 0x02, | |
36 HIDInterface = 0x03, | |
37 PhysicalInterfaceDevice = 0x5, | |
38 ImageInterface = 0x06, | |
39 PrinterInterface = 0x07, | |
40 MassStorageInterface = 0x08, | |
41 HubDevice = 0x09, | |
42 CDCDataInterface = 0x0A, | |
43 SmartCardInterface = 0x0B, | |
44 ContentSecurityInterface = 0x0D, | |
45 VidioInterface = 0x0E, | |
46 PersonalHeathcareInterface = 0x0F, | |
47 DiagnosticDeviceBoth = 0xDC, | |
48 WirelessControllerInterface = 0xE0, | |
49 MiscellaneousBoth = 0xEF, | |
50 ApplicationSpecificInterface = 0xFE, | |
51 VendorSpecificBoth = 0xFF | |
52 } | |
53 | |
54 enum HubCharacteristics : byte { | |
55 GangedPowerSwitching = 0x00, | |
56 IndividualPotPowerSwitching = 0x01, | |
57 // to do | |
58 } | |
59 | |
60 enum USB_HUB_NODE { | |
61 UsbHub, | |
62 UsbMIParent | |
63 } | |
64 | |
65 enum USB_CONNECTION_STATUS : int { | |
66 NoDeviceConnected, | |
67 DeviceConnected, | |
68 DeviceFailedEnumeration, | |
69 DeviceGeneralFailure, | |
70 DeviceCausedOvercurrent, | |
71 DeviceNotEnoughPower, | |
72 DeviceNotEnoughBandwidth, | |
73 DeviceHubNestedTooDeeply, | |
74 DeviceInLegacyHub | |
75 } | |
76 | |
77 enum USB_DEVICE_SPEED : byte { | |
78 UsbLowSpeed = 0, | |
79 UsbFullSpeed, | |
80 UsbHighSpeed | |
81 } | |
82 | |
83 [Flags] | |
84 enum DeviceInterfaceDataFlags : uint { | |
85 Unknown = 0x00000000, | |
86 Active = 0x00000001, | |
87 Default = 0x00000002, | |
88 Removed = 0x00000004 | |
89 } | |
90 | |
91 [Flags] | |
92 enum HubPortStatus : short { | |
93 Connection = 0x0001, | |
94 Enabled = 0x0002, | |
95 Suspend = 0x0004, | |
96 OverCurrent = 0x0008, | |
97 BeingReset = 0x0010, | |
98 Power = 0x0100, | |
99 LowSpeed = 0x0200, | |
100 HighSpeed = 0x0400, | |
101 TestMode = 0x0800, | |
102 Indicator = 0x1000, | |
103 // these are the bits which cause the hub port state machine to keep moving | |
104 //kHubPortStateChangeMask = kHubPortConnection | kHubPortEnabled | kHubPortSuspend | kHubPortOverCurrent | kHubPortBeingReset | |
105 } | |
106 | |
107 enum HubStatus : byte { | |
108 LocalPowerStatus = 1, | |
109 OverCurrentIndicator = 2, | |
110 LocalPowerStatusChange = 1, | |
111 OverCurrentIndicatorChange = 2 | |
112 } | |
113 | |
114 enum PortIndicatorSlectors : byte { | |
115 IndicatorAutomatic = 0, | |
116 IndicatorAmber, | |
117 IndicatorGreen, | |
118 IndicatorOff | |
119 } | |
120 | |
121 enum PowerSwitching : byte { | |
122 SupportsGangPower = 0, | |
123 SupportsIndividualPortPower = 1, | |
124 SetPowerOff = 0, | |
125 SetPowerOn = 1 | |
126 } | |
127 | |
128 #endregion | |
129 | |
130 #region structures | |
131 | |
132 [StructLayout(LayoutKind.Sequential)] | |
133 struct SP_CLASSINSTALL_HEADER { | |
134 public int cbSize; | |
135 public int InstallFunction; //DI_FUNCTION InstallFunction; | |
136 } | |
137 | |
138 [StructLayout(LayoutKind.Sequential)] | |
139 struct SP_PROPCHANGE_PARAMS { | |
140 public SP_CLASSINSTALL_HEADER ClassInstallHeader; | |
141 public int StateChange; | |
142 public int Scope; | |
143 public int HwProfile; | |
144 } | |
145 | |
146 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] | |
147 struct USB_HCD_DRIVERKEY_NAME { | |
148 public uint ActualLength; | |
149 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = UsbApi.MAX_BUFFER_SIZE)] | |
150 public string DriverKeyName; //WCHAR DriverKeyName[1]; | |
151 } | |
152 | |
153 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] | |
154 struct USB_ROOT_HUB_NAME { | |
155 public uint ActualLength; | |
156 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = UsbApi.MAX_BUFFER_SIZE)] | |
157 public string RootHubName; //WCHAR RootHubName[1]; | |
158 } | |
159 | |
160 [StructLayout(LayoutKind.Sequential, Pack = 1)] | |
161 struct USB_HUB_DESCRIPTOR { | |
162 public byte bDescriptorLength; | |
62
edc41c861d96
Updates and cleanup in Windows USB enumeration code
Ivo Smits <Ivo@UCIS.nl>
parents:
58
diff
changeset
|
163 public UsbDescriptorType bDescriptorType; |
21 | 164 public byte bNumberOfPorts; |
165 public ushort wHubCharacteristics; | |
166 public byte bPowerOnToPowerGood; | |
167 public byte bHubControlCurrent; | |
168 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)] | |
169 public byte[] bRemoveAndPowerMask; //UCHAR bRemoveAndPowerMask[64]; | |
170 } | |
171 | |
172 [StructLayout(LayoutKind.Sequential)] | |
173 struct USB_HUB_INFORMATION { | |
174 public USB_HUB_DESCRIPTOR HubDescriptor; | |
175 public bool HubIsBusPowered; | |
176 } | |
177 | |
178 [StructLayout(LayoutKind.Sequential)] | |
179 struct USB_NODE_INFORMATION { | |
180 //public int NodeType; | |
181 public USB_HUB_NODE NodeType; | |
182 public USB_HUB_INFORMATION HubInformation; // union { USB_HUB_INFORMATION HubInformation; USB_MI_PARENT_INFORMATION MiParentInformation; } | |
183 } | |
184 | |
185 [StructLayout(LayoutKind.Sequential, Pack = 1)] | |
186 struct USB_NODE_CONNECTION_INFORMATION_EX { | |
187 public uint ConnectionIndex; | |
62
edc41c861d96
Updates and cleanup in Windows USB enumeration code
Ivo Smits <Ivo@UCIS.nl>
parents:
58
diff
changeset
|
188 public UsbDeviceDescriptor DeviceDescriptor; |
21 | 189 public byte CurrentConfigurationValue; |
190 public USB_DEVICE_SPEED Speed; | |
191 public byte DeviceIsHub; //BOOLEAN DeviceIsHub; | |
192 public ushort DeviceAddress; | |
193 public uint NumberOfOpenPipes; | |
194 public USB_CONNECTION_STATUS ConnectionStatus; | |
195 //public IntPtr PipeList; //USB_PIPE_INFO PipeList[0]; | |
196 //[MarshalAs(UnmanagedType.ByValArray, SizeConst=100)] | |
197 //Byte[] PipeList; | |
198 } | |
199 | |
200 [StructLayout(LayoutKind.Sequential)] | |
201 struct HID_DESCRIPTOR_DESC_LIST { | |
202 public byte bReportType; | |
203 public short wReportLength; | |
204 } | |
205 | |
206 [StructLayout(LayoutKind.Sequential, Pack = 1)] | |
207 struct HID_DESCRIPTOR { | |
208 public byte bLength; | |
62
edc41c861d96
Updates and cleanup in Windows USB enumeration code
Ivo Smits <Ivo@UCIS.nl>
parents:
58
diff
changeset
|
209 public UsbDescriptorType bDescriptorType; |
21 | 210 public ushort bcdHID; |
211 public byte bCountry; | |
212 public byte bNumDescriptors; | |
213 public HID_DESCRIPTOR_DESC_LIST hid_desclist; //DescriptorList [1]; | |
214 } | |
215 | |
62
edc41c861d96
Updates and cleanup in Windows USB enumeration code
Ivo Smits <Ivo@UCIS.nl>
parents:
58
diff
changeset
|
216 [StructLayout(LayoutKind.Sequential, Pack = 8)] |
21 | 217 struct USB_DESCRIPTOR_REQUEST { |
218 public uint ConnectionIndex; | |
62
edc41c861d96
Updates and cleanup in Windows USB enumeration code
Ivo Smits <Ivo@UCIS.nl>
parents:
58
diff
changeset
|
219 public UsbSetupPacket SetupPacket; |
21 | 220 //public byte[] Data; //UCHAR Data[0]; |
221 } | |
222 | |
58
fd63c453ff65
Improved Windows USB enumeration classes
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
223 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] |
21 | 224 struct USB_NODE_CONNECTION_NAME { |
225 public uint ConnectionIndex; | |
226 public uint ActualLength; | |
227 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = UsbApi.MAX_BUFFER_SIZE)] | |
228 public string NodeName; //WCHAR NodeName[1]; | |
229 } | |
230 | |
58
fd63c453ff65
Improved Windows USB enumeration classes
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
231 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] |
21 | 232 struct USB_NODE_CONNECTION_DRIVERKEY_NAME { |
233 public uint ConnectionIndex; | |
234 public uint ActualLength; | |
235 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = UsbApi.MAX_BUFFER_SIZE)] | |
236 public string DriverKeyName; //WCHAR DriverKeyName[1]; | |
237 } | |
238 | |
239 [StructLayout(LayoutKind.Sequential)] | |
240 struct STORAGE_DEVICE_NUMBER { | |
241 public int DeviceType; //DEVICE_TYPE DeviceType; | |
242 public uint DeviceNumber; | |
243 public uint PartitionNumber; | |
244 } | |
245 #endregion | |
62
edc41c861d96
Updates and cleanup in Windows USB enumeration code
Ivo Smits <Ivo@UCIS.nl>
parents:
58
diff
changeset
|
246 } |