NativeMethods.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #region License
  2. /* Copyright 2012 James F. Bellinger <http://www.zer7.com/software/hidsharp>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  13. #endregion
  14. using System.Runtime.InteropServices;
  15. namespace HidSharp.Platform.Libusb
  16. {
  17. internal static class NativeMethods
  18. {
  19. private const string Libusb = "libusb-1.0";
  20. public struct DeviceDescriptor
  21. {
  22. public byte bLength, bDescriptorType;
  23. public ushort bcdUSB;
  24. public byte bDeviceClass, bDeviceSubClass, bDeviceProtocol, bMaxPacketSize0;
  25. public ushort idVendor, idProduct, bcdDevice;
  26. public byte iManufacturer, iProduct, iSerialNumber, bNumConfigurations;
  27. }
  28. public enum DeviceClass : byte
  29. {
  30. HID = 0x03,
  31. MassStorage = 0x08,
  32. VendorSpecific = 0xff
  33. }
  34. public enum DescriptorType : byte
  35. {
  36. Device = 0x01,
  37. Configuration = 0x02,
  38. String = 0x03,
  39. Interface = 0x04,
  40. Endpoint = 0x05,
  41. HID = 0x21,
  42. Report = 0x22,
  43. Physical = 0x23,
  44. Hub = 0x29
  45. }
  46. public enum EndpointDirection : byte
  47. {
  48. In = 0x80,
  49. Out = 0,
  50. }
  51. public enum Request : byte
  52. {
  53. GetDescriptor = 0x06
  54. }
  55. public enum RequestRecipient : byte
  56. {
  57. Device = 0,
  58. Interface = 1,
  59. Endpoint = 2,
  60. Other = 3
  61. }
  62. public enum RequestType : byte
  63. {
  64. Standard = 0x00,
  65. Class = 0x20,
  66. Vendor = 0x40
  67. }
  68. public enum TransferType : byte
  69. {
  70. Control = 0,
  71. Isochronous,
  72. Bulk,
  73. Interrupt
  74. }
  75. public struct Version
  76. {
  77. public ushort Major, Minor, Micro, Nano;
  78. }
  79. public enum Error
  80. {
  81. None = 0,
  82. IO = -1,
  83. InvalidParameter = -2,
  84. AccessDenied = -3,
  85. NoDevice = -4,
  86. NotFound = -5,
  87. Busy = -6,
  88. Timeout = -7,
  89. Overflow = -8,
  90. Pipe = -9,
  91. Interrupted = -10,
  92. OutOfMemory = -11,
  93. NotSupported = -12
  94. }
  95. [DllImport(Libusb)]
  96. public static extern Error libusb_init(out IntPtr context);
  97. [DllImport(Libusb)]
  98. public static extern void libusb_set_debug(IntPtr context, int level);
  99. [DllImport(Libusb)]
  100. public static extern void libusb_exit(IntPtr context);
  101. [DllImport(Libusb)]
  102. public static extern IntPtr libusb_get_device_list(IntPtr context, out IntPtr list);
  103. [DllImport(Libusb)]
  104. public static extern void libusb_free_device_list(IntPtr context, IntPtr list);
  105. [DllImport(Libusb)]
  106. public static extern IntPtr libusb_ref_device(IntPtr device);
  107. [DllImport(Libusb)]
  108. public static extern void libusb_unref_device(IntPtr device);
  109. [DllImport(Libusb)]
  110. public static extern int libusb_get_max_packet_size(IntPtr device, byte endpoint);
  111. [DllImport(Libusb)]
  112. public static extern Error libusb_open(IntPtr device, out IntPtr deviceHandle);
  113. [DllImport(Libusb)]
  114. public static extern void libusb_close(IntPtr deviceHandle);
  115. [DllImport(Libusb)]
  116. public static extern Error libusb_get_configuration(IntPtr deviceHandle, out int configuration);
  117. [DllImport(Libusb)]
  118. public static extern Error libusb_set_configuration(IntPtr deviceHandle, int configuration);
  119. [DllImport(Libusb)]
  120. public static extern Error libusb_claim_interface(IntPtr deviceHandle, int @interface);
  121. [DllImport(Libusb)]
  122. public static extern Error libusb_release_interface(IntPtr deviceHandle, int @interface);
  123. [DllImport(Libusb)]
  124. public static extern Error libusb_set_interface_alt_setting(IntPtr deviceHandle, int @interface, int altSetting);
  125. [DllImport(Libusb)]
  126. public static extern Error libusb_clear_halt(IntPtr deviceHandle, byte endpoint);
  127. [DllImport(Libusb)]
  128. public static extern Error libusb_reset_device(IntPtr deviceHandle);
  129. [DllImport(Libusb)]
  130. public static extern Error libusb_kernel_driver_active(IntPtr deviceHandle, int @interface);
  131. [DllImport(Libusb)]
  132. public static extern Error libusb_detach_kernel_driver(IntPtr deviceHandle, int @interface);
  133. [DllImport(Libusb)]
  134. public static extern Error libusb_attach_kernel_driver(IntPtr deviceHandle, int @interface);
  135. [DllImport(Libusb)]
  136. public static extern IntPtr libusb_get_version();
  137. [DllImport(Libusb)]
  138. public static extern Error libusb_get_device_descriptor(IntPtr device, out DeviceDescriptor descriptor);
  139. [DllImport(Libusb)]
  140. public static extern Error libusb_get_active_config_descriptor(IntPtr device, out IntPtr configuration);
  141. [DllImport(Libusb)]
  142. public static extern Error libusb_get_config_descriptor_by_value(IntPtr device, byte index, out IntPtr configuration);
  143. [DllImport(Libusb)]
  144. public static extern void libusb_free_config_descriptor(IntPtr configuration);
  145. private static Error libusb_get_descriptor_core(IntPtr deviceHandle, DescriptorType type, byte index, byte[] data, ushort wLength, ushort wIndex)
  146. {
  147. return libusb_control_transfer(deviceHandle,
  148. (byte)EndpointDirection.In, (byte)Request.GetDescriptor,
  149. (ushort)((byte)DescriptorType.String << 8 | index),
  150. wIndex, data, wLength, 1000);
  151. }
  152. public static Error libusb_get_descriptor(IntPtr deviceHandle, DescriptorType type, byte index, byte[] data, ushort wLength)
  153. {
  154. return libusb_get_descriptor_core(deviceHandle,
  155. type, index, data, wLength, 0);
  156. }
  157. public static Error libusb_get_string_descriptor(IntPtr deviceHandle, DescriptorType type, byte index, ushort languageID, byte[] data, ushort wLength)
  158. {
  159. return libusb_get_descriptor_core(deviceHandle,
  160. DescriptorType.String, index,
  161. data, wLength, languageID);
  162. }
  163. [DllImport(Libusb)]
  164. public static extern Error libusb_control_transfer(IntPtr deviceHandle,
  165. byte bmRequestType, byte bRequest,
  166. ushort wValue, ushort wIndex,
  167. byte[] data, ushort wLength,
  168. uint timeout);
  169. [DllImport(Libusb)]
  170. public static extern Error libusb_bulk_transfer(IntPtr deviceHandle,
  171. byte endpoint,
  172. byte[] data, int length,
  173. out int transferred,
  174. uint timeout);
  175. [DllImport(Libusb)]
  176. public static extern Error libusb_interrupt_transfer(IntPtr deviceHandle,
  177. byte endpoint,
  178. byte[] data, int length,
  179. out int transferred,
  180. uint timeout);
  181. }
  182. }