1 /* 2 * darwin backend for libusb 1.0 3 * Copyright © 2008-2023 Nathan Hjelm <[email protected]> 4 * Copyright © 2019-2023 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 #if defined(kIOUSBInterfaceInterfaceID800) 40 #define MAX_INTERFACE_VERSION 800 41 #elif defined(kIOUSBInterfaceInterfaceID700) 42 #define MAX_INTERFACE_VERSION 700 43 #elif defined(kIOUSBInterfaceInterfaceID650) 44 #define MAX_INTERFACE_VERSION 650 45 #elif defined(kIOUSBInterfaceInterfaceID550) 46 #define MAX_INTERFACE_VERSION 550 47 #elif defined(kIOUSBInterfaceInterfaceID245) 48 #define MAX_INTERFACE_VERSION 245 49 #else 50 #define MAX_INTERFACE_VERSION 220 51 #endif 52 53 /* set to the minimum version and casted up as needed. */ 54 typedef IOUSBInterfaceInterface220 **usb_interface_t; 55 56 #define IOINTERFACE0(darwin_interface, version) ((IOUSBInterfaceInterface ## version **) (darwin_interface)->interface) 57 #define IOINTERFACE_V(darwin_interface, version) IOINTERFACE0(darwin_interface, version) 58 #define IOINTERFACE(darwin_interface) ((darwin_interface)->interface) 59 60 /* IOUSBDeviceInterface */ 61 62 #if defined(kIOUSBDeviceInterfaceID650) 63 #define MAX_DEVICE_VERSION 650 64 #elif defined(kIOUSBDeviceInterfaceID500) 65 #define MAX_DEVICE_VERSION 500 66 #elif defined(kIOUSBDeviceInterfaceID320) 67 #define MAX_DEVICE_VERSION 320 68 #elif defined(kIOUSBDeviceInterfaceID300) 69 #define MAX_DEVICE_VERSION 300 70 #elif defined(kIOUSBDeviceInterfaceID245) 71 #define MAX_DEVICE_VERSION 245 72 #else 73 #define MAX_DEVICE_VERSION 197 74 #endif 75 76 /* set to the minimum version and casted up as needed */ 77 typedef IOUSBDeviceInterface197 **usb_device_t; 78 79 #define IODEVICE0(darwin_device, version) ((IOUSBDeviceInterface ## version **)(darwin_device)) 80 #define IODEVICE_V(darwin_device, version) IODEVICE0(darwin_device, version) 81 82 #if !defined(kIOUSBHostInterfaceClassName) 83 #define kIOUSBHostInterfaceClassName "IOUSBHostInterface" 84 #endif 85 86 #if !defined(kUSBHostMatchingPropertyInterfaceNumber) 87 #define kUSBHostMatchingPropertyInterfaceNumber "bInterfaceNumber" 88 #endif 89 90 #if !defined(IO_OBJECT_NULL) 91 #define IO_OBJECT_NULL ((io_object_t) 0) 92 #endif 93 94 /* returns the current macOS version in a format similar to the 95 * MAC_OS_X_VERSION_MIN_REQUIRED macro. 96 * Examples: 97 * 10.1.5 -> 100105 98 * 13.3.0 -> 130300 99 */ 100 uint32_t get_running_version(void); 101 102 typedef IOCFPlugInInterface *io_cf_plugin_ref_t; 103 typedef IONotificationPortRef io_notification_port_t; 104 105 /* private structures */ 106 struct darwin_cached_device { 107 struct list_head list; 108 IOUSBDeviceDescriptor dev_descriptor; 109 UInt32 location; 110 UInt64 parent_session; 111 UInt64 session; 112 USBDeviceAddress address; 113 char sys_path[21]; 114 usb_device_t device; 115 io_service_t service; 116 int open_count; 117 UInt8 first_config, active_config, port; 118 int can_enumerate; 119 int refcount; 120 bool in_reenumerate; 121 int capture_count; 122 }; 123 124 struct darwin_device_priv { 125 struct darwin_cached_device *dev; 126 }; 127 128 struct darwin_device_handle_priv { 129 bool is_open; 130 CFRunLoopSourceRef cfSource; 131 132 struct darwin_interface { 133 usb_interface_t interface; 134 uint8_t num_endpoints; 135 CFRunLoopSourceRef cfSource; 136 uint64_t frames[256]; 137 uint8_t endpoint_addrs[USB_MAXENDPOINTS]; 138 } interfaces[USB_MAXINTERFACES]; 139 }; 140 141 struct darwin_transfer_priv { 142 /* Isoc */ 143 IOUSBIsocFrame *isoc_framelist; 144 int num_iso_packets; 145 146 /* Control */ 147 IOUSBDevRequestTO req; 148 149 /* Bulk */ 150 151 /* Completion status */ 152 IOReturn result; 153 UInt32 size; 154 }; 155 156 #endif 157