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