• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 typedef IOCFPlugInInterface *io_cf_plugin_ref_t;
152 typedef IONotificationPortRef io_notification_port_t;
153 
154 /* private structures */
155 struct darwin_cached_device {
156   struct list_head      list;
157   IOUSBDeviceDescriptor dev_descriptor;
158   UInt32                location;
159   UInt64                parent_session;
160   UInt64                session;
161   USBDeviceAddress      address;
162   char                  sys_path[21];
163   usb_device_t        **device;
164   int                   open_count;
165   UInt8                 first_config, active_config, port;
166   int                   can_enumerate;
167   int                   refcount;
168   bool                  in_reenumerate;
169 };
170 
171 struct darwin_device_priv {
172   struct darwin_cached_device *dev;
173 };
174 
175 struct darwin_device_handle_priv {
176   bool                 is_open;
177   CFRunLoopSourceRef   cfSource;
178 
179   struct darwin_interface {
180     usb_interface_t    **interface;
181     uint8_t              num_endpoints;
182     CFRunLoopSourceRef   cfSource;
183     uint64_t             frames[256];
184     uint8_t              endpoint_addrs[USB_MAXENDPOINTS];
185   } interfaces[USB_MAXINTERFACES];
186 };
187 
188 struct darwin_transfer_priv {
189   /* Isoc */
190   IOUSBIsocFrame *isoc_framelist;
191   int num_iso_packets;
192 
193   /* Control */
194   IOUSBDevRequestTO req;
195 
196   /* Bulk */
197 
198   /* Completion status */
199   IOReturn result;
200   UInt32 size;
201 };
202 
203 #endif
204