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