Mercurial > hg > ucis.core
annotate USBLib/Communication/LibUsbDotNet.cs @ 61:2b24666cd759
Fixed errors in Windows USB enumeration and LibUsbDotNet configuration descriptor code
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Sun, 13 Oct 2013 00:48:28 +0200 |
parents | 3424fa5a12c9 |
children | abe0d55a2201 |
rev | line source |
---|---|
21 | 1 ???using System; |
22 | 2 using System.Collections.Generic; |
21 | 3 using System.Collections.ObjectModel; |
22 | 4 using System.IO; |
21 | 5 using System.Runtime.InteropServices; |
22 | 6 using System.Text; |
21 | 7 using System.Threading; |
22 | 8 using LibUsbDotNet.Descriptors; |
9 using LibUsbDotNet.Info; | |
21 | 10 using LibUsbDotNet.Main; |
11 using UCIS.USBLib.Communication; | |
12 using LibUsb0Registry = UCIS.USBLib.Communication.LibUsb.LibUsb0Registry; | |
13 using LibUsb1Registry = UCIS.USBLib.Communication.LibUsb1.LibUsb1Registry; | |
14 using nIUsbDevice = UCIS.USBLib.Communication.IUsbDevice; | |
15 using nIUsbInterface = UCIS.USBLib.Communication.IUsbInterface; | |
16 using WinUsbRegistry = UCIS.USBLib.Communication.WinUsb.WinUsbRegistry; | |
35
6fcedb1030bf
USBLib: Added support for USBIO driver
Ivo Smits <Ivo@UCIS.nl>
parents:
22
diff
changeset
|
17 using USBIORegistry = UCIS.USBLib.Communication.USBIO.USBIORegistry; |
21 | 18 |
19 namespace LibUsbDotNet { | |
20 public class UsbDevice : IUsbDevice { | |
21 public nIUsbInterface Device { get; private set; } | |
22 public UsbDevice(nIUsbInterface dev) { | |
60
3424fa5a12c9
Updated Windows USB enumeration classes and VBoxUSB backend
Ivo Smits <Ivo@UCIS.nl>
parents:
38
diff
changeset
|
23 if (dev == null) throw new ArgumentNullException("dev"); |
21 | 24 Device = dev; |
25 } | |
26 public bool GetDescriptor(byte descriptorType, byte index, short langId, Byte[] buffer, int bufferLength, out int transferLength) { | |
27 try { | |
28 transferLength = Device.GetDescriptor(descriptorType, index, langId, buffer, 0, bufferLength); | |
29 return true; | |
30 } catch { | |
31 transferLength = 0; | |
32 return false; | |
33 } | |
34 } | |
35 public bool ControlTransfer(ref UsbSetupPacket setupPacket, Byte[] buffer, int bufferLength, out int lengthTransferred) { | |
36 if ((setupPacket.RequestType & 128) != 0) { | |
37 lengthTransferred = Device.ControlRead((UsbControlRequestType)setupPacket.RequestType, setupPacket.Request, setupPacket.Value, setupPacket.Index, buffer, 0, bufferLength); | |
38 } else { | |
39 lengthTransferred = Device.ControlWrite((UsbControlRequestType)setupPacket.RequestType, setupPacket.Request, setupPacket.Value, setupPacket.Index, buffer, 0, bufferLength); | |
40 } | |
41 return true; | |
42 } | |
43 public DriverModeType DriverMode { get { return DriverModeType.Unknown; } } | |
44 public enum DriverModeType { | |
45 Unknown, | |
46 LibUsb, | |
47 WinUsb, | |
48 MonoLibUsb, | |
49 LibUsbWinBack | |
50 } | |
51 public UsbEndpointWriter OpenEndpointWriter(WriteEndpointID writeEndpointID, EndpointType endpointType) { | |
52 return new UsbEndpointWriter(Device, (Byte)writeEndpointID, endpointType); | |
53 } | |
54 public UsbEndpointReader OpenEndpointReader(ReadEndpointID readEndpointID, int buffersize, EndpointType endpointType) { | |
55 UsbEndpointReader reader = new UsbEndpointReader(Device, (Byte)readEndpointID, endpointType); | |
56 reader.ReadBufferSize = buffersize; | |
57 return reader; | |
58 } | |
59 public void Close() { | |
60 Device.Dispose(); | |
61 } | |
62 public UsbDeviceInfo Info { get { return new UsbDeviceInfo(this); } } | |
63 public static IList<UsbRegistry> AllDevices { | |
64 get { | |
65 List<UsbRegistry> list = new List<UsbRegistry>(); | |
66 if (Environment.OSVersion.Platform == PlatformID.Win32NT) { | |
67 foreach (IUsbDeviceRegistry reg in WinUsbRegistry.DeviceList) list.Add(new UsbRegistry(reg)); | |
68 foreach (IUsbDeviceRegistry reg in LibUsb0Registry.DeviceList) list.Add(new UsbRegistry(reg)); | |
35
6fcedb1030bf
USBLib: Added support for USBIO driver
Ivo Smits <Ivo@UCIS.nl>
parents:
22
diff
changeset
|
69 foreach (IUsbDeviceRegistry reg in USBIORegistry.DeviceList) list.Add(new UsbRegistry(reg)); |
21 | 70 } else { |
71 foreach (IUsbDeviceRegistry reg in LibUsb1Registry.DeviceList) list.Add(new UsbRegistry(reg)); | |
72 } | |
73 return list; | |
74 } | |
75 } | |
76 private SafeHandle Handle { get { return null; } } | |
77 public bool SetConfiguration(byte config) { | |
78 nIUsbDevice dev = Device as nIUsbDevice; | |
79 if (dev == null) return false; | |
80 try { | |
81 dev.Configuration = config; | |
82 return true; | |
83 } catch { | |
84 return false; | |
85 } | |
86 } | |
87 public bool ClaimInterface(int interfaceID) { | |
88 nIUsbDevice dev = Device as nIUsbDevice; | |
89 if (dev == null) return false; | |
90 try { | |
60
3424fa5a12c9
Updated Windows USB enumeration classes and VBoxUSB backend
Ivo Smits <Ivo@UCIS.nl>
parents:
38
diff
changeset
|
91 if (dev.Configuration == 0) dev.Configuration = 1; |
21 | 92 dev.ClaimInterface(interfaceID); |
93 return true; | |
94 } catch { | |
95 return false; | |
96 } | |
97 } | |
98 public bool ReleaseInterface(int interfaceID) { | |
99 nIUsbDevice dev = Device as nIUsbDevice; | |
100 if (dev == null) return false; | |
101 try { | |
102 dev.ReleaseInterface(interfaceID); | |
103 return true; | |
104 } catch { | |
105 return false; | |
106 } | |
107 } | |
108 public IList<UsbConfigInfo> Configs { | |
109 get { | |
110 List<UsbConfigInfo> rtnConfigs = new List<UsbConfigInfo>(); | |
111 int iConfigs = Info.Descriptor.ConfigurationCount; | |
112 for (int iConfig = 0; iConfig < iConfigs; iConfig++) { | |
113 int iBytesTransmitted; | |
61
2b24666cd759
Fixed errors in Windows USB enumeration and LibUsbDotNet configuration descriptor code
Ivo Smits <Ivo@UCIS.nl>
parents:
60
diff
changeset
|
114 byte[] cfgBuffer = new byte[9]; |
38
a9c4fed19e99
USBLib: fixes in USBIO driver and LibUsbDotNet compatibility code
Ivo Smits <Ivo@UCIS.nl>
parents:
35
diff
changeset
|
115 if (!GetDescriptor((byte)UsbDescriptorType.Configuration, (byte)iConfig, 0, cfgBuffer, cfgBuffer.Length, out iBytesTransmitted)) |
21 | 116 throw new Exception("Could not read configuration descriptor"); |
117 if (iBytesTransmitted < UsbConfigDescriptor.Size || cfgBuffer[1] != (byte)UsbDescriptorType.Configuration) | |
118 throw new Exception("GetDeviceConfigs: USB config descriptor is invalid."); | |
119 UsbConfigDescriptor configDescriptor = new UsbConfigDescriptor(); | |
120 Helper.BytesToObject(cfgBuffer, 0, Math.Min(UsbConfigDescriptor.Size, cfgBuffer[0]), configDescriptor); | |
61
2b24666cd759
Fixed errors in Windows USB enumeration and LibUsbDotNet configuration descriptor code
Ivo Smits <Ivo@UCIS.nl>
parents:
60
diff
changeset
|
121 if (configDescriptor.TotalLength > cfgBuffer.Length) { |
2b24666cd759
Fixed errors in Windows USB enumeration and LibUsbDotNet configuration descriptor code
Ivo Smits <Ivo@UCIS.nl>
parents:
60
diff
changeset
|
122 cfgBuffer = new Byte[configDescriptor.TotalLength]; |
2b24666cd759
Fixed errors in Windows USB enumeration and LibUsbDotNet configuration descriptor code
Ivo Smits <Ivo@UCIS.nl>
parents:
60
diff
changeset
|
123 if (!GetDescriptor((byte)UsbDescriptorType.Configuration, (byte)iConfig, 0, cfgBuffer, cfgBuffer.Length, out iBytesTransmitted)) |
2b24666cd759
Fixed errors in Windows USB enumeration and LibUsbDotNet configuration descriptor code
Ivo Smits <Ivo@UCIS.nl>
parents:
60
diff
changeset
|
124 throw new Exception("Could not read configuration descriptor"); |
2b24666cd759
Fixed errors in Windows USB enumeration and LibUsbDotNet configuration descriptor code
Ivo Smits <Ivo@UCIS.nl>
parents:
60
diff
changeset
|
125 if (iBytesTransmitted < UsbConfigDescriptor.Size || cfgBuffer[1] != (byte)UsbDescriptorType.Configuration) |
2b24666cd759
Fixed errors in Windows USB enumeration and LibUsbDotNet configuration descriptor code
Ivo Smits <Ivo@UCIS.nl>
parents:
60
diff
changeset
|
126 throw new Exception("GetDeviceConfigs: USB config descriptor is invalid."); |
2b24666cd759
Fixed errors in Windows USB enumeration and LibUsbDotNet configuration descriptor code
Ivo Smits <Ivo@UCIS.nl>
parents:
60
diff
changeset
|
127 Helper.BytesToObject(cfgBuffer, 0, Math.Min(UsbConfigDescriptor.Size, cfgBuffer[0]), configDescriptor); |
2b24666cd759
Fixed errors in Windows USB enumeration and LibUsbDotNet configuration descriptor code
Ivo Smits <Ivo@UCIS.nl>
parents:
60
diff
changeset
|
128 } |
21 | 129 if (configDescriptor.TotalLength != iBytesTransmitted) throw new Exception("GetDeviceConfigs: USB config descriptor length doesn't match the length received."); |
130 List<byte[]> rawDescriptorList = new List<byte[]>(); | |
131 int iRawLengthPosition = configDescriptor.Length; | |
132 while (iRawLengthPosition < configDescriptor.TotalLength) { | |
133 byte[] rawDescriptor = new byte[cfgBuffer[iRawLengthPosition]]; | |
134 if (iRawLengthPosition + rawDescriptor.Length > iBytesTransmitted) throw new Exception("Descriptor length is out of range."); | |
135 Array.Copy(cfgBuffer, iRawLengthPosition, rawDescriptor, 0, rawDescriptor.Length); | |
136 rawDescriptorList.Add(rawDescriptor); | |
137 iRawLengthPosition += rawDescriptor.Length; | |
138 } | |
139 rtnConfigs.Add(new UsbConfigInfo(this, configDescriptor, ref rawDescriptorList)); | |
140 } | |
141 return rtnConfigs; | |
142 } | |
143 } | |
144 } | |
145 public interface IUsbDevice { | |
146 bool SetConfiguration(byte config); | |
147 bool ClaimInterface(int interfaceID); | |
148 bool ReleaseInterface(int interfaceID); | |
149 } | |
150 public class UsbEndpointWriter : IDisposable { | |
151 public nIUsbInterface Device { get; private set; } | |
152 public Byte EndpointID { get; private set; } | |
153 public EndpointType EndpointType { get; private set; } | |
154 public Byte EpNum { get { return EndpointID; } } | |
155 public UsbEndpointWriter(nIUsbInterface dev, byte epid, EndpointType eptype) { | |
156 Device = dev; | |
157 EndpointID = epid; | |
158 EndpointType = eptype; | |
159 } | |
160 public ErrorCode Write(byte[] buffer, int offset, int count, int timeout, out int transferLength) { | |
161 switch (EndpointType) { | |
162 case EndpointType.Bulk: transferLength = Device.BulkWrite(EndpointID, buffer, offset, count); break; | |
163 case EndpointType.Interrupt: transferLength = Device.InterruptWrite(EndpointID, buffer, offset, count); break; | |
164 default: transferLength = 0; return ErrorCode.Error; | |
165 } | |
166 return ErrorCode.Ok; | |
167 } | |
168 public void Dispose() { } | |
169 } | |
170 public class UsbEndpointReader : IDisposable { | |
171 public nIUsbInterface Device { get; private set; } | |
172 public Byte EndpointID { get; private set; } | |
173 public EndpointType EndpointType { get; private set; } | |
174 public Byte EpNum { get { return EndpointID; } } | |
175 public int ReadBufferSize { get; set; } | |
176 | |
177 public UsbEndpointReader(nIUsbInterface dev, byte epid, EndpointType eptype) { | |
178 Device = dev; | |
179 EndpointID = epid; | |
180 EndpointType = eptype; | |
181 ReadBufferSize = 4096; | |
182 } | |
183 public ErrorCode Read(byte[] buffer, int offset, int count, int timeout, out int transferLength) { | |
184 switch (EndpointType) { | |
185 case EndpointType.Bulk: transferLength = Device.BulkRead(EndpointID, buffer, offset, count); break; | |
186 case EndpointType.Interrupt: transferLength = Device.InterruptRead(EndpointID, buffer, offset, count); break; | |
187 default: transferLength = 0; return ErrorCode.Error; | |
188 } | |
189 return ErrorCode.Ok; | |
190 } | |
191 public void Dispose() { DataReceivedEnabled = false; } | |
192 | |
193 private bool mDataReceivedEnabled; | |
194 private Thread mReadThread; | |
195 | |
196 public virtual bool DataReceivedEnabled { | |
197 get { return mDataReceivedEnabled; } | |
198 set { | |
199 if (value != mDataReceivedEnabled) { | |
200 if (mDataReceivedEnabled) { | |
201 mReadThread.Abort(); | |
202 } else { | |
203 mDataReceivedEnabled = true; | |
204 mReadThread = new Thread(ReadDataProcess); | |
205 mReadThread.Start(); | |
206 } | |
207 } | |
208 } | |
209 } | |
210 | |
211 private void ReadDataProcess(object state) { | |
212 byte[] buffer = new byte[ReadBufferSize]; | |
213 try { | |
214 while (mDataReceivedEnabled) { | |
215 int transferLength; | |
216 Read(buffer, 0, buffer.Length, -1, out transferLength); | |
217 EventHandler<EndpointDataEventArgs> eh = DataReceived; | |
218 if (!ReferenceEquals(eh, null)) eh(this, new EndpointDataEventArgs(buffer, transferLength)); | |
219 } | |
220 } catch (Exception ex) { | |
221 if (ReadError != null) ReadError(this, new ErrorEventArgs(ex)); | |
222 } finally { | |
223 mDataReceivedEnabled = false; | |
224 } | |
225 } | |
226 | |
227 public event EventHandler<EndpointDataEventArgs> DataReceived; | |
228 public event ErrorEventHandler ReadError; | |
229 } | |
230 } | |
231 namespace LibUsbDotNet.Main { | |
232 public static class UsbConstants { | |
233 public const int MAX_CONFIG_SIZE = 4096; | |
234 public const int MAX_DEVICES = 128; | |
235 public const int MAX_ENDPOINTS = 32; | |
236 public const byte ENDPOINT_DIR_MASK = 0x80; | |
237 public const byte ENDPOINT_NUMBER_MASK = 0xf; | |
238 } | |
239 public static class Helper { | |
240 public static void BytesToObject(byte[] sourceBytes, int iStartIndex, int iLength, object destObject) { | |
241 GCHandle gch = GCHandle.Alloc(destObject, GCHandleType.Pinned); | |
242 IntPtr ptrDestObject = gch.AddrOfPinnedObject(); | |
243 Marshal.Copy(sourceBytes, iStartIndex, ptrDestObject, iLength); | |
244 gch.Free(); | |
245 } | |
246 public static string ToString(string sep0, string[] names, string sep1, object[] values, string sep2) { | |
247 StringBuilder sb = new StringBuilder(); | |
248 for (int i = 0; i < names.Length; i++) sb.Append(sep0 + names[i] + sep1 + values[i] + sep2); | |
249 return sb.ToString(); | |
250 } | |
251 } | |
252 public class EndpointDataEventArgs : EventArgs { | |
253 internal EndpointDataEventArgs(byte[] bytes, int size) { | |
254 Buffer = bytes; | |
255 Count = size; | |
256 } | |
257 public byte[] Buffer { get; private set; } | |
258 public int Count { get; private set; } | |
259 } | |
260 [StructLayout(LayoutKind.Sequential, Pack = 1)] | |
261 public struct UsbSetupPacket { | |
262 public byte RequestType; | |
263 public byte Request; | |
264 public short Value; | |
265 public short Index; | |
266 public short Length; | |
267 public UsbSetupPacket(byte requestType, byte request, short value, short index, short length) { | |
268 RequestType = requestType; | |
269 Request = request; | |
270 Value = value; | |
271 Index = index; | |
272 Length = length; | |
273 } | |
274 } | |
275 public enum UsbEndpointDirection : byte { | |
276 EndpointIn = 0x80, | |
277 EndpointOut = 0x00, | |
278 } | |
279 public enum UsbRequestType : byte { | |
280 TypeClass = (0x01 << 5), | |
281 TypeReserved = (0x03 << 5), | |
282 TypeStandard = (0x00 << 5), | |
283 TypeVendor = (0x02 << 5), | |
284 } | |
285 public enum WriteEndpointID : byte { | |
286 Ep01 = 0x01, | |
287 Ep02 = 0x02, | |
288 Ep03 = 0x03, | |
289 Ep04 = 0x04, | |
290 Ep05 = 0x05, | |
291 Ep06 = 0x06, | |
292 Ep07 = 0x07, | |
293 Ep08 = 0x08, | |
294 Ep09 = 0x09, | |
295 Ep10 = 0x0A, | |
296 Ep11 = 0x0B, | |
297 Ep12 = 0x0C, | |
298 Ep13 = 0x0D, | |
299 Ep14 = 0x0E, | |
300 Ep15 = 0x0F, | |
301 } | |
302 public enum ReadEndpointID : byte { | |
303 Ep01 = 0x81, | |
304 Ep02 = 0x82, | |
305 Ep03 = 0x83, | |
306 Ep04 = 0x84, | |
307 Ep05 = 0x85, | |
308 Ep06 = 0x86, | |
309 Ep07 = 0x87, | |
310 Ep08 = 0x88, | |
311 Ep09 = 0x89, | |
312 Ep10 = 0x8A, | |
313 Ep11 = 0x8B, | |
314 Ep12 = 0x8C, | |
315 Ep13 = 0x8D, | |
316 Ep14 = 0x8E, | |
317 Ep15 = 0x8F, | |
318 } | |
319 public enum UsbRequestRecipient : byte { | |
320 RecipDevice = 0x00, | |
321 RecipInterface = 0x01, | |
322 RecipEndpoint = 0x02, | |
323 RecipOther = 0x03, | |
324 } | |
325 public enum EndpointType : byte { | |
326 Control, | |
327 Isochronous, | |
328 Bulk, | |
329 Interrupt | |
330 } | |
331 public enum ErrorCode { | |
332 Ok = 0, | |
333 Error = 1, | |
334 } | |
335 public class UsbRegistry { | |
336 public IUsbDeviceRegistry Registry { get; private set; } | |
337 public UsbRegistry(IUsbDeviceRegistry reg) { | |
338 Registry = reg; | |
339 } | |
340 public int Vid { get { return Registry.Vid; } } | |
341 public int Pid { get { return Registry.Pid; } } | |
342 public int Rev { get { return 0; } } | |
343 public Boolean IsAlive { get { return true; } } | |
344 public Boolean Open(out UsbDevice hand) { | |
345 hand = new UsbDevice(Registry.Open()); | |
346 return true; | |
347 } | |
348 public IDictionary<String, Object> DeviceProperties { get { return Registry.DeviceProperties; } } | |
349 public String FullName { get { return Registry.FullName; } } | |
350 public String Name { get { return Registry.Name; } } | |
351 public String SymbolicName { get { return Registry.SymbolicName; } } | |
352 } | |
353 } | |
354 namespace LibUsbDotNet.Info { | |
355 public class UsbDeviceInfo { | |
356 private readonly UsbDeviceDescriptor mDeviceDescriptor; | |
357 internal UsbDevice mUsbDevice; | |
358 internal UsbDeviceInfo(UsbDevice usbDevice) { | |
359 mUsbDevice = usbDevice; | |
360 mDeviceDescriptor = new UsbDeviceDescriptor(); | |
361 Byte[] bytes = new Byte[UsbDeviceDescriptor.Size]; | |
362 int ret; | |
363 usbDevice.GetDescriptor((byte)UsbDescriptorType.Device, 0, 0, bytes, UsbDeviceDescriptor.Size, out ret); | |
364 Object asobj = mDeviceDescriptor; | |
365 Helper.BytesToObject(bytes, 0, ret, asobj); | |
366 mDeviceDescriptor = (UsbDeviceDescriptor)asobj; | |
367 } | |
368 public UsbDeviceDescriptor Descriptor { get { return mDeviceDescriptor; } } | |
369 public String ManufacturerString { | |
370 get { | |
371 if (Descriptor.ManufacturerStringIndex == 0) return null; | |
372 return mUsbDevice.Device.GetString(0, Descriptor.ManufacturerStringIndex); | |
373 } | |
374 } | |
375 public String ProductString { | |
376 get { | |
377 if (Descriptor.ProductStringIndex == 0) return null; | |
378 return mUsbDevice.Device.GetString(0, Descriptor.ProductStringIndex); | |
379 } | |
380 } | |
381 public String SerialString { | |
382 get { | |
383 if (Descriptor.SerialStringIndex == 0) return null; | |
384 return mUsbDevice.Device.GetString(0, Descriptor.SerialStringIndex); | |
385 } | |
386 } | |
387 } | |
388 public class UsbConfigInfo { | |
389 private readonly List<UsbInterfaceInfo> mInterfaceList = new List<UsbInterfaceInfo>(); | |
390 internal readonly UsbConfigDescriptor mUsbConfigDescriptor; | |
391 internal UsbDevice mUsbDevice; | |
392 internal UsbConfigInfo(UsbDevice usbDevice, UsbConfigDescriptor descriptor, ref List<byte[]> rawDescriptors) { | |
393 mUsbDevice = usbDevice; | |
394 mUsbConfigDescriptor = descriptor; | |
395 UsbInterfaceInfo currentInterface = null; | |
396 for (int iRawDescriptor = 0; iRawDescriptor < rawDescriptors.Count; iRawDescriptor++) { | |
397 byte[] bytesRawDescriptor = rawDescriptors[iRawDescriptor]; | |
398 switch (bytesRawDescriptor[1]) { | |
399 case (byte)UsbDescriptorType.Interface: | |
400 currentInterface = new UsbInterfaceInfo(usbDevice, bytesRawDescriptor); | |
401 mInterfaceList.Add(currentInterface); | |
402 break; | |
403 case (byte)UsbDescriptorType.Endpoint: | |
404 if (currentInterface == null) throw new Exception("Recieved and endpoint descriptor before receiving an interface descriptor."); | |
405 currentInterface.mEndpointInfo.Add(new UsbEndpointInfo(bytesRawDescriptor)); | |
406 break; | |
407 } | |
408 } | |
409 } | |
410 public UsbConfigDescriptor Descriptor { get { return mUsbConfigDescriptor; } } | |
411 public ReadOnlyCollection<UsbInterfaceInfo> InterfaceInfoList { get { return mInterfaceList.AsReadOnly(); } } | |
412 } | |
413 public class UsbInterfaceInfo { | |
414 internal readonly UsbInterfaceDescriptor mUsbInterfaceDescriptor; | |
415 internal UsbDevice mUsbDevice; | |
416 internal List<UsbEndpointInfo> mEndpointInfo = new List<UsbEndpointInfo>(); | |
417 internal UsbInterfaceInfo(UsbDevice usbDevice, byte[] descriptor) { | |
418 mUsbDevice = usbDevice; | |
419 mUsbInterfaceDescriptor = new UsbInterfaceDescriptor(); | |
420 Helper.BytesToObject(descriptor, 0, Math.Min(UsbInterfaceDescriptor.Size, descriptor[0]), mUsbInterfaceDescriptor); | |
421 } | |
422 public UsbInterfaceDescriptor Descriptor { get { return mUsbInterfaceDescriptor; } } | |
423 public ReadOnlyCollection<UsbEndpointInfo> EndpointInfoList { get { return mEndpointInfo.AsReadOnly(); } } | |
424 } | |
425 public class UsbEndpointInfo { | |
426 internal UsbEndpointDescriptor mUsbEndpointDescriptor; | |
427 internal UsbEndpointInfo(byte[] descriptor) { | |
428 mUsbEndpointDescriptor = new UsbEndpointDescriptor(); | |
429 Helper.BytesToObject(descriptor, 0, Math.Min(UsbEndpointDescriptor.Size, descriptor[0]), mUsbEndpointDescriptor); | |
430 } | |
431 public UsbEndpointDescriptor Descriptor { | |
432 get { return mUsbEndpointDescriptor; } | |
433 } | |
434 } | |
435 } | |
436 namespace LibUsbDotNet.Descriptors { | |
437 public enum DescriptorType : byte { | |
438 Device = 1, | |
439 Configuration = 2, | |
440 String = 3, | |
441 Interface = 4, | |
442 Endpoint = 5, | |
443 DeviceQualifier = 6, | |
444 OtherSpeedConfiguration = 7, | |
445 InterfacePower = 8, | |
446 OTG = 9, | |
447 Debug = 10, | |
448 InterfaceAssociation = 11, | |
449 Hid = 0x21, | |
450 HidReport = 0x22, | |
451 Physical = 0x23, | |
452 Hub = 0x29 | |
453 } | |
454 public enum ClassCodeType : byte { | |
455 PerInterface = 0, | |
456 Audio = 1, | |
457 Comm = 2, | |
458 Hid = 3, | |
459 Printer = 7, | |
460 Ptp = 6, | |
461 MassStorage = 8, | |
462 Hub = 9, | |
463 Data = 10, | |
464 VendorSpec = 0xff | |
465 } | |
466 [StructLayout(LayoutKind.Sequential, Pack = 1)] | |
467 public abstract class UsbDescriptor { | |
468 public static readonly int Size = Marshal.SizeOf(typeof(UsbDescriptor)); | |
469 public byte Length; | |
470 public DescriptorType DescriptorType; | |
471 } | |
472 [StructLayout(LayoutKind.Sequential, Pack = 1)] | |
473 public class UsbDeviceDescriptor : UsbDescriptor { | |
474 public new static readonly int Size = Marshal.SizeOf(typeof(UsbDeviceDescriptor)); | |
475 public short BcdUsb; | |
476 public ClassCodeType Class; | |
477 public byte SubClass; | |
478 public byte Protocol; | |
479 public byte MaxPacketSize0; | |
480 public short VendorID; | |
481 public short ProductID; | |
482 public short BcdDevice; | |
483 public byte ManufacturerStringIndex; | |
484 public byte ProductStringIndex; | |
485 public byte SerialStringIndex; | |
486 public byte ConfigurationCount; | |
487 internal UsbDeviceDescriptor() { } | |
488 } | |
489 [StructLayout(LayoutKind.Sequential, Pack = 1)] | |
490 public class UsbConfigDescriptor : UsbDescriptor { | |
491 public new static readonly int Size = Marshal.SizeOf(typeof(UsbConfigDescriptor)); | |
492 public readonly short TotalLength; | |
493 public readonly byte InterfaceCount; | |
494 public readonly byte ConfigID; | |
495 public readonly byte StringIndex; | |
496 public readonly byte Attributes; | |
497 public readonly byte MaxPower; | |
498 internal UsbConfigDescriptor() { } | |
499 } | |
500 [StructLayout(LayoutKind.Sequential, Pack = 1)] | |
501 public class UsbInterfaceDescriptor : UsbDescriptor { | |
502 public new static readonly int Size = Marshal.SizeOf(typeof(UsbInterfaceDescriptor)); | |
503 public readonly byte InterfaceID; | |
504 public readonly byte AlternateID; | |
505 public readonly byte EndpointCount; | |
506 public readonly ClassCodeType Class; | |
507 public readonly byte SubClass; | |
508 public readonly byte Protocol; | |
509 public readonly byte StringIndex; | |
510 internal UsbInterfaceDescriptor() { } | |
511 } | |
512 [StructLayout(LayoutKind.Sequential, Pack = 1)] | |
513 public class UsbEndpointDescriptor : UsbDescriptor { | |
514 public new static readonly int Size = Marshal.SizeOf(typeof(UsbEndpointDescriptor)); | |
515 public readonly byte EndpointID; | |
516 public readonly byte Attributes; | |
517 public readonly short MaxPacketSize; | |
518 public readonly byte Interval; | |
519 public readonly byte Refresh; | |
520 public readonly byte SynchAddress; | |
521 internal UsbEndpointDescriptor() { } | |
522 } | |
523 } | |
524 namespace MonoLibUsb { | |
525 public static class MonoUsbApi { | |
526 internal const CallingConvention CC = 0; | |
527 internal const string LIBUSB_DLL = "libusb-1.0.dll"; | |
528 [DllImport(LIBUSB_DLL, CallingConvention = CC, SetLastError = false, EntryPoint = "libusb_detach_kernel_driver")] | |
529 public static extern int DetachKernelDriver([In] MonoUsbDeviceHandle deviceHandle, int interfaceNumber); | |
530 public static int ControlTransfer(MonoUsbDeviceHandle deviceHandle, byte requestType, byte request, short value, short index, object data, short dataLength, int timeout) { | |
531 throw new NotImplementedException(); | |
532 } | |
533 public static int BulkTransfer(MonoUsbDeviceHandle deviceHandle, byte endpoint, object data, int length, out int actualLength, int timeout) { | |
534 throw new NotImplementedException(); | |
535 } | |
536 } | |
537 public abstract class MonoUsbDeviceHandle : SafeHandle { | |
538 public MonoUsbDeviceHandle(Boolean bOwnsHandle) : base(IntPtr.Zero, bOwnsHandle) { } | |
539 } | |
540 } |