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