darwin_usb.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * darwin backend for libusb 1.0
  3. * Copyright © 2008-2019 Nathan Hjelm <hjelmn@users.sourceforge.net>
  4. * Copyright © 2019 Google LLC. All rights reserved.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #if !defined(LIBUSB_DARWIN_H)
  21. #define LIBUSB_DARWIN_H
  22. #include <stdbool.h>
  23. #include "libusbi.h"
  24. #include <IOKit/IOTypes.h>
  25. #include <IOKit/IOCFBundle.h>
  26. #include <IOKit/usb/IOUSBLib.h>
  27. #include <IOKit/IOCFPlugIn.h>
  28. #if defined(HAVE_IOKIT_USB_IOUSBHOSTFAMILYDEFINITIONS_H)
  29. #include <IOKit/usb/IOUSBHostFamilyDefinitions.h>
  30. #endif
  31. /* IOUSBInterfaceInferface */
  32. /* New in OS 10.12.0. */
  33. #if defined (kIOUSBInterfaceInterfaceID800)
  34. #define usb_interface_t IOUSBInterfaceInterface800
  35. #define InterfaceInterfaceID kIOUSBInterfaceInterfaceID800
  36. #define InterfaceVersion 800
  37. /* New in OS 10.10.0. */
  38. #elif defined (kIOUSBInterfaceInterfaceID700)
  39. #define usb_interface_t IOUSBInterfaceInterface700
  40. #define InterfaceInterfaceID kIOUSBInterfaceInterfaceID700
  41. #define InterfaceVersion 700
  42. /* New in OS 10.9.0. */
  43. #elif defined (kIOUSBInterfaceInterfaceID650)
  44. #define usb_interface_t IOUSBInterfaceInterface650
  45. #define InterfaceInterfaceID kIOUSBInterfaceInterfaceID650
  46. #define InterfaceVersion 650
  47. /* New in OS 10.8.2 but can't test deployment target to that granularity, so round up. */
  48. #elif defined (kIOUSBInterfaceInterfaceID550)
  49. #define usb_interface_t IOUSBInterfaceInterface550
  50. #define InterfaceInterfaceID kIOUSBInterfaceInterfaceID550
  51. #define InterfaceVersion 550
  52. /* New in OS 10.7.3 but can't test deployment target to that granularity, so round up. */
  53. #elif defined (kIOUSBInterfaceInterfaceID500)
  54. #define usb_interface_t IOUSBInterfaceInterface500
  55. #define InterfaceInterfaceID kIOUSBInterfaceInterfaceID500
  56. #define InterfaceVersion 500
  57. /* New in OS 10.5.0. */
  58. #elif defined (kIOUSBInterfaceInterfaceID300)
  59. #define usb_interface_t IOUSBInterfaceInterface300
  60. #define InterfaceInterfaceID kIOUSBInterfaceInterfaceID300
  61. #define InterfaceVersion 300
  62. /* New in OS 10.4.5 (or 10.4.6?) but can't test deployment target to that granularity, so round up. */
  63. #elif defined (kIOUSBInterfaceInterfaceID245)
  64. #define usb_interface_t IOUSBInterfaceInterface245
  65. #define InterfaceInterfaceID kIOUSBInterfaceInterfaceID245
  66. #define InterfaceVersion 245
  67. /* New in OS 10.4.0. */
  68. #elif defined (kIOUSBInterfaceInterfaceID220)
  69. #define usb_interface_t IOUSBInterfaceInterface220
  70. #define InterfaceInterfaceID kIOUSBInterfaceInterfaceID220
  71. #define InterfaceVersion 220
  72. #else
  73. #error "IOUSBFamily is too old. Please upgrade your SDK and/or deployment target"
  74. #endif
  75. /* IOUSBDeviceInterface */
  76. /* New in OS 10.9.0. */
  77. #if defined (kIOUSBDeviceInterfaceID650)
  78. #define usb_device_t IOUSBDeviceInterface650
  79. #define DeviceInterfaceID kIOUSBDeviceInterfaceID650
  80. #define DeviceVersion 650
  81. /* New in OS 10.7.3 but can't test deployment target to that granularity, so round up. */
  82. #elif defined (kIOUSBDeviceInterfaceID500)
  83. #define usb_device_t IOUSBDeviceInterface500
  84. #define DeviceInterfaceID kIOUSBDeviceInterfaceID500
  85. #define DeviceVersion 500
  86. /* New in OS 10.5.4 but can't test deployment target to that granularity, so round up. */
  87. #elif defined (kIOUSBDeviceInterfaceID320)
  88. #define usb_device_t IOUSBDeviceInterface320
  89. #define DeviceInterfaceID kIOUSBDeviceInterfaceID320
  90. #define DeviceVersion 320
  91. /* New in OS 10.5.0. */
  92. #elif defined (kIOUSBDeviceInterfaceID300)
  93. #define usb_device_t IOUSBDeviceInterface300
  94. #define DeviceInterfaceID kIOUSBDeviceInterfaceID300
  95. #define DeviceVersion 300
  96. /* New in OS 10.4.5 (or 10.4.6?) but can't test deployment target to that granularity, so round up. */
  97. #elif defined (kIOUSBDeviceInterfaceID245)
  98. #define usb_device_t IOUSBDeviceInterface245
  99. #define DeviceInterfaceID kIOUSBDeviceInterfaceID245
  100. #define DeviceVersion 245
  101. /* New in OS 10.2.3 but can't test deployment target to that granularity, so round up. */
  102. #elif defined (kIOUSBDeviceInterfaceID197)
  103. #define usb_device_t IOUSBDeviceInterface197
  104. #define DeviceInterfaceID kIOUSBDeviceInterfaceID197
  105. #define DeviceVersion 197
  106. #else
  107. #error "IOUSBFamily is too old. Please upgrade your SDK and/or deployment target"
  108. #endif
  109. #if !defined(kIOUSBHostInterfaceClassName)
  110. #define kIOUSBHostInterfaceClassName "IOUSBHostInterface"
  111. #endif
  112. #if !defined(kUSBHostMatchingPropertyInterfaceNumber)
  113. #define kUSBHostMatchingPropertyInterfaceNumber "bInterfaceNumber"
  114. #endif
  115. #if !defined(IO_OBJECT_NULL)
  116. #define IO_OBJECT_NULL ((io_object_t) 0)
  117. #endif
  118. /* Testing availability */
  119. #ifndef __has_builtin
  120. #define __has_builtin(x) 0 // Compatibility with non-clang compilers.
  121. #endif
  122. #if __has_builtin(__builtin_available)
  123. #define HAS_CAPTURE_DEVICE() __builtin_available(macOS 10.10, *)
  124. #else
  125. #define HAS_CAPTURE_DEVICE() 0
  126. #endif
  127. typedef IOCFPlugInInterface *io_cf_plugin_ref_t;
  128. typedef IONotificationPortRef io_notification_port_t;
  129. /* private structures */
  130. struct darwin_cached_device {
  131. struct list_head list;
  132. IOUSBDeviceDescriptor dev_descriptor;
  133. UInt32 location;
  134. UInt64 parent_session;
  135. UInt64 session;
  136. USBDeviceAddress address;
  137. char sys_path[21];
  138. usb_device_t **device;
  139. io_service_t service;
  140. int open_count;
  141. UInt8 first_config, active_config, port;
  142. int can_enumerate;
  143. int refcount;
  144. bool in_reenumerate;
  145. int capture_count;
  146. };
  147. struct darwin_device_priv {
  148. struct darwin_cached_device *dev;
  149. };
  150. struct darwin_device_handle_priv {
  151. bool is_open;
  152. CFRunLoopSourceRef cfSource;
  153. struct darwin_interface {
  154. usb_interface_t **interface;
  155. uint8_t num_endpoints;
  156. CFRunLoopSourceRef cfSource;
  157. uint64_t frames[256];
  158. uint8_t endpoint_addrs[USB_MAXENDPOINTS];
  159. } interfaces[USB_MAXINTERFACES];
  160. };
  161. struct darwin_transfer_priv {
  162. /* Isoc */
  163. IOUSBIsocFrame *isoc_framelist;
  164. int num_iso_packets;
  165. /* Control */
  166. IOUSBDevRequestTO req;
  167. /* Bulk */
  168. /* Completion status */
  169. IOReturn result;
  170. UInt32 size;
  171. };
  172. #endif