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