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 21 #if !defined(LIBUSB_DARWIN_H) 22 #define LIBUSB_DARWIN_H 23 24 #include <stdbool.h> 25 26 #include "libusbi.h" 27 28 #include <IOKit/IOTypes.h> 29 #include <IOKit/IOCFBundle.h> 30 #include <IOKit/usb/IOUSBLib.h> 31 #include <IOKit/IOCFPlugIn.h> 32 33 #if defined(HAVE_IOKIT_USB_IOUSBHOSTFAMILYDEFINITIONS_H) 34 #include <IOKit/usb/IOUSBHostFamilyDefinitions.h> 35 #endif 36 37 /* IOUSBInterfaceInferface */ 38 39 /* New in OS 10.12.0. */ 40 #if defined (kIOUSBInterfaceInterfaceID800) 41 42 #define usb_interface_t IOUSBInterfaceInterface800 43 #define InterfaceInterfaceID kIOUSBInterfaceInterfaceID800 44 #define InterfaceVersion 800 45 46 /* New in OS 10.10.0. */ 47 #elif defined (kIOUSBInterfaceInterfaceID700) 48 49 #define usb_interface_t IOUSBInterfaceInterface700 50 #define InterfaceInterfaceID kIOUSBInterfaceInterfaceID700 51 #define InterfaceVersion 700 52 53 /* New in OS 10.9.0. */ 54 #elif defined (kIOUSBInterfaceInterfaceID650) 55 56 #define usb_interface_t IOUSBInterfaceInterface650 57 #define InterfaceInterfaceID kIOUSBInterfaceInterfaceID650 58 #define InterfaceVersion 650 59 60 /* New in OS 10.8.2 but can't test deployment target to that granularity, so round up. */ 61 #elif defined (kIOUSBInterfaceInterfaceID550) 62 63 #define usb_interface_t IOUSBInterfaceInterface550 64 #define InterfaceInterfaceID kIOUSBInterfaceInterfaceID550 65 #define InterfaceVersion 550 66 67 /* New in OS 10.7.3 but can't test deployment target to that granularity, so round up. */ 68 #elif defined (kIOUSBInterfaceInterfaceID500) 69 70 #define usb_interface_t IOUSBInterfaceInterface500 71 #define InterfaceInterfaceID kIOUSBInterfaceInterfaceID500 72 #define InterfaceVersion 500 73 74 /* New in OS 10.5.0. */ 75 #elif defined (kIOUSBInterfaceInterfaceID300) 76 77 #define usb_interface_t IOUSBInterfaceInterface300 78 #define InterfaceInterfaceID kIOUSBInterfaceInterfaceID300 79 #define InterfaceVersion 300 80 81 /* New in OS 10.4.5 (or 10.4.6?) but can't test deployment target to that granularity, so round up. */ 82 #elif defined (kIOUSBInterfaceInterfaceID245) 83 84 #define usb_interface_t IOUSBInterfaceInterface245 85 #define InterfaceInterfaceID kIOUSBInterfaceInterfaceID245 86 #define InterfaceVersion 245 87 88 /* New in OS 10.4.0. */ 89 #elif defined (kIOUSBInterfaceInterfaceID220) 90 91 #define usb_interface_t IOUSBInterfaceInterface220 92 #define InterfaceInterfaceID kIOUSBInterfaceInterfaceID220 93 #define InterfaceVersion 220 94 95 #else 96 97 #error "IOUSBFamily is too old. Please upgrade your SDK and/or deployment target" 98 99 #endif 100 101 /* IOUSBDeviceInterface */ 102 103 /* New in OS 10.9.0. */ 104 #if defined (kIOUSBDeviceInterfaceID650) 105 106 #define usb_device_t IOUSBDeviceInterface650 107 #define DeviceInterfaceID kIOUSBDeviceInterfaceID650 108 #define DeviceVersion 650 109 110 /* New in OS 10.7.3 but can't test deployment target to that granularity, so round up. */ 111 #elif defined (kIOUSBDeviceInterfaceID500) 112 113 #define usb_device_t IOUSBDeviceInterface500 114 #define DeviceInterfaceID kIOUSBDeviceInterfaceID500 115 #define DeviceVersion 500 116 117 /* New in OS 10.5.4 but can't test deployment target to that granularity, so round up. */ 118 #elif defined (kIOUSBDeviceInterfaceID320) 119 120 #define usb_device_t IOUSBDeviceInterface320 121 #define DeviceInterfaceID kIOUSBDeviceInterfaceID320 122 #define DeviceVersion 320 123 124 /* New in OS 10.5.0. */ 125 #elif defined (kIOUSBDeviceInterfaceID300) 126 127 #define usb_device_t IOUSBDeviceInterface300 128 #define DeviceInterfaceID kIOUSBDeviceInterfaceID300 129 #define DeviceVersion 300 130 131 /* New in OS 10.4.5 (or 10.4.6?) but can't test deployment target to that granularity, so round up. */ 132 #elif defined (kIOUSBDeviceInterfaceID245) 133 134 #define usb_device_t IOUSBDeviceInterface245 135 #define DeviceInterfaceID kIOUSBDeviceInterfaceID245 136 #define DeviceVersion 245 137 138 /* New in OS 10.2.3 but can't test deployment target to that granularity, so round up. */ 139 #elif defined (kIOUSBDeviceInterfaceID197) 140 141 #define usb_device_t IOUSBDeviceInterface197 142 #define DeviceInterfaceID kIOUSBDeviceInterfaceID197 143 #define DeviceVersion 197 144 145 #else 146 147 #error "IOUSBFamily is too old. Please upgrade your SDK and/or deployment target" 148 149 #endif 150 151 #if !defined(kIOUSBHostInterfaceClassName) 152 #define kIOUSBHostInterfaceClassName "IOUSBHostInterface" 153 #endif 154 155 #if !defined(kUSBHostMatchingPropertyInterfaceNumber) 156 #define kUSBHostMatchingPropertyInterfaceNumber "bInterfaceNumber" 157 #endif 158 159 #if !defined(IO_OBJECT_NULL) 160 #define IO_OBJECT_NULL ((io_object_t) 0) 161 #endif 162 163 /* Testing availability */ 164 #ifndef __has_builtin 165 #define __has_builtin(x) 0 // Compatibility with non-clang compilers. 166 #endif 167 #if __has_builtin(__builtin_available) 168 #define HAS_CAPTURE_DEVICE() __builtin_available(macOS 10.10, *) 169 #else 170 #define HAS_CAPTURE_DEVICE() 0 171 #endif 172 173 typedef IOCFPlugInInterface *io_cf_plugin_ref_t; 174 typedef IONotificationPortRef io_notification_port_t; 175 176 /* private structures */ 177 struct darwin_cached_device { 178 struct list_head list; 179 IOUSBDeviceDescriptor dev_descriptor; 180 UInt32 location; 181 UInt64 parent_session; 182 UInt64 session; 183 USBDeviceAddress address; 184 char sys_path[21]; 185 usb_device_t **device; 186 io_service_t service; 187 int open_count; 188 UInt8 first_config, active_config, port; 189 int can_enumerate; 190 int refcount; 191 bool in_reenumerate; 192 int capture_count; 193 }; 194 195 struct darwin_device_priv { 196 struct darwin_cached_device *dev; 197 }; 198 199 struct darwin_device_handle_priv { 200 bool is_open; 201 CFRunLoopSourceRef cfSource; 202 203 struct darwin_interface { 204 usb_interface_t **interface; 205 uint8_t num_endpoints; 206 CFRunLoopSourceRef cfSource; 207 uint64_t frames[256]; 208 uint8_t endpoint_addrs[USB_MAXENDPOINTS]; 209 } interfaces[USB_MAXINTERFACES]; 210 }; 211 212 struct darwin_transfer_priv { 213 /* Isoc */ 214 IOUSBIsocFrame *isoc_framelist; 215 int num_iso_packets; 216 217 /* Control */ 218 IOUSBDevRequestTO req; 219 220 /* Bulk */ 221 222 /* Completion status */ 223 IOReturn result; 224 UInt32 size; 225 }; 226 227 #endif 228