Fix build failure on Mac OS X 10.7
[platform/upstream/libusb.git] / libusb / os / darwin_usb.c
1 /* -*- Mode: C; indent-tabs-mode:nil -*- */
2 /*
3  * darwin backend for libusb 1.0
4  * Copyright © 2008-2020 Nathan Hjelm <hjelmn@cs.unm.edu>
5  * Copyright © 2019-2020 Google LLC. All rights reserved.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include <config.h>
23 #include <assert.h>
24 #include <time.h>
25 #include <ctype.h>
26 #include <pthread.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <sys/sysctl.h>
34
35 #include <mach/clock.h>
36 #include <mach/clock_types.h>
37 #include <mach/mach_host.h>
38 #include <mach/mach_port.h>
39
40 /* Suppress warnings about the use of the deprecated objc_registerThreadWithCollector
41  * function. Its use is also conditionalized to only older deployment targets. */
42 #define OBJC_SILENCE_GC_DEPRECATIONS 1
43
44 /* Default timeout to 10s for reenumerate. This is needed because USBDeviceReEnumerate
45  * does not return error status on macOS. */
46 #define DARWIN_REENUMERATE_TIMEOUT_US 10000000
47
48 #include <AvailabilityMacros.h>
49 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060 && MAC_OS_X_VERSION_MIN_REQUIRED < 101200
50   #include <objc/objc-auto.h>
51 #endif
52
53 #include "darwin_usb.h"
54
55 static int init_count = 0;
56
57 /* async event thread */
58 static pthread_mutex_t libusb_darwin_at_mutex = PTHREAD_MUTEX_INITIALIZER;
59 static pthread_cond_t  libusb_darwin_at_cond = PTHREAD_COND_INITIALIZER;
60
61 #if !defined(HAVE_CLOCK_GETTIME)
62 static clock_serv_t clock_realtime;
63 static clock_serv_t clock_monotonic;
64 #endif
65
66 #define LIBUSB_DARWIN_STARTUP_FAILURE ((CFRunLoopRef) -1)
67
68 static CFRunLoopRef libusb_darwin_acfl = NULL; /* event cf loop */
69 static CFRunLoopSourceRef libusb_darwin_acfls = NULL; /* shutdown signal for event cf loop */
70
71 static usbi_mutex_t darwin_cached_devices_lock = PTHREAD_MUTEX_INITIALIZER;
72 static struct list_head darwin_cached_devices;
73 static const char *darwin_device_class = "IOUSBDevice";
74
75 #define DARWIN_CACHED_DEVICE(a) (((struct darwin_device_priv *)usbi_get_device_priv((a)))->dev)
76
77 /* async event thread */
78 static pthread_t libusb_darwin_at;
79
80 static int darwin_get_config_descriptor(struct libusb_device *dev, uint8_t config_index, void *buffer, size_t len);
81 static int darwin_claim_interface(struct libusb_device_handle *dev_handle, uint8_t iface);
82 static int darwin_release_interface(struct libusb_device_handle *dev_handle, uint8_t iface);
83 static int darwin_reenumerate_device(struct libusb_device_handle *dev_handle, bool capture);
84 static int darwin_clear_halt(struct libusb_device_handle *dev_handle, unsigned char endpoint);
85 static int darwin_reset_device(struct libusb_device_handle *dev_handle);
86 static void darwin_async_io_callback (void *refcon, IOReturn result, void *arg0);
87
88 static enum libusb_error darwin_scan_devices(struct libusb_context *ctx);
89 static enum libusb_error process_new_device (struct libusb_context *ctx, struct darwin_cached_device *cached_device,
90                                              UInt64 old_session_id);
91
92 static enum libusb_error darwin_get_cached_device(io_service_t service, struct darwin_cached_device **cached_out,
93                                                   UInt64 *old_session_id);
94
95 #if defined(ENABLE_LOGGING)
96 static const char *darwin_error_str (IOReturn result) {
97   static char string_buffer[50];
98   switch (result) {
99   case kIOReturnSuccess:
100     return "no error";
101   case kIOReturnNotOpen:
102     return "device not opened for exclusive access";
103   case kIOReturnNoDevice:
104     return "no connection to an IOService";
105   case kIOUSBNoAsyncPortErr:
106     return "no async port has been opened for interface";
107   case kIOReturnExclusiveAccess:
108     return "another process has device opened for exclusive access";
109   case kIOUSBPipeStalled:
110     return "pipe is stalled";
111   case kIOReturnError:
112     return "could not establish a connection to the Darwin kernel";
113   case kIOUSBTransactionTimeout:
114     return "transaction timed out";
115   case kIOReturnBadArgument:
116     return "invalid argument";
117   case kIOReturnAborted:
118     return "transaction aborted";
119   case kIOReturnNotResponding:
120     return "device not responding";
121   case kIOReturnOverrun:
122     return "data overrun";
123   case kIOReturnCannotWire:
124     return "physical memory can not be wired down";
125   case kIOReturnNoResources:
126     return "out of resources";
127   case kIOUSBHighSpeedSplitError:
128     return "high speed split error";
129   case kIOUSBUnknownPipeErr:
130     return "pipe ref not recognized";
131   default:
132     snprintf(string_buffer, sizeof(string_buffer), "unknown error (0x%x)", result);
133     return string_buffer;
134   }
135 }
136 #endif
137
138 static enum libusb_error darwin_to_libusb (IOReturn result) {
139   switch (result) {
140   case kIOReturnUnderrun:
141   case kIOReturnSuccess:
142     return LIBUSB_SUCCESS;
143   case kIOReturnNotOpen:
144   case kIOReturnNoDevice:
145     return LIBUSB_ERROR_NO_DEVICE;
146   case kIOReturnExclusiveAccess:
147     return LIBUSB_ERROR_ACCESS;
148   case kIOUSBPipeStalled:
149     return LIBUSB_ERROR_PIPE;
150   case kIOReturnBadArgument:
151     return LIBUSB_ERROR_INVALID_PARAM;
152   case kIOUSBTransactionTimeout:
153     return LIBUSB_ERROR_TIMEOUT;
154   case kIOReturnNotResponding:
155   case kIOReturnAborted:
156   case kIOReturnError:
157   case kIOUSBNoAsyncPortErr:
158   case kIOUSBUnknownPipeErr:
159   default:
160     return LIBUSB_ERROR_OTHER;
161   }
162 }
163
164 /* this function must be called with the darwin_cached_devices_lock held */
165 static void darwin_deref_cached_device(struct darwin_cached_device *cached_dev) {
166   cached_dev->refcount--;
167   /* free the device and remove it from the cache */
168   if (0 == cached_dev->refcount) {
169     list_del(&cached_dev->list);
170
171     if (cached_dev->device) {
172       (*(cached_dev->device))->Release(cached_dev->device);
173       cached_dev->device = NULL;
174     }
175     IOObjectRelease (cached_dev->service);
176     free (cached_dev);
177   }
178 }
179
180 static void darwin_ref_cached_device(struct darwin_cached_device *cached_dev) {
181   cached_dev->refcount++;
182 }
183
184 static int ep_to_pipeRef(struct libusb_device_handle *dev_handle, uint8_t ep, uint8_t *pipep, uint8_t *ifcp, struct darwin_interface **interface_out) {
185   struct darwin_device_handle_priv *priv = usbi_get_device_handle_priv(dev_handle);
186
187   /* current interface */
188   struct darwin_interface *cInterface;
189
190   uint8_t i, iface;
191
192   usbi_dbg ("converting ep address 0x%02x to pipeRef and interface", ep);
193
194   for (iface = 0 ; iface < USB_MAXINTERFACES ; iface++) {
195     cInterface = &priv->interfaces[iface];
196
197     if (dev_handle->claimed_interfaces & (1U << iface)) {
198       for (i = 0 ; i < cInterface->num_endpoints ; i++) {
199         if (cInterface->endpoint_addrs[i] == ep) {
200           *pipep = i + 1;
201
202           if (ifcp)
203             *ifcp = iface;
204
205           if (interface_out)
206             *interface_out = cInterface;
207
208           usbi_dbg ("pipe %d on interface %d matches", *pipep, iface);
209           return LIBUSB_SUCCESS;
210         }
211       }
212     }
213   }
214
215   /* No pipe found with the correct endpoint address */
216   usbi_warn (HANDLE_CTX(dev_handle), "no pipeRef found with endpoint address 0x%02x.", ep);
217
218   return LIBUSB_ERROR_NOT_FOUND;
219 }
220
221 static IOReturn usb_setup_device_iterator (io_iterator_t *deviceIterator, UInt32 location) {
222   CFMutableDictionaryRef matchingDict = IOServiceMatching(darwin_device_class);
223
224   if (!matchingDict)
225     return kIOReturnError;
226
227   if (location) {
228     CFMutableDictionaryRef propertyMatchDict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
229                                                                          &kCFTypeDictionaryKeyCallBacks,
230                                                                          &kCFTypeDictionaryValueCallBacks);
231
232     /* there are no unsigned CFNumber types so treat the value as signed. the OS seems to do this
233          internally (CFNumberType of locationID is kCFNumberSInt32Type) */
234     CFTypeRef locationCF = CFNumberCreate (NULL, kCFNumberSInt32Type, &location);
235
236     if (propertyMatchDict && locationCF) {
237       CFDictionarySetValue (propertyMatchDict, CFSTR(kUSBDevicePropertyLocationID), locationCF);
238       CFDictionarySetValue (matchingDict, CFSTR(kIOPropertyMatchKey), propertyMatchDict);
239     }
240     /* else we can still proceed as long as the caller accounts for the possibility of other devices in the iterator */
241
242     /* release our references as per the Create Rule */
243     if (propertyMatchDict)
244       CFRelease (propertyMatchDict);
245     if (locationCF)
246       CFRelease (locationCF);
247   }
248
249   return IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, deviceIterator);
250 }
251
252 /* Returns 1 on success, 0 on failure. */
253 static bool get_ioregistry_value_number (io_service_t service, CFStringRef property, CFNumberType type, void *p) {
254   CFTypeRef cfNumber = IORegistryEntryCreateCFProperty (service, property, kCFAllocatorDefault, 0);
255   Boolean success = 0;
256
257   if (cfNumber) {
258     if (CFGetTypeID(cfNumber) == CFNumberGetTypeID()) {
259       success = CFNumberGetValue(cfNumber, type, p);
260     }
261
262     CFRelease (cfNumber);
263   }
264
265   return (success != 0);
266 }
267
268 /* Returns 1 on success, 0 on failure. */
269 static bool get_ioregistry_value_data (io_service_t service, CFStringRef property, ssize_t size, void *p) {
270   CFTypeRef cfData = IORegistryEntryCreateCFProperty (service, property, kCFAllocatorDefault, 0);
271   bool success = false;
272
273   if (cfData) {
274     if (CFGetTypeID (cfData) == CFDataGetTypeID ()) {
275       CFIndex length = CFDataGetLength (cfData);
276       if (length < size) {
277         size = length;
278       }
279
280       CFDataGetBytes (cfData, CFRangeMake(0, size), p);
281       success = true;
282     }
283
284     CFRelease (cfData);
285   }
286
287   return success;
288 }
289
290 static usb_device_t **darwin_device_from_service (io_service_t service)
291 {
292   io_cf_plugin_ref_t *plugInInterface = NULL;
293   usb_device_t **device;
294   IOReturn kresult;
295   SInt32 score;
296   const int max_retries = 5;
297
298   /* The IOCreatePlugInInterfaceForService function might consistently return
299      an "out of resources" error with certain USB devices the first time we run 
300      it. The reason is still unclear, but retrying fixes the problem */
301   for (int count = 0; count < max_retries; count++) {
302     kresult = IOCreatePlugInInterfaceForService(service, kIOUSBDeviceUserClientTypeID,
303                                                 kIOCFPlugInInterfaceID, &plugInInterface,
304                                                 &score);
305     if (kIOReturnSuccess == kresult && plugInInterface) {
306       break;
307     }
308
309     usbi_dbg ("set up plugin for service retry: %s", darwin_error_str (kresult));
310
311     /* sleep for a little while before trying again */
312     nanosleep(&(struct timespec){.tv_sec = 0, .tv_nsec = 1000}, NULL);
313   }
314
315   if (kIOReturnSuccess != kresult || !plugInInterface) {
316     usbi_dbg ("could not set up plugin for service: %s", darwin_error_str (kresult));
317     return NULL;
318   }
319
320   (void)(*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(DeviceInterfaceID),
321                                            (LPVOID)&device);
322   /* Use release instead of IODestroyPlugInInterface to avoid stopping IOServices associated with this device */
323   (*plugInInterface)->Release (plugInInterface);
324
325   return device;
326 }
327
328 static void darwin_devices_attached (void *ptr, io_iterator_t add_devices) {
329   UNUSED(ptr);
330   struct darwin_cached_device *cached_device;
331   UInt64 old_session_id;
332   struct libusb_context *ctx;
333   io_service_t service;
334   int ret;
335
336   usbi_mutex_lock(&active_contexts_lock);
337
338   while ((service = IOIteratorNext(add_devices))) {
339     ret = darwin_get_cached_device (service, &cached_device, &old_session_id);
340     if (ret < 0 || !cached_device->can_enumerate) {
341       continue;
342     }
343
344     /* add this device to each active context's device list */
345     for_each_context(ctx) {
346       process_new_device (ctx, cached_device, old_session_id);
347     }
348
349     if (cached_device->in_reenumerate) {
350       usbi_dbg ("cached device in reset state. reset complete...");
351       cached_device->in_reenumerate = false;
352     }
353
354     IOObjectRelease(service);
355   }
356
357   usbi_mutex_unlock(&active_contexts_lock);
358 }
359
360 static void darwin_devices_detached (void *ptr, io_iterator_t rem_devices) {
361   UNUSED(ptr);
362   struct libusb_device *dev = NULL;
363   struct libusb_context *ctx;
364   struct darwin_cached_device *old_device;
365
366   io_service_t device;
367   UInt64 session;
368   int ret;
369
370   usbi_mutex_lock(&active_contexts_lock);
371
372   while ((device = IOIteratorNext (rem_devices)) != 0) {
373     bool is_reenumerating = false;
374
375     /* get the location from the i/o registry */
376     ret = get_ioregistry_value_number (device, CFSTR("sessionID"), kCFNumberSInt64Type, &session);
377     IOObjectRelease (device);
378     if (!ret)
379       continue;
380
381     /* we need to match darwin_ref_cached_device call made in darwin_get_cached_device function
382        otherwise no cached device will ever get freed */
383     usbi_mutex_lock(&darwin_cached_devices_lock);
384     list_for_each_entry(old_device, &darwin_cached_devices, list, struct darwin_cached_device) {
385       if (old_device->session == session) {
386         if (old_device->in_reenumerate) {
387           /* device is re-enumerating. do not dereference the device at this time. libusb_reset_device()
388            * will deref if needed. */
389           usbi_dbg ("detected device detached due to re-enumeration");
390
391           /* the device object is no longer usable so go ahead and release it */
392           if (old_device->device) {
393             (*(old_device->device))->Release(old_device->device);
394             old_device->device = NULL;
395           }
396
397           is_reenumerating = true;
398         } else {
399           darwin_deref_cached_device (old_device);
400         }
401
402         break;
403       }
404     }
405
406     usbi_mutex_unlock(&darwin_cached_devices_lock);
407     if (is_reenumerating) {
408       continue;
409     }
410
411     for_each_context(ctx) {
412       usbi_dbg ("notifying context %p of device disconnect", ctx);
413
414       dev = usbi_get_device_by_session_id(ctx, (unsigned long) session);
415       if (dev) {
416         /* signal the core that this device has been disconnected. the core will tear down this device
417            when the reference count reaches 0 */
418         usbi_disconnect_device(dev);
419         libusb_unref_device(dev);
420       }
421     }
422   }
423
424   usbi_mutex_unlock(&active_contexts_lock);
425 }
426
427 static void darwin_hotplug_poll (void)
428 {
429   /* not sure if 1 ms will be too long/short but it should work ok */
430   mach_timespec_t timeout = {.tv_sec = 0, .tv_nsec = 1000000ul};
431
432   /* since a kernel thread may notify the IOIterators used for
433    * hotplug notification we can't just clear the iterators.
434    * instead just wait until all IOService providers are quiet */
435   (void) IOKitWaitQuiet (kIOMasterPortDefault, &timeout);
436 }
437
438 static void darwin_clear_iterator (io_iterator_t iter) {
439   io_service_t device;
440
441   while ((device = IOIteratorNext (iter)) != 0)
442     IOObjectRelease (device);
443 }
444
445 static void darwin_fail_startup(void) {
446   pthread_mutex_lock (&libusb_darwin_at_mutex);
447   libusb_darwin_acfl = LIBUSB_DARWIN_STARTUP_FAILURE;
448   pthread_cond_signal (&libusb_darwin_at_cond);
449   pthread_mutex_unlock (&libusb_darwin_at_mutex);
450   pthread_exit (NULL);
451 }
452
453 static void *darwin_event_thread_main (void *arg0) {
454   IOReturn kresult;
455   struct libusb_context *ctx = (struct libusb_context *)arg0;
456   CFRunLoopRef runloop;
457   CFRunLoopSourceRef libusb_shutdown_cfsource;
458   CFRunLoopSourceContext libusb_shutdown_cfsourcectx;
459
460 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
461   /* Set this thread's name, so it can be seen in the debugger
462      and crash reports. */
463   pthread_setname_np ("org.libusb.device-hotplug");
464 #endif
465
466 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060 && MAC_OS_X_VERSION_MIN_REQUIRED < 101200
467   /* Tell the Objective-C garbage collector about this thread.
468      This is required because, unlike NSThreads, pthreads are
469      not automatically registered. Although we don't use
470      Objective-C, we use CoreFoundation, which does.
471      Garbage collection support was entirely removed in 10.12,
472      so don't bother there. */
473   objc_registerThreadWithCollector();
474 #endif
475
476   /* hotplug (device arrival/removal) sources */
477   CFRunLoopSourceRef     libusb_notification_cfsource;
478   io_notification_port_t libusb_notification_port;
479   io_iterator_t          libusb_rem_device_iterator;
480   io_iterator_t          libusb_add_device_iterator;
481
482   usbi_dbg ("creating hotplug event source");
483
484   runloop = CFRunLoopGetCurrent ();
485   CFRetain (runloop);
486
487   /* add the shutdown cfsource to the run loop */
488   memset(&libusb_shutdown_cfsourcectx, 0, sizeof(libusb_shutdown_cfsourcectx));
489   libusb_shutdown_cfsourcectx.info = runloop;
490   libusb_shutdown_cfsourcectx.perform = (void (*)(void *))CFRunLoopStop;
491   libusb_shutdown_cfsource = CFRunLoopSourceCreate(NULL, 0, &libusb_shutdown_cfsourcectx);
492   CFRunLoopAddSource(runloop, libusb_shutdown_cfsource, kCFRunLoopDefaultMode);
493
494   /* add the notification port to the run loop */
495   libusb_notification_port     = IONotificationPortCreate (kIOMasterPortDefault);
496   libusb_notification_cfsource = IONotificationPortGetRunLoopSource (libusb_notification_port);
497   CFRunLoopAddSource(runloop, libusb_notification_cfsource, kCFRunLoopDefaultMode);
498
499   /* create notifications for removed devices */
500   kresult = IOServiceAddMatchingNotification (libusb_notification_port, kIOTerminatedNotification,
501                                               IOServiceMatching(darwin_device_class),
502                                               darwin_devices_detached,
503                                               ctx, &libusb_rem_device_iterator);
504
505   if (kresult != kIOReturnSuccess) {
506     usbi_err (ctx, "could not add hotplug event source: %s", darwin_error_str (kresult));
507     CFRelease (libusb_shutdown_cfsource);
508     CFRelease (runloop);
509     darwin_fail_startup ();
510   }
511
512   /* create notifications for attached devices */
513   kresult = IOServiceAddMatchingNotification(libusb_notification_port, kIOFirstMatchNotification,
514                                               IOServiceMatching(darwin_device_class),
515                                               darwin_devices_attached,
516                                               ctx, &libusb_add_device_iterator);
517
518   if (kresult != kIOReturnSuccess) {
519     usbi_err (ctx, "could not add hotplug event source: %s", darwin_error_str (kresult));
520     CFRelease (libusb_shutdown_cfsource);
521     CFRelease (runloop);
522     darwin_fail_startup ();
523   }
524
525   /* arm notifiers */
526   darwin_clear_iterator (libusb_rem_device_iterator);
527   darwin_clear_iterator (libusb_add_device_iterator);
528
529   usbi_dbg ("darwin event thread ready to receive events");
530
531   /* signal the main thread that the hotplug runloop has been created. */
532   pthread_mutex_lock (&libusb_darwin_at_mutex);
533   libusb_darwin_acfl = runloop;
534   libusb_darwin_acfls = libusb_shutdown_cfsource;
535   pthread_cond_signal (&libusb_darwin_at_cond);
536   pthread_mutex_unlock (&libusb_darwin_at_mutex);
537
538   /* run the runloop */
539   CFRunLoopRun();
540
541   usbi_dbg ("darwin event thread exiting");
542
543   /* signal the main thread that the hotplug runloop has finished. */
544   pthread_mutex_lock (&libusb_darwin_at_mutex);
545   libusb_darwin_acfls = NULL;
546   libusb_darwin_acfl = NULL;
547   pthread_cond_signal (&libusb_darwin_at_cond);
548   pthread_mutex_unlock (&libusb_darwin_at_mutex);
549
550   /* remove the notification cfsource */
551   CFRunLoopRemoveSource(runloop, libusb_notification_cfsource, kCFRunLoopDefaultMode);
552
553   /* remove the shutdown cfsource */
554   CFRunLoopRemoveSource(runloop, libusb_shutdown_cfsource, kCFRunLoopDefaultMode);
555
556   /* delete notification port */
557   IONotificationPortDestroy (libusb_notification_port);
558
559   /* delete iterators */
560   IOObjectRelease (libusb_rem_device_iterator);
561   IOObjectRelease (libusb_add_device_iterator);
562
563   CFRelease (libusb_shutdown_cfsource);
564   CFRelease (runloop);
565
566   pthread_exit (NULL);
567 }
568
569 /* cleanup function to destroy cached devices */
570 static void darwin_cleanup_devices(void) {
571   struct darwin_cached_device *dev, *next;
572
573   list_for_each_entry_safe(dev, next, &darwin_cached_devices, list, struct darwin_cached_device) {
574     darwin_deref_cached_device(dev);
575   }
576 }
577
578 static int darwin_init(struct libusb_context *ctx) {
579   bool first_init;
580   int rc;
581
582   first_init = (1 == ++init_count);
583
584   do {
585     if (first_init) {
586       if (NULL == darwin_cached_devices.next) {
587         list_init (&darwin_cached_devices);
588       }
589       assert(list_empty(&darwin_cached_devices));
590 #if !defined(HAVE_CLOCK_GETTIME)
591       /* create the clocks that will be used if clock_gettime() is not available */
592       host_name_port_t host_self;
593
594       host_self = mach_host_self();
595       host_get_clock_service(host_self, CALENDAR_CLOCK, &clock_realtime);
596       host_get_clock_service(host_self, SYSTEM_CLOCK, &clock_monotonic);
597       mach_port_deallocate(mach_task_self(), host_self);
598 #endif
599     }
600
601     rc = darwin_scan_devices (ctx);
602     if (LIBUSB_SUCCESS != rc)
603       break;
604
605     if (first_init) {
606       rc = pthread_create (&libusb_darwin_at, NULL, darwin_event_thread_main, ctx);
607       if (0 != rc) {
608         usbi_err (ctx, "could not create event thread, error %d", rc);
609         rc = LIBUSB_ERROR_OTHER;
610         break;
611       }
612
613       pthread_mutex_lock (&libusb_darwin_at_mutex);
614       while (!libusb_darwin_acfl)
615         pthread_cond_wait (&libusb_darwin_at_cond, &libusb_darwin_at_mutex);
616       if (libusb_darwin_acfl == LIBUSB_DARWIN_STARTUP_FAILURE) {
617         libusb_darwin_acfl = NULL;
618         rc = LIBUSB_ERROR_OTHER;
619       }
620       pthread_mutex_unlock (&libusb_darwin_at_mutex);
621
622       if (0 != rc)
623         pthread_join (libusb_darwin_at, NULL);
624     }
625   } while (0);
626
627   if (LIBUSB_SUCCESS != rc) {
628     if (first_init) {
629       darwin_cleanup_devices ();
630 #if !defined(HAVE_CLOCK_GETTIME)
631       mach_port_deallocate(mach_task_self(), clock_realtime);
632       mach_port_deallocate(mach_task_self(), clock_monotonic);
633 #endif
634     }
635     --init_count;
636   }
637
638   return rc;
639 }
640
641 static void darwin_exit (struct libusb_context *ctx) {
642   UNUSED(ctx);
643
644   if (0 == --init_count) {
645     /* stop the event runloop and wait for the thread to terminate. */
646     pthread_mutex_lock (&libusb_darwin_at_mutex);
647     CFRunLoopSourceSignal (libusb_darwin_acfls);
648     CFRunLoopWakeUp (libusb_darwin_acfl);
649     while (libusb_darwin_acfl)
650       pthread_cond_wait (&libusb_darwin_at_cond, &libusb_darwin_at_mutex);
651     pthread_mutex_unlock (&libusb_darwin_at_mutex);
652     pthread_join (libusb_darwin_at, NULL);
653
654     darwin_cleanup_devices ();
655
656 #if !defined(HAVE_CLOCK_GETTIME)
657     mach_port_deallocate(mach_task_self(), clock_realtime);
658     mach_port_deallocate(mach_task_self(), clock_monotonic);
659 #endif
660   }
661 }
662
663 static int get_configuration_index (struct libusb_device *dev, UInt8 config_value) {
664   struct darwin_cached_device *priv = DARWIN_CACHED_DEVICE(dev);
665   UInt8 i, numConfig;
666   IOUSBConfigurationDescriptorPtr desc;
667   IOReturn kresult;
668
669   /* is there a simpler way to determine the index? */
670   kresult = (*(priv->device))->GetNumberOfConfigurations (priv->device, &numConfig);
671   if (kresult != kIOReturnSuccess)
672     return darwin_to_libusb (kresult);
673
674   for (i = 0 ; i < numConfig ; i++) {
675     (*(priv->device))->GetConfigurationDescriptorPtr (priv->device, i, &desc);
676
677     if (desc->bConfigurationValue == config_value)
678       return i;
679   }
680
681   /* configuration not found */
682   return LIBUSB_ERROR_NOT_FOUND;
683 }
684
685 static int darwin_get_active_config_descriptor(struct libusb_device *dev, void *buffer, size_t len) {
686   struct darwin_cached_device *priv = DARWIN_CACHED_DEVICE(dev);
687   int config_index;
688
689   if (0 == priv->active_config)
690     return LIBUSB_ERROR_NOT_FOUND;
691
692   config_index = get_configuration_index (dev, priv->active_config);
693   if (config_index < 0)
694     return config_index;
695
696   assert(config_index >= 0 && config_index <= UINT8_MAX);
697   return darwin_get_config_descriptor (dev, (UInt8)config_index, buffer, len);
698 }
699
700 static int darwin_get_config_descriptor(struct libusb_device *dev, uint8_t config_index, void *buffer, size_t len) {
701   struct darwin_cached_device *priv = DARWIN_CACHED_DEVICE(dev);
702   IOUSBConfigurationDescriptorPtr desc;
703   IOReturn kresult;
704   int ret;
705
706   if (!priv || !priv->device)
707     return LIBUSB_ERROR_OTHER;
708
709   kresult = (*priv->device)->GetConfigurationDescriptorPtr (priv->device, config_index, &desc);
710   if (kresult == kIOReturnSuccess) {
711     /* copy descriptor */
712     if (libusb_le16_to_cpu(desc->wTotalLength) < len)
713       len = libusb_le16_to_cpu(desc->wTotalLength);
714
715     memmove (buffer, desc, len);
716   }
717
718   ret = darwin_to_libusb (kresult);
719   if (ret != LIBUSB_SUCCESS)
720     return ret;
721
722   return (int) len;
723 }
724
725 /* check whether the os has configured the device */
726 static enum libusb_error darwin_check_configuration (struct libusb_context *ctx, struct darwin_cached_device *dev) {
727   usb_device_t **darwin_device = dev->device;
728
729   IOUSBConfigurationDescriptorPtr configDesc;
730   IOUSBFindInterfaceRequest request;
731   IOReturn                  kresult;
732   io_iterator_t             interface_iterator;
733   io_service_t              firstInterface;
734
735   if (dev->dev_descriptor.bNumConfigurations < 1) {
736     usbi_err (ctx, "device has no configurations");
737     return LIBUSB_ERROR_OTHER; /* no configurations at this speed so we can't use it */
738   }
739
740   /* checking the configuration of a root hub simulation takes ~1 s in 10.11. the device is
741      not usable anyway */
742   if (0x05ac == libusb_le16_to_cpu (dev->dev_descriptor.idVendor) &&
743       0x8005 == libusb_le16_to_cpu (dev->dev_descriptor.idProduct)) {
744     usbi_dbg ("ignoring configuration on root hub simulation");
745     dev->active_config = 0;
746     return LIBUSB_SUCCESS;
747   }
748
749   /* find the first configuration */
750   kresult = (*darwin_device)->GetConfigurationDescriptorPtr (darwin_device, 0, &configDesc);
751   dev->first_config = (kIOReturnSuccess == kresult) ? configDesc->bConfigurationValue : 1;
752
753   /* check if the device is already configured. there is probably a better way than iterating over the
754      to accomplish this (the trick is we need to avoid a call to GetConfigurations since buggy devices
755      might lock up on the device request) */
756
757   /* Setup the Interface Request */
758   request.bInterfaceClass    = kIOUSBFindInterfaceDontCare;
759   request.bInterfaceSubClass = kIOUSBFindInterfaceDontCare;
760   request.bInterfaceProtocol = kIOUSBFindInterfaceDontCare;
761   request.bAlternateSetting  = kIOUSBFindInterfaceDontCare;
762
763   kresult = (*(darwin_device))->CreateInterfaceIterator(darwin_device, &request, &interface_iterator);
764   if (kresult != kIOReturnSuccess)
765     return darwin_to_libusb (kresult);
766
767   /* iterate once */
768   firstInterface = IOIteratorNext(interface_iterator);
769
770   /* done with the interface iterator */
771   IOObjectRelease(interface_iterator);
772
773   if (firstInterface) {
774     IOObjectRelease (firstInterface);
775
776     /* device is configured */
777     if (dev->dev_descriptor.bNumConfigurations == 1)
778       /* to avoid problems with some devices get the configurations value from the configuration descriptor */
779       dev->active_config = dev->first_config;
780     else
781       /* devices with more than one configuration should work with GetConfiguration */
782       (*darwin_device)->GetConfiguration (darwin_device, &dev->active_config);
783   } else
784     /* not configured */
785     dev->active_config = 0;
786
787   usbi_dbg ("active config: %u, first config: %u", dev->active_config, dev->first_config);
788
789   return LIBUSB_SUCCESS;
790 }
791
792 static IOReturn darwin_request_descriptor (usb_device_t **device, UInt8 desc, UInt8 desc_index, void *buffer, size_t buffer_size) {
793   IOUSBDevRequestTO req;
794
795   assert(buffer_size <= UINT16_MAX);
796
797   memset (buffer, 0, buffer_size);
798
799   /* Set up request for descriptor/ */
800   req.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
801   req.bRequest      = kUSBRqGetDescriptor;
802   req.wValue        = (UInt16)(desc << 8);
803   req.wIndex        = desc_index;
804   req.wLength       = (UInt16)buffer_size;
805   req.pData         = buffer;
806   req.noDataTimeout = 20;
807   req.completionTimeout = 100;
808
809   return (*device)->DeviceRequestTO (device, &req);
810 }
811
812 static enum libusb_error darwin_cache_device_descriptor (struct darwin_cached_device *dev) {
813   usb_device_t **device = dev->device;
814   int retries = 1;
815   long delay = 30000; // microseconds
816   int unsuspended = 0, try_unsuspend = 1, try_reconfigure = 1;
817   int is_open = 0;
818   IOReturn ret = 0, ret2;
819   UInt8 bDeviceClass;
820   UInt16 idProduct, idVendor;
821
822   dev->can_enumerate = 0;
823
824   (*device)->GetDeviceClass (device, &bDeviceClass);
825   (*device)->GetDeviceProduct (device, &idProduct);
826   (*device)->GetDeviceVendor (device, &idVendor);
827
828   /* According to Apple's documentation the device must be open for DeviceRequest but we may not be able to open some
829    * devices and Apple's USB Prober doesn't bother to open the device before issuing a descriptor request.  Still,
830    * to follow the spec as closely as possible, try opening the device */
831   is_open = ((*device)->USBDeviceOpenSeize(device) == kIOReturnSuccess);
832
833   do {
834     /**** retrieve device descriptor ****/
835     ret = darwin_request_descriptor (device, kUSBDeviceDesc, 0, &dev->dev_descriptor, sizeof(dev->dev_descriptor));
836
837     if (kIOReturnOverrun == ret && kUSBDeviceDesc == dev->dev_descriptor.bDescriptorType)
838       /* received an overrun error but we still received a device descriptor */
839       ret = kIOReturnSuccess;
840
841     if (kIOUSBVendorIDAppleComputer == idVendor) {
842       /* NTH: don't bother retrying or unsuspending Apple devices */
843       break;
844     }
845
846     if (kIOReturnSuccess == ret && (0 == dev->dev_descriptor.bNumConfigurations ||
847                                     0 == dev->dev_descriptor.bcdUSB)) {
848       /* work around for incorrectly configured devices */
849       if (try_reconfigure && is_open) {
850         usbi_dbg("descriptor appears to be invalid. resetting configuration before trying again...");
851
852         /* set the first configuration */
853         (*device)->SetConfiguration(device, 1);
854
855         /* don't try to reconfigure again */
856         try_reconfigure = 0;
857       }
858
859       ret = kIOUSBPipeStalled;
860     }
861
862     if (kIOReturnSuccess != ret && is_open && try_unsuspend) {
863       /* device may be suspended. unsuspend it and try again */
864 #if DeviceVersion >= 320
865       UInt32 info = 0;
866
867       /* IOUSBFamily 320+ provides a way to detect device suspension but earlier versions do not */
868       (void)(*device)->GetUSBDeviceInformation (device, &info);
869
870       /* note that the device was suspended */
871       if (info & (1U << kUSBInformationDeviceIsSuspendedBit) || 0 == info)
872         try_unsuspend = 1;
873 #endif
874
875       if (try_unsuspend) {
876         /* try to unsuspend the device */
877         ret2 = (*device)->USBDeviceSuspend (device, 0);
878         if (kIOReturnSuccess != ret2) {
879           /* prevent log spew from poorly behaving devices.  this indicates the
880              os actually had trouble communicating with the device */
881           usbi_dbg("could not retrieve device descriptor. failed to unsuspend: %s",darwin_error_str(ret2));
882         } else
883           unsuspended = 1;
884
885         try_unsuspend = 0;
886       }
887     }
888
889     if (kIOReturnSuccess != ret) {
890       usbi_dbg("kernel responded with code: 0x%08x. sleeping for %ld ms before trying again", ret, delay/1000);
891       /* sleep for a little while before trying again */
892       nanosleep(&(struct timespec){delay / 1000000, (delay * 1000) % 1000000000}, NULL);
893     }
894   } while (kIOReturnSuccess != ret && retries--);
895
896   if (unsuspended)
897     /* resuspend the device */
898     (void)(*device)->USBDeviceSuspend (device, 1);
899
900   if (is_open)
901     (void) (*device)->USBDeviceClose (device);
902
903   if (ret != kIOReturnSuccess) {
904     /* a debug message was already printed out for this error */
905     if (LIBUSB_CLASS_HUB == bDeviceClass)
906       usbi_dbg ("could not retrieve device descriptor %.4x:%.4x: %s (%x). skipping device",
907                 idVendor, idProduct, darwin_error_str (ret), ret);
908     else
909       usbi_warn (NULL, "could not retrieve device descriptor %.4x:%.4x: %s (%x). skipping device",
910                  idVendor, idProduct, darwin_error_str (ret), ret);
911     return darwin_to_libusb (ret);
912   }
913
914   /* catch buggy hubs (which appear to be virtual). Apple's own USB prober has problems with these devices. */
915   if (libusb_le16_to_cpu (dev->dev_descriptor.idProduct) != idProduct) {
916     /* not a valid device */
917     usbi_warn (NULL, "idProduct from iokit (%04x) does not match idProduct in descriptor (%04x). skipping device",
918                idProduct, libusb_le16_to_cpu (dev->dev_descriptor.idProduct));
919     return LIBUSB_ERROR_NO_DEVICE;
920   }
921
922   usbi_dbg ("cached device descriptor:");
923   usbi_dbg ("  bDescriptorType:    0x%02x", dev->dev_descriptor.bDescriptorType);
924   usbi_dbg ("  bcdUSB:             0x%04x", libusb_le16_to_cpu (dev->dev_descriptor.bcdUSB));
925   usbi_dbg ("  bDeviceClass:       0x%02x", dev->dev_descriptor.bDeviceClass);
926   usbi_dbg ("  bDeviceSubClass:    0x%02x", dev->dev_descriptor.bDeviceSubClass);
927   usbi_dbg ("  bDeviceProtocol:    0x%02x", dev->dev_descriptor.bDeviceProtocol);
928   usbi_dbg ("  bMaxPacketSize0:    0x%02x", dev->dev_descriptor.bMaxPacketSize0);
929   usbi_dbg ("  idVendor:           0x%04x", libusb_le16_to_cpu (dev->dev_descriptor.idVendor));
930   usbi_dbg ("  idProduct:          0x%04x", libusb_le16_to_cpu (dev->dev_descriptor.idProduct));
931   usbi_dbg ("  bcdDevice:          0x%04x", libusb_le16_to_cpu (dev->dev_descriptor.bcdDevice));
932   usbi_dbg ("  iManufacturer:      0x%02x", dev->dev_descriptor.iManufacturer);
933   usbi_dbg ("  iProduct:           0x%02x", dev->dev_descriptor.iProduct);
934   usbi_dbg ("  iSerialNumber:      0x%02x", dev->dev_descriptor.iSerialNumber);
935   usbi_dbg ("  bNumConfigurations: 0x%02x", dev->dev_descriptor.bNumConfigurations);
936
937   dev->can_enumerate = 1;
938
939   return LIBUSB_SUCCESS;
940 }
941
942 /* Returns 1 on success, 0 on failure. */
943 static bool get_device_port (io_service_t service, UInt8 *port) {
944   IOReturn kresult;
945   io_service_t parent;
946   bool ret = false;
947
948   if (get_ioregistry_value_number (service, CFSTR("PortNum"), kCFNumberSInt8Type, port)) {
949     return true;
950   }
951
952   kresult = IORegistryEntryGetParentEntry (service, kIOServicePlane, &parent);
953   if (kIOReturnSuccess == kresult) {
954     ret = get_ioregistry_value_data (parent, CFSTR("port"), 1, port);
955     IOObjectRelease (parent);
956   }
957
958   return ret;
959 }
960
961 /* Returns 1 on success, 0 on failure. */
962 static bool get_device_parent_sessionID(io_service_t service, UInt64 *parent_sessionID) {
963   IOReturn kresult;
964   io_service_t parent;
965
966   /* Walk up the tree in the IOService plane until we find a parent that has a sessionID */
967   parent = service;
968   while((kresult = IORegistryEntryGetParentEntry (parent, kIOUSBPlane, &parent)) == kIOReturnSuccess) {
969     if (get_ioregistry_value_number (parent, CFSTR("sessionID"), kCFNumberSInt64Type, parent_sessionID)) {
970         /* Success */
971         return true;
972     }
973   }
974
975   /* We ran out of parents */
976   return false;
977 }
978
979 static enum libusb_error darwin_get_cached_device(io_service_t service, struct darwin_cached_device **cached_out,
980                                                   UInt64 *old_session_id) {
981   struct darwin_cached_device *new_device;
982   UInt64 sessionID = 0, parent_sessionID = 0;
983   UInt32 locationID = 0;
984   enum libusb_error ret = LIBUSB_SUCCESS;
985   usb_device_t **device;
986   UInt8 port = 0;
987
988   /* assuming sessionID != 0 normally (never seen it be 0) */
989   *old_session_id = 0;
990   *cached_out = NULL;
991
992   /* get some info from the io registry */
993   (void) get_ioregistry_value_number (service, CFSTR("sessionID"), kCFNumberSInt64Type, &sessionID);
994   (void) get_ioregistry_value_number (service, CFSTR("locationID"), kCFNumberSInt32Type, &locationID);
995   if (!get_device_port (service, &port)) {
996     usbi_dbg("could not get connected port number");
997   }
998
999   usbi_dbg("finding cached device for sessionID 0x%" PRIx64, sessionID);
1000
1001   if (get_device_parent_sessionID(service, &parent_sessionID)) {
1002     usbi_dbg("parent sessionID: 0x%" PRIx64, parent_sessionID);
1003   }
1004
1005   usbi_mutex_lock(&darwin_cached_devices_lock);
1006   do {
1007     list_for_each_entry(new_device, &darwin_cached_devices, list, struct darwin_cached_device) {
1008       usbi_dbg("matching sessionID/locationID 0x%" PRIx64 "/0x%x against cached device with sessionID/locationID 0x%" PRIx64 "/0x%x",
1009                sessionID, locationID, new_device->session, new_device->location);
1010       if (new_device->location == locationID && new_device->in_reenumerate) {
1011         usbi_dbg ("found cached device with matching location that is being re-enumerated");
1012         *old_session_id = new_device->session;
1013         break;
1014       }
1015
1016       if (new_device->session == sessionID) {
1017         usbi_dbg("using cached device for device");
1018         *cached_out = new_device;
1019         break;
1020       }
1021     }
1022
1023     if (*cached_out)
1024       break;
1025
1026     usbi_dbg("caching new device with sessionID 0x%" PRIx64, sessionID);
1027
1028     device = darwin_device_from_service (service);
1029     if (!device) {
1030       ret = LIBUSB_ERROR_NO_DEVICE;
1031       break;
1032     }
1033
1034     if (!(*old_session_id)) {
1035       new_device = calloc (1, sizeof (*new_device));
1036       if (!new_device) {
1037         ret = LIBUSB_ERROR_NO_MEM;
1038         break;
1039       }
1040
1041       /* add this device to the cached device list */
1042       list_add(&new_device->list, &darwin_cached_devices);
1043
1044       (*device)->GetDeviceAddress (device, (USBDeviceAddress *)&new_device->address);
1045
1046       /* keep a reference to this device */
1047       darwin_ref_cached_device(new_device);
1048
1049       (*device)->GetLocationID (device, &new_device->location);
1050       new_device->port = port;
1051       new_device->parent_session = parent_sessionID;
1052     } else {
1053       /* release the ref to old device's service */
1054       IOObjectRelease (new_device->service);
1055     }
1056
1057     /* keep track of devices regardless of if we successfully enumerate them to
1058        prevent them from being enumerated multiple times */
1059     *cached_out = new_device;
1060
1061     new_device->session = sessionID;
1062     new_device->device = device;
1063     new_device->service = service;
1064
1065     /* retain the service */
1066     IOObjectRetain (service);
1067
1068     /* cache the device descriptor */
1069     ret = darwin_cache_device_descriptor(new_device);
1070     if (ret)
1071       break;
1072
1073     if (new_device->can_enumerate) {
1074       snprintf(new_device->sys_path, 20, "%03i-%04x-%04x-%02x-%02x", new_device->address,
1075                libusb_le16_to_cpu (new_device->dev_descriptor.idVendor),
1076                libusb_le16_to_cpu (new_device->dev_descriptor.idProduct),
1077                new_device->dev_descriptor.bDeviceClass, new_device->dev_descriptor.bDeviceSubClass);
1078     }
1079   } while (0);
1080
1081   usbi_mutex_unlock(&darwin_cached_devices_lock);
1082
1083   return ret;
1084 }
1085
1086 static enum libusb_error process_new_device (struct libusb_context *ctx, struct darwin_cached_device *cached_device,
1087                                              UInt64 old_session_id) {
1088   struct darwin_device_priv *priv;
1089   struct libusb_device *dev = NULL;
1090   UInt8 devSpeed;
1091   enum libusb_error ret = LIBUSB_SUCCESS;
1092
1093   do {
1094     /* check current active configuration (and cache the first configuration value--
1095        which may be used by claim_interface) */
1096     ret = darwin_check_configuration (ctx, cached_device);
1097     if (ret)
1098       break;
1099
1100     if (0 != old_session_id) {
1101       usbi_dbg ("re-using existing device from context %p for with session 0x%" PRIx64 " new session 0x%" PRIx64,
1102                 ctx, old_session_id, cached_device->session);
1103       /* save the libusb device before the session id is updated */
1104       dev = usbi_get_device_by_session_id (ctx, (unsigned long) old_session_id);
1105     }
1106
1107     if (!dev) {
1108       usbi_dbg ("allocating new device in context %p for with session 0x%" PRIx64,
1109                 ctx, cached_device->session);
1110
1111       dev = usbi_alloc_device(ctx, (unsigned long) cached_device->session);
1112       if (!dev) {
1113         return LIBUSB_ERROR_NO_MEM;
1114       }
1115
1116       priv = usbi_get_device_priv(dev);
1117
1118       priv->dev = cached_device;
1119       darwin_ref_cached_device (priv->dev);
1120       dev->port_number    = cached_device->port;
1121       dev->bus_number     = cached_device->location >> 24;
1122       assert(cached_device->address <= UINT8_MAX);
1123       dev->device_address = (uint8_t)cached_device->address;
1124     } else {
1125       priv = usbi_get_device_priv(dev);
1126     }
1127
1128     static_assert(sizeof(dev->device_descriptor) == sizeof(cached_device->dev_descriptor),
1129                   "mismatch between libusb and IOKit device descriptor sizes");
1130     memcpy(&dev->device_descriptor, &cached_device->dev_descriptor, LIBUSB_DT_DEVICE_SIZE);
1131     usbi_localize_device_descriptor(&dev->device_descriptor);
1132     dev->session_data = cached_device->session;
1133
1134     if (NULL != dev->parent_dev) {
1135       libusb_unref_device(dev->parent_dev);
1136       dev->parent_dev = NULL;
1137     }
1138
1139     if (cached_device->parent_session > 0) {
1140       dev->parent_dev = usbi_get_device_by_session_id (ctx, (unsigned long) cached_device->parent_session);
1141     }
1142
1143     (*(priv->dev->device))->GetDeviceSpeed (priv->dev->device, &devSpeed);
1144
1145     switch (devSpeed) {
1146     case kUSBDeviceSpeedLow: dev->speed = LIBUSB_SPEED_LOW; break;
1147     case kUSBDeviceSpeedFull: dev->speed = LIBUSB_SPEED_FULL; break;
1148     case kUSBDeviceSpeedHigh: dev->speed = LIBUSB_SPEED_HIGH; break;
1149 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1080
1150     case kUSBDeviceSpeedSuper: dev->speed = LIBUSB_SPEED_SUPER; break;
1151 #endif
1152 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 101200
1153     case kUSBDeviceSpeedSuperPlus: dev->speed = LIBUSB_SPEED_SUPER_PLUS; break;
1154 #endif
1155     default:
1156       usbi_warn (ctx, "Got unknown device speed %d", devSpeed);
1157     }
1158
1159     ret = usbi_sanitize_device (dev);
1160     if (ret < 0)
1161       break;
1162
1163     usbi_dbg ("found device with address %d port = %d parent = %p at %p", dev->device_address,
1164               dev->port_number, (void *) dev->parent_dev, priv->dev->sys_path);
1165
1166   } while (0);
1167
1168   if (!cached_device->in_reenumerate && 0 == ret) {
1169     usbi_connect_device (dev);
1170   } else {
1171     libusb_unref_device (dev);
1172   }
1173
1174   return ret;
1175 }
1176
1177 static enum libusb_error darwin_scan_devices(struct libusb_context *ctx) {
1178   struct darwin_cached_device *cached_device;
1179   UInt64 old_session_id;
1180   io_iterator_t deviceIterator;
1181   io_service_t service;
1182   IOReturn kresult;
1183   int ret;
1184
1185   kresult = usb_setup_device_iterator (&deviceIterator, 0);
1186   if (kresult != kIOReturnSuccess)
1187     return darwin_to_libusb (kresult);
1188
1189   while ((service = IOIteratorNext (deviceIterator))) {
1190     ret = darwin_get_cached_device (service, &cached_device, &old_session_id);
1191     if (ret < 0 || !cached_device->can_enumerate) {
1192       continue;
1193     }
1194
1195     (void) process_new_device (ctx, cached_device, old_session_id);
1196
1197     IOObjectRelease(service);
1198   }
1199
1200   IOObjectRelease(deviceIterator);
1201
1202   return LIBUSB_SUCCESS;
1203 }
1204
1205 static int darwin_open (struct libusb_device_handle *dev_handle) {
1206   struct darwin_device_handle_priv *priv = usbi_get_device_handle_priv(dev_handle);
1207   struct darwin_cached_device *dpriv = DARWIN_CACHED_DEVICE(dev_handle->dev);
1208   IOReturn kresult;
1209
1210   if (0 == dpriv->open_count) {
1211     /* try to open the device */
1212     kresult = (*(dpriv->device))->USBDeviceOpenSeize (dpriv->device);
1213     if (kresult != kIOReturnSuccess) {
1214       usbi_warn (HANDLE_CTX (dev_handle), "USBDeviceOpen: %s", darwin_error_str(kresult));
1215
1216       if (kIOReturnExclusiveAccess != kresult) {
1217         return darwin_to_libusb (kresult);
1218       }
1219
1220       /* it is possible to perform some actions on a device that is not open so do not return an error */
1221       priv->is_open = false;
1222     } else {
1223       priv->is_open = true;
1224     }
1225
1226     /* create async event source */
1227     kresult = (*(dpriv->device))->CreateDeviceAsyncEventSource (dpriv->device, &priv->cfSource);
1228     if (kresult != kIOReturnSuccess) {
1229       usbi_err (HANDLE_CTX (dev_handle), "CreateDeviceAsyncEventSource: %s", darwin_error_str(kresult));
1230
1231       if (priv->is_open) {
1232         (*(dpriv->device))->USBDeviceClose (dpriv->device);
1233       }
1234
1235       priv->is_open = false;
1236
1237       return darwin_to_libusb (kresult);
1238     }
1239
1240     CFRetain (libusb_darwin_acfl);
1241
1242     /* add the cfSource to the aync run loop */
1243     CFRunLoopAddSource(libusb_darwin_acfl, priv->cfSource, kCFRunLoopCommonModes);
1244   }
1245
1246   /* device opened successfully */
1247   dpriv->open_count++;
1248
1249   usbi_dbg ("device open for access");
1250
1251   return 0;
1252 }
1253
1254 static void darwin_close (struct libusb_device_handle *dev_handle) {
1255   struct darwin_device_handle_priv *priv = usbi_get_device_handle_priv(dev_handle);
1256   struct darwin_cached_device *dpriv = DARWIN_CACHED_DEVICE(dev_handle->dev);
1257   IOReturn kresult;
1258   int i;
1259
1260   if (dpriv->open_count == 0) {
1261     /* something is probably very wrong if this is the case */
1262     usbi_err (HANDLE_CTX (dev_handle), "Close called on a device that was not open!");
1263     return;
1264   }
1265
1266   dpriv->open_count--;
1267
1268   /* make sure all interfaces are released */
1269   for (i = 0 ; i < USB_MAXINTERFACES ; i++)
1270     if (dev_handle->claimed_interfaces & (1U << i))
1271       libusb_release_interface (dev_handle, i);
1272
1273   if (0 == dpriv->open_count) {
1274     /* delete the device's async event source */
1275     if (priv->cfSource) {
1276       CFRunLoopRemoveSource (libusb_darwin_acfl, priv->cfSource, kCFRunLoopDefaultMode);
1277       CFRelease (priv->cfSource);
1278       priv->cfSource = NULL;
1279       CFRelease (libusb_darwin_acfl);
1280     }
1281
1282     if (priv->is_open) {
1283       /* close the device */
1284       kresult = (*(dpriv->device))->USBDeviceClose(dpriv->device);
1285       if (kresult != kIOReturnSuccess) {
1286         /* Log the fact that we had a problem closing the file, however failing a
1287          * close isn't really an error, so return success anyway */
1288         usbi_warn (HANDLE_CTX (dev_handle), "USBDeviceClose: %s", darwin_error_str(kresult));
1289       }
1290     }
1291   }
1292 }
1293
1294 static int darwin_get_configuration(struct libusb_device_handle *dev_handle, uint8_t *config) {
1295   struct darwin_cached_device *dpriv = DARWIN_CACHED_DEVICE(dev_handle->dev);
1296
1297   *config = dpriv->active_config;
1298
1299   return LIBUSB_SUCCESS;
1300 }
1301
1302 static enum libusb_error darwin_set_configuration(struct libusb_device_handle *dev_handle, int config) {
1303   struct darwin_cached_device *dpriv = DARWIN_CACHED_DEVICE(dev_handle->dev);
1304   IOReturn kresult;
1305   uint8_t i;
1306
1307   if (config == -1)
1308     config = 0;
1309
1310   /* Setting configuration will invalidate the interface, so we need
1311      to reclaim it. First, dispose of existing interfaces, if any. */
1312   for (i = 0 ; i < USB_MAXINTERFACES ; i++)
1313     if (dev_handle->claimed_interfaces & (1U << i))
1314       darwin_release_interface (dev_handle, i);
1315
1316   kresult = (*(dpriv->device))->SetConfiguration (dpriv->device, (UInt8)config);
1317   if (kresult != kIOReturnSuccess)
1318     return darwin_to_libusb (kresult);
1319
1320   /* Reclaim any interfaces. */
1321   for (i = 0 ; i < USB_MAXINTERFACES ; i++)
1322     if (dev_handle->claimed_interfaces & (1U << i))
1323       darwin_claim_interface (dev_handle, i);
1324
1325   dpriv->active_config = (UInt8)config;
1326
1327   return LIBUSB_SUCCESS;
1328 }
1329
1330 static IOReturn darwin_get_interface (usb_device_t **darwin_device, uint8_t ifc, io_service_t *usbInterfacep) {
1331   IOUSBFindInterfaceRequest request;
1332   IOReturn                  kresult;
1333   io_iterator_t             interface_iterator;
1334   UInt8                     bInterfaceNumber;
1335   bool                      ret;
1336
1337   *usbInterfacep = IO_OBJECT_NULL;
1338
1339   /* Setup the Interface Request */
1340   request.bInterfaceClass    = kIOUSBFindInterfaceDontCare;
1341   request.bInterfaceSubClass = kIOUSBFindInterfaceDontCare;
1342   request.bInterfaceProtocol = kIOUSBFindInterfaceDontCare;
1343   request.bAlternateSetting  = kIOUSBFindInterfaceDontCare;
1344
1345   kresult = (*(darwin_device))->CreateInterfaceIterator(darwin_device, &request, &interface_iterator);
1346   if (kresult != kIOReturnSuccess)
1347     return kresult;
1348
1349   while ((*usbInterfacep = IOIteratorNext(interface_iterator))) {
1350     /* find the interface number */
1351     ret = get_ioregistry_value_number (*usbInterfacep, CFSTR("bInterfaceNumber"), kCFNumberSInt8Type,
1352                                        &bInterfaceNumber);
1353
1354     if (ret && bInterfaceNumber == ifc) {
1355       break;
1356     }
1357
1358     (void) IOObjectRelease (*usbInterfacep);
1359   }
1360
1361   /* done with the interface iterator */
1362   IOObjectRelease(interface_iterator);
1363
1364   return kIOReturnSuccess;
1365 }
1366
1367 static enum libusb_error get_endpoints (struct libusb_device_handle *dev_handle, uint8_t iface) {
1368   struct darwin_device_handle_priv *priv = usbi_get_device_handle_priv(dev_handle);
1369
1370   /* current interface */
1371   struct darwin_interface *cInterface = &priv->interfaces[iface];
1372
1373   IOReturn kresult;
1374
1375   UInt8 numep, direction, number;
1376   UInt8 dont_care1, dont_care3;
1377   UInt16 dont_care2;
1378   int rc;
1379
1380   usbi_dbg ("building table of endpoints.");
1381
1382   /* retrieve the total number of endpoints on this interface */
1383   kresult = (*(cInterface->interface))->GetNumEndpoints(cInterface->interface, &numep);
1384   if (kresult != kIOReturnSuccess) {
1385     usbi_err (HANDLE_CTX (dev_handle), "can't get number of endpoints for interface: %s", darwin_error_str(kresult));
1386     return darwin_to_libusb (kresult);
1387   }
1388
1389   /* iterate through pipe references */
1390   for (UInt8 i = 1 ; i <= numep ; i++) {
1391     kresult = (*(cInterface->interface))->GetPipeProperties(cInterface->interface, i, &direction, &number, &dont_care1,
1392                                                             &dont_care2, &dont_care3);
1393
1394     if (kresult != kIOReturnSuccess) {
1395       /* probably a buggy device. try to get the endpoint address from the descriptors */
1396       struct libusb_config_descriptor *config;
1397       const struct libusb_endpoint_descriptor *endpoint_desc;
1398       UInt8 alt_setting;
1399
1400       kresult = (*(cInterface->interface))->GetAlternateSetting (cInterface->interface, &alt_setting);
1401       if (kresult != kIOReturnSuccess) {
1402         usbi_err (HANDLE_CTX (dev_handle), "can't get alternate setting for interface");
1403         return darwin_to_libusb (kresult);
1404       }
1405
1406       rc = libusb_get_active_config_descriptor (dev_handle->dev, &config);
1407       if (LIBUSB_SUCCESS != rc) {
1408         return rc;
1409       }
1410
1411       endpoint_desc = config->interface[iface].altsetting[alt_setting].endpoint + i - 1;
1412
1413       cInterface->endpoint_addrs[i - 1] = endpoint_desc->bEndpointAddress;
1414     } else {
1415       cInterface->endpoint_addrs[i - 1] = (UInt8)(((kUSBIn == direction) << kUSBRqDirnShift) | (number & LIBUSB_ENDPOINT_ADDRESS_MASK));
1416     }
1417
1418     usbi_dbg ("interface: %i pipe %i: dir: %i number: %i", iface, i, cInterface->endpoint_addrs[i - 1] >> kUSBRqDirnShift,
1419               cInterface->endpoint_addrs[i - 1] & LIBUSB_ENDPOINT_ADDRESS_MASK);
1420   }
1421
1422   cInterface->num_endpoints = numep;
1423
1424   return LIBUSB_SUCCESS;
1425 }
1426
1427 static int darwin_claim_interface(struct libusb_device_handle *dev_handle, uint8_t iface) {
1428   struct darwin_cached_device *dpriv = DARWIN_CACHED_DEVICE(dev_handle->dev);
1429   struct darwin_device_handle_priv *priv = usbi_get_device_handle_priv(dev_handle);
1430   io_service_t          usbInterface = IO_OBJECT_NULL;
1431   IOReturn              kresult;
1432   enum libusb_error     ret;
1433   IOCFPlugInInterface **plugInInterface = NULL;
1434   SInt32                score;
1435
1436   /* current interface */
1437   struct darwin_interface *cInterface = &priv->interfaces[iface];
1438
1439   kresult = darwin_get_interface (dpriv->device, iface, &usbInterface);
1440   if (kresult != kIOReturnSuccess)
1441     return darwin_to_libusb (kresult);
1442
1443   /* make sure we have an interface */
1444   if (!usbInterface && dpriv->first_config != 0) {
1445     usbi_info (HANDLE_CTX (dev_handle), "no interface found; setting configuration: %d", dpriv->first_config);
1446
1447     /* set the configuration */
1448     ret = darwin_set_configuration (dev_handle, (int) dpriv->first_config);
1449     if (ret != LIBUSB_SUCCESS) {
1450       usbi_err (HANDLE_CTX (dev_handle), "could not set configuration");
1451       return ret;
1452     }
1453
1454     kresult = darwin_get_interface (dpriv->device, iface, &usbInterface);
1455     if (kresult != kIOReturnSuccess) {
1456       usbi_err (HANDLE_CTX (dev_handle), "darwin_get_interface: %s", darwin_error_str(kresult));
1457       return darwin_to_libusb (kresult);
1458     }
1459   }
1460
1461   if (!usbInterface) {
1462     usbi_err (HANDLE_CTX (dev_handle), "interface not found");
1463     return LIBUSB_ERROR_NOT_FOUND;
1464   }
1465
1466   /* get an interface to the device's interface */
1467   kresult = IOCreatePlugInInterfaceForService (usbInterface, kIOUSBInterfaceUserClientTypeID,
1468                                                kIOCFPlugInInterfaceID, &plugInInterface, &score);
1469
1470   /* ignore release error */
1471   (void)IOObjectRelease (usbInterface);
1472
1473   if (kresult != kIOReturnSuccess) {
1474     usbi_err (HANDLE_CTX (dev_handle), "IOCreatePlugInInterfaceForService: %s", darwin_error_str(kresult));
1475     return darwin_to_libusb (kresult);
1476   }
1477
1478   if (!plugInInterface) {
1479     usbi_err (HANDLE_CTX (dev_handle), "plugin interface not found");
1480     return LIBUSB_ERROR_NOT_FOUND;
1481   }
1482
1483   /* Do the actual claim */
1484   kresult = (*plugInInterface)->QueryInterface(plugInInterface,
1485                                                CFUUIDGetUUIDBytes(InterfaceInterfaceID),
1486                                                (LPVOID)&cInterface->interface);
1487   /* We no longer need the intermediate plug-in */
1488   /* Use release instead of IODestroyPlugInInterface to avoid stopping IOServices associated with this device */
1489   (*plugInInterface)->Release (plugInInterface);
1490   if (kresult != kIOReturnSuccess || !cInterface->interface) {
1491     usbi_err (HANDLE_CTX (dev_handle), "QueryInterface: %s", darwin_error_str(kresult));
1492     return darwin_to_libusb (kresult);
1493   }
1494
1495   /* claim the interface */
1496   kresult = (*(cInterface->interface))->USBInterfaceOpen(cInterface->interface);
1497   if (kresult != kIOReturnSuccess) {
1498     usbi_err (HANDLE_CTX (dev_handle), "USBInterfaceOpen: %s", darwin_error_str(kresult));
1499     return darwin_to_libusb (kresult);
1500   }
1501
1502   /* update list of endpoints */
1503   ret = get_endpoints (dev_handle, iface);
1504   if (ret) {
1505     /* this should not happen */
1506     darwin_release_interface (dev_handle, iface);
1507     usbi_err (HANDLE_CTX (dev_handle), "could not build endpoint table");
1508     return ret;
1509   }
1510
1511   cInterface->cfSource = NULL;
1512
1513   /* create async event source */
1514   kresult = (*(cInterface->interface))->CreateInterfaceAsyncEventSource (cInterface->interface, &cInterface->cfSource);
1515   if (kresult != kIOReturnSuccess) {
1516     usbi_err (HANDLE_CTX (dev_handle), "could not create async event source");
1517
1518     /* can't continue without an async event source */
1519     (void)darwin_release_interface (dev_handle, iface);
1520
1521     return darwin_to_libusb (kresult);
1522   }
1523
1524   /* add the cfSource to the async thread's run loop */
1525   CFRunLoopAddSource(libusb_darwin_acfl, cInterface->cfSource, kCFRunLoopDefaultMode);
1526
1527   usbi_dbg ("interface opened");
1528
1529   return LIBUSB_SUCCESS;
1530 }
1531
1532 static int darwin_release_interface(struct libusb_device_handle *dev_handle, uint8_t iface) {
1533   struct darwin_device_handle_priv *priv = usbi_get_device_handle_priv(dev_handle);
1534   IOReturn kresult;
1535
1536   /* current interface */
1537   struct darwin_interface *cInterface = &priv->interfaces[iface];
1538
1539   /* Check to see if an interface is open */
1540   if (!cInterface->interface)
1541     return LIBUSB_SUCCESS;
1542
1543   /* clean up endpoint data */
1544   cInterface->num_endpoints = 0;
1545
1546   /* delete the interface's async event source */
1547   if (cInterface->cfSource) {
1548     CFRunLoopRemoveSource (libusb_darwin_acfl, cInterface->cfSource, kCFRunLoopDefaultMode);
1549     CFRelease (cInterface->cfSource);
1550   }
1551
1552   kresult = (*(cInterface->interface))->USBInterfaceClose(cInterface->interface);
1553   if (kresult != kIOReturnSuccess)
1554     usbi_warn (HANDLE_CTX (dev_handle), "USBInterfaceClose: %s", darwin_error_str(kresult));
1555
1556   kresult = (*(cInterface->interface))->Release(cInterface->interface);
1557   if (kresult != kIOReturnSuccess)
1558     usbi_warn (HANDLE_CTX (dev_handle), "Release: %s", darwin_error_str(kresult));
1559
1560   cInterface->interface = (usb_interface_t **) IO_OBJECT_NULL;
1561
1562   return darwin_to_libusb (kresult);
1563 }
1564
1565 static int darwin_set_interface_altsetting(struct libusb_device_handle *dev_handle, uint8_t iface, uint8_t altsetting) {
1566   struct darwin_device_handle_priv *priv = usbi_get_device_handle_priv(dev_handle);
1567   IOReturn kresult;
1568   enum libusb_error ret;
1569   int i;
1570   uint8_t old_alt_setting;
1571
1572   /* current interface */
1573   struct darwin_interface *cInterface = &priv->interfaces[iface];
1574
1575   if (!cInterface->interface)
1576     return LIBUSB_ERROR_NO_DEVICE;
1577
1578   kresult = (*(cInterface->interface))->SetAlternateInterface (cInterface->interface, altsetting);
1579   if (kresult != kIOReturnSuccess)
1580     usbi_warn (HANDLE_CTX (dev_handle), "SetAlternateInterface: %s", darwin_error_str(kresult));
1581
1582   if (kresult != kIOUSBPipeStalled)
1583     return darwin_to_libusb (kresult);
1584
1585   /* If a device only supports a default setting for the specified interface, then a STALL
1586      (kIOUSBPipeStalled) may be returned. Ref: USB 2.0 specs 9.4.10.
1587      Mimick the behaviour in e.g. the Linux kernel: in such case, reset all endpoints
1588      of the interface (as would have been done per 9.1.1.5) and return success. */
1589
1590   /* For some reason we need to reclaim the interface after the pipe error */
1591   ret = darwin_claim_interface (dev_handle, iface);
1592
1593   if (ret) {
1594     darwin_release_interface (dev_handle, iface);
1595     usbi_err (HANDLE_CTX (dev_handle), "could not reclaim interface");
1596   }
1597
1598   /* Return error if a change to another value was attempted */
1599   kresult = (*(cInterface->interface))->GetAlternateSetting (cInterface->interface, &old_alt_setting);
1600   if (kresult == kIOReturnSuccess && altsetting != old_alt_setting)
1601     return LIBUSB_ERROR_PIPE;
1602
1603   for (i = 0 ; i < cInterface->num_endpoints ; i++)
1604     darwin_clear_halt(dev_handle, cInterface->endpoint_addrs[i]);
1605
1606   return LIBUSB_SUCCESS;
1607 }
1608
1609 static int darwin_clear_halt(struct libusb_device_handle *dev_handle, unsigned char endpoint) {
1610   /* current interface */
1611   struct darwin_interface *cInterface;
1612   IOReturn kresult;
1613   uint8_t pipeRef;
1614
1615   /* determine the interface/endpoint to use */
1616   if (ep_to_pipeRef (dev_handle, endpoint, &pipeRef, NULL, &cInterface) != 0) {
1617     usbi_err (HANDLE_CTX (dev_handle), "endpoint not found on any open interface");
1618
1619     return LIBUSB_ERROR_NOT_FOUND;
1620   }
1621
1622   /* newer versions of darwin support clearing additional bits on the device's endpoint */
1623   kresult = (*(cInterface->interface))->ClearPipeStallBothEnds(cInterface->interface, pipeRef);
1624   if (kresult != kIOReturnSuccess)
1625     usbi_warn (HANDLE_CTX (dev_handle), "ClearPipeStall: %s", darwin_error_str (kresult));
1626
1627   return darwin_to_libusb (kresult);
1628 }
1629
1630 static int darwin_restore_state (struct libusb_device_handle *dev_handle, int8_t active_config,
1631                                  unsigned long claimed_interfaces) {
1632   struct darwin_cached_device *dpriv = DARWIN_CACHED_DEVICE(dev_handle->dev);
1633   struct darwin_device_handle_priv *priv = usbi_get_device_handle_priv(dev_handle);
1634   int open_count = dpriv->open_count;
1635   int ret;
1636
1637   /* clear claimed interfaces temporarily */
1638   dev_handle->claimed_interfaces = 0;
1639
1640   /* close and re-open the device */
1641   priv->is_open = false;
1642   dpriv->open_count = 1;
1643
1644   /* clean up open interfaces */
1645   (void) darwin_close (dev_handle);
1646
1647   /* re-open the device */
1648   ret = darwin_open (dev_handle);
1649   dpriv->open_count = open_count;
1650   if (LIBUSB_SUCCESS != ret) {
1651     /* could not restore configuration */
1652     return LIBUSB_ERROR_NOT_FOUND;
1653   }
1654
1655   if (dpriv->active_config != active_config) {
1656     usbi_dbg ("darwin/restore_state: restoring configuration %d...", active_config);
1657
1658     ret = darwin_set_configuration (dev_handle, active_config);
1659     if (LIBUSB_SUCCESS != ret) {
1660       usbi_dbg ("darwin/restore_state: could not restore configuration");
1661       return LIBUSB_ERROR_NOT_FOUND;
1662     }
1663   }
1664
1665   usbi_dbg ("darwin/restore_state: reclaiming interfaces");
1666
1667   if (claimed_interfaces) {
1668     for (uint8_t iface = 0 ; iface < USB_MAXINTERFACES ; ++iface) {
1669       if (!(claimed_interfaces & (1U << iface))) {
1670         continue;
1671       }
1672
1673       usbi_dbg ("darwin/restore_state: re-claiming interface %u", iface);
1674
1675       ret = darwin_claim_interface (dev_handle, iface);
1676       if (LIBUSB_SUCCESS != ret) {
1677         usbi_dbg ("darwin/restore_state: could not claim interface %u", iface);
1678         return LIBUSB_ERROR_NOT_FOUND;
1679       }
1680
1681       dev_handle->claimed_interfaces |= 1U << iface;
1682     }
1683   }
1684
1685   usbi_dbg ("darwin/restore_state: device state restored");
1686
1687   return LIBUSB_SUCCESS;
1688 }
1689
1690 static int darwin_reenumerate_device (struct libusb_device_handle *dev_handle, bool capture) {
1691   struct darwin_cached_device *dpriv = DARWIN_CACHED_DEVICE(dev_handle->dev);
1692   unsigned long claimed_interfaces = dev_handle->claimed_interfaces;
1693   int8_t active_config = dpriv->active_config;
1694   UInt32 options = 0;
1695   IOUSBDeviceDescriptor descriptor;
1696   IOUSBConfigurationDescriptorPtr cached_configuration;
1697   IOUSBConfigurationDescriptor *cached_configurations;
1698   IOReturn kresult;
1699   UInt8 i;
1700   UInt32 time;
1701
1702   if (dpriv->in_reenumerate) {
1703     /* ack, two (or more) threads are trying to reset the device! abort! */
1704     return LIBUSB_ERROR_NOT_FOUND;
1705   }
1706
1707   dpriv->in_reenumerate = true;
1708
1709   /* store copies of descriptors so they can be compared after the reset */
1710   memcpy (&descriptor, &dpriv->dev_descriptor, sizeof (descriptor));
1711   cached_configurations = alloca (sizeof (*cached_configurations) * descriptor.bNumConfigurations);
1712
1713   for (i = 0 ; i < descriptor.bNumConfigurations ; ++i) {
1714     (*(dpriv->device))->GetConfigurationDescriptorPtr (dpriv->device, i, &cached_configuration);
1715     memcpy (cached_configurations + i, cached_configuration, sizeof (cached_configurations[i]));
1716   }
1717
1718   /* if we need to release capture */
1719   if (HAS_CAPTURE_DEVICE()) {
1720     if (capture) {
1721       options |= kUSBReEnumerateCaptureDeviceMask;
1722     }
1723   } else {
1724     capture = false;
1725   }
1726
1727   /* from macOS 10.11 ResetDevice no longer does anything so just use USBDeviceReEnumerate */
1728   kresult = (*(dpriv->device))->USBDeviceReEnumerate (dpriv->device, options);
1729   if (kresult != kIOReturnSuccess) {
1730     usbi_err (HANDLE_CTX (dev_handle), "USBDeviceReEnumerate: %s", darwin_error_str (kresult));
1731     dpriv->in_reenumerate = false;
1732     return darwin_to_libusb (kresult);
1733   }
1734
1735   /* capture mode does not re-enumerate but it does require re-open */
1736   if (capture) {
1737     usbi_dbg ("darwin/reenumerate_device: restoring state...");
1738     dpriv->in_reenumerate = false;
1739     return darwin_restore_state (dev_handle, active_config, claimed_interfaces);
1740   }
1741
1742   usbi_dbg ("darwin/reenumerate_device: waiting for re-enumeration to complete...");
1743
1744   time = 0;
1745   while (dpriv->in_reenumerate) {
1746     struct timespec delay = {.tv_sec = 0, .tv_nsec = 1000};
1747     nanosleep (&delay, NULL);
1748     if (time++ >= DARWIN_REENUMERATE_TIMEOUT_US) {
1749       usbi_err (HANDLE_CTX (dev_handle), "darwin/reenumerate_device: timeout waiting for reenumerate");
1750       dpriv->in_reenumerate = false;
1751       return LIBUSB_ERROR_TIMEOUT;
1752     }
1753   }
1754
1755   /* compare descriptors */
1756   usbi_dbg ("darwin/reenumerate_device: checking whether descriptors changed");
1757
1758   if (memcmp (&descriptor, &dpriv->dev_descriptor, sizeof (descriptor))) {
1759     /* device descriptor changed. need to return not found. */
1760     usbi_dbg ("darwin/reenumerate_device: device descriptor changed");
1761     return LIBUSB_ERROR_NOT_FOUND;
1762   }
1763
1764   for (i = 0 ; i < descriptor.bNumConfigurations ; ++i) {
1765     (void) (*(dpriv->device))->GetConfigurationDescriptorPtr (dpriv->device, i, &cached_configuration);
1766     if (memcmp (cached_configuration, cached_configurations + i, sizeof (cached_configurations[i]))) {
1767       usbi_dbg ("darwin/reenumerate_device: configuration descriptor %d changed", i);
1768       return LIBUSB_ERROR_NOT_FOUND;
1769     }
1770   }
1771
1772   usbi_dbg ("darwin/reenumerate_device: device reset complete. restoring state...");
1773
1774   return darwin_restore_state (dev_handle, active_config, claimed_interfaces);
1775 }
1776
1777 static int darwin_reset_device (struct libusb_device_handle *dev_handle) {
1778   struct darwin_cached_device *dpriv = DARWIN_CACHED_DEVICE(dev_handle->dev);
1779   IOReturn kresult;
1780
1781   if (dpriv->capture_count > 0) {
1782     /* we have to use ResetDevice as USBDeviceReEnumerate() loses the authorization for capture */
1783     kresult = (*(dpriv->device))->ResetDevice (dpriv->device);
1784     return darwin_to_libusb (kresult);
1785   } else {
1786     return darwin_reenumerate_device (dev_handle, false);
1787   }
1788 }
1789
1790 static int darwin_kernel_driver_active(struct libusb_device_handle *dev_handle, uint8_t interface) {
1791   enum libusb_error ret = darwin_claim_interface (dev_handle, interface);
1792   if (ret == LIBUSB_SUCCESS) {
1793     darwin_release_interface (dev_handle, interface);
1794   }
1795   return (ret == LIBUSB_ERROR_ACCESS);
1796 }
1797
1798 static void darwin_destroy_device(struct libusb_device *dev) {
1799   struct darwin_device_priv *dpriv = usbi_get_device_priv(dev);
1800
1801   if (dpriv->dev) {
1802     /* need to hold the lock in case this is the last reference to the device */
1803     usbi_mutex_lock(&darwin_cached_devices_lock);
1804     darwin_deref_cached_device (dpriv->dev);
1805     dpriv->dev = NULL;
1806     usbi_mutex_unlock(&darwin_cached_devices_lock);
1807   }
1808 }
1809
1810 static int submit_bulk_transfer(struct usbi_transfer *itransfer) {
1811   struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1812
1813   IOReturn               ret;
1814   uint8_t                transferType;
1815   uint8_t                pipeRef;
1816   uint16_t               maxPacketSize;
1817
1818   struct darwin_interface *cInterface;
1819 #if InterfaceVersion >= 550
1820   IOUSBEndpointProperties pipeProperties = {.bVersion = kUSBEndpointPropertiesVersion3};
1821 #else
1822   /* None of the values below are used in libusb for bulk transfers */
1823   uint8_t                 direction, number, interval;
1824 #endif
1825
1826   if (ep_to_pipeRef (transfer->dev_handle, transfer->endpoint, &pipeRef, NULL, &cInterface) != 0) {
1827     usbi_err (TRANSFER_CTX (transfer), "endpoint not found on any open interface");
1828
1829     return LIBUSB_ERROR_NOT_FOUND;
1830   }
1831
1832 #if InterfaceVersion >= 550
1833   ret = (*(cInterface->interface))->GetPipePropertiesV3 (cInterface->interface, pipeRef, &pipeProperties);
1834
1835   transferType = pipeProperties.bTransferType;
1836   maxPacketSize = pipeProperties.wMaxPacketSize;
1837 #else
1838   ret = (*(cInterface->interface))->GetPipeProperties (cInterface->interface, pipeRef, &direction, &number,
1839                                                        &transferType, &maxPacketSize, &interval);
1840 #endif
1841
1842   if (ret) {
1843     usbi_err (TRANSFER_CTX (transfer), "bulk transfer failed (dir = %s): %s (code = 0x%08x)", IS_XFERIN(transfer) ? "In" : "Out",
1844               darwin_error_str(ret), ret);
1845     return darwin_to_libusb (ret);
1846   }
1847
1848   if (0 != (transfer->length % maxPacketSize)) {
1849     /* do not need a zero packet */
1850     transfer->flags &= ~LIBUSB_TRANSFER_ADD_ZERO_PACKET;
1851   }
1852
1853   /* submit the request */
1854   /* timeouts are unavailable on interrupt endpoints */
1855   if (transferType == kUSBInterrupt) {
1856     if (IS_XFERIN(transfer))
1857       ret = (*(cInterface->interface))->ReadPipeAsync(cInterface->interface, pipeRef, transfer->buffer,
1858                                                       (UInt32)transfer->length, darwin_async_io_callback, itransfer);
1859     else
1860       ret = (*(cInterface->interface))->WritePipeAsync(cInterface->interface, pipeRef, transfer->buffer,
1861                                                        (UInt32)transfer->length, darwin_async_io_callback, itransfer);
1862   } else {
1863     itransfer->timeout_flags |= USBI_TRANSFER_OS_HANDLES_TIMEOUT;
1864
1865     if (IS_XFERIN(transfer))
1866       ret = (*(cInterface->interface))->ReadPipeAsyncTO(cInterface->interface, pipeRef, transfer->buffer,
1867                                                         (UInt32)transfer->length, transfer->timeout, transfer->timeout,
1868                                                         darwin_async_io_callback, itransfer);
1869     else
1870       ret = (*(cInterface->interface))->WritePipeAsyncTO(cInterface->interface, pipeRef, transfer->buffer,
1871                                                          (UInt32)transfer->length, transfer->timeout, transfer->timeout,
1872                                                          darwin_async_io_callback, itransfer);
1873   }
1874
1875   if (ret)
1876     usbi_err (TRANSFER_CTX (transfer), "bulk transfer failed (dir = %s): %s (code = 0x%08x)", IS_XFERIN(transfer) ? "In" : "Out",
1877                darwin_error_str(ret), ret);
1878
1879   return darwin_to_libusb (ret);
1880 }
1881
1882 #if InterfaceVersion >= 550
1883 static int submit_stream_transfer(struct usbi_transfer *itransfer) {
1884   struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1885   struct darwin_interface *cInterface;
1886   uint8_t pipeRef;
1887   IOReturn ret;
1888
1889   if (ep_to_pipeRef (transfer->dev_handle, transfer->endpoint, &pipeRef, NULL, &cInterface) != 0) {
1890     usbi_err (TRANSFER_CTX (transfer), "endpoint not found on any open interface");
1891
1892     return LIBUSB_ERROR_NOT_FOUND;
1893   }
1894
1895   itransfer->timeout_flags |= USBI_TRANSFER_OS_HANDLES_TIMEOUT;
1896
1897   if (IS_XFERIN(transfer))
1898     ret = (*(cInterface->interface))->ReadStreamsPipeAsyncTO(cInterface->interface, pipeRef, itransfer->stream_id,
1899                                                              transfer->buffer, (UInt32)transfer->length, transfer->timeout,
1900                                                              transfer->timeout, darwin_async_io_callback, itransfer);
1901   else
1902     ret = (*(cInterface->interface))->WriteStreamsPipeAsyncTO(cInterface->interface, pipeRef, itransfer->stream_id,
1903                                                               transfer->buffer, (UInt32)transfer->length, transfer->timeout,
1904                                                               transfer->timeout, darwin_async_io_callback, itransfer);
1905
1906   if (ret)
1907     usbi_err (TRANSFER_CTX (transfer), "bulk stream transfer failed (dir = %s): %s (code = 0x%08x)", IS_XFERIN(transfer) ? "In" : "Out",
1908                darwin_error_str(ret), ret);
1909
1910   return darwin_to_libusb (ret);
1911 }
1912 #endif
1913
1914 static int submit_iso_transfer(struct usbi_transfer *itransfer) {
1915   struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1916   struct darwin_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
1917
1918   IOReturn kresult;
1919   uint8_t direction, number, interval, pipeRef, transferType;
1920   uint16_t maxPacketSize;
1921   UInt64 frame;
1922   AbsoluteTime atTime;
1923   int i;
1924
1925   struct darwin_interface *cInterface;
1926
1927   /* construct an array of IOUSBIsocFrames, reuse the old one if the sizes are the same */
1928   if (tpriv->num_iso_packets != transfer->num_iso_packets) {
1929     free(tpriv->isoc_framelist);
1930     tpriv->isoc_framelist = NULL;
1931   }
1932
1933   if (!tpriv->isoc_framelist) {
1934     tpriv->num_iso_packets = transfer->num_iso_packets;
1935     tpriv->isoc_framelist = (IOUSBIsocFrame*) calloc ((size_t)transfer->num_iso_packets, sizeof(IOUSBIsocFrame));
1936     if (!tpriv->isoc_framelist)
1937       return LIBUSB_ERROR_NO_MEM;
1938   }
1939
1940   /* copy the frame list from the libusb descriptor (the structures differ only is member order) */
1941   for (i = 0 ; i < transfer->num_iso_packets ; i++) {
1942     unsigned int length = transfer->iso_packet_desc[i].length;
1943     assert(length <= UINT16_MAX);
1944     tpriv->isoc_framelist[i].frReqCount = (UInt16)length;
1945   }
1946
1947   /* determine the interface/endpoint to use */
1948   if (ep_to_pipeRef (transfer->dev_handle, transfer->endpoint, &pipeRef, NULL, &cInterface) != 0) {
1949     usbi_err (TRANSFER_CTX (transfer), "endpoint not found on any open interface");
1950
1951     return LIBUSB_ERROR_NOT_FOUND;
1952   }
1953
1954   /* determine the properties of this endpoint and the speed of the device */
1955   (*(cInterface->interface))->GetPipeProperties (cInterface->interface, pipeRef, &direction, &number,
1956                                                  &transferType, &maxPacketSize, &interval);
1957
1958   /* Last but not least we need the bus frame number */
1959   kresult = (*(cInterface->interface))->GetBusFrameNumber(cInterface->interface, &frame, &atTime);
1960   if (kresult != kIOReturnSuccess) {
1961     usbi_err (TRANSFER_CTX (transfer), "failed to get bus frame number: %d", kresult);
1962     free(tpriv->isoc_framelist);
1963     tpriv->isoc_framelist = NULL;
1964
1965     return darwin_to_libusb (kresult);
1966   }
1967
1968   (*(cInterface->interface))->GetPipeProperties (cInterface->interface, pipeRef, &direction, &number,
1969                                                  &transferType, &maxPacketSize, &interval);
1970
1971   /* schedule for a frame a little in the future */
1972   frame += 4;
1973
1974   if (cInterface->frames[transfer->endpoint] && frame < cInterface->frames[transfer->endpoint])
1975     frame = cInterface->frames[transfer->endpoint];
1976
1977   /* submit the request */
1978   if (IS_XFERIN(transfer))
1979     kresult = (*(cInterface->interface))->ReadIsochPipeAsync(cInterface->interface, pipeRef, transfer->buffer, frame,
1980                                                              (UInt32)transfer->num_iso_packets, tpriv->isoc_framelist, darwin_async_io_callback,
1981                                                              itransfer);
1982   else
1983     kresult = (*(cInterface->interface))->WriteIsochPipeAsync(cInterface->interface, pipeRef, transfer->buffer, frame,
1984                                                               (UInt32)transfer->num_iso_packets, tpriv->isoc_framelist, darwin_async_io_callback,
1985                                                               itransfer);
1986
1987   if (LIBUSB_SPEED_FULL == transfer->dev_handle->dev->speed)
1988     /* Full speed */
1989     cInterface->frames[transfer->endpoint] = frame + (UInt32)transfer->num_iso_packets * (1U << (interval - 1));
1990   else
1991     /* High/super speed */
1992     cInterface->frames[transfer->endpoint] = frame + (UInt32)transfer->num_iso_packets * (1U << (interval - 1)) / 8;
1993
1994   if (kresult != kIOReturnSuccess) {
1995     usbi_err (TRANSFER_CTX (transfer), "isochronous transfer failed (dir: %s): %s", IS_XFERIN(transfer) ? "In" : "Out",
1996                darwin_error_str(kresult));
1997     free (tpriv->isoc_framelist);
1998     tpriv->isoc_framelist = NULL;
1999   }
2000
2001   return darwin_to_libusb (kresult);
2002 }
2003
2004 static int submit_control_transfer(struct usbi_transfer *itransfer) {
2005   struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2006   struct libusb_control_setup *setup = (struct libusb_control_setup *) transfer->buffer;
2007   struct darwin_cached_device *dpriv = DARWIN_CACHED_DEVICE(transfer->dev_handle->dev);
2008   struct darwin_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2009
2010   IOReturn               kresult;
2011
2012   memset(&tpriv->req, 0, sizeof(tpriv->req));
2013
2014   /* IOUSBDeviceInterface expects the request in cpu endianness */
2015   tpriv->req.bmRequestType     = setup->bmRequestType;
2016   tpriv->req.bRequest          = setup->bRequest;
2017   /* these values should be in bus order from libusb_fill_control_setup */
2018   tpriv->req.wValue            = OSSwapLittleToHostInt16 (setup->wValue);
2019   tpriv->req.wIndex            = OSSwapLittleToHostInt16 (setup->wIndex);
2020   tpriv->req.wLength           = OSSwapLittleToHostInt16 (setup->wLength);
2021   /* data is stored after the libusb control block */
2022   tpriv->req.pData             = transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
2023   tpriv->req.completionTimeout = transfer->timeout;
2024   tpriv->req.noDataTimeout     = transfer->timeout;
2025
2026   itransfer->timeout_flags |= USBI_TRANSFER_OS_HANDLES_TIMEOUT;
2027
2028   /* all transfers in libusb-1.0 are async */
2029
2030   if (transfer->endpoint) {
2031     struct darwin_interface *cInterface;
2032     uint8_t                 pipeRef;
2033
2034     if (ep_to_pipeRef (transfer->dev_handle, transfer->endpoint, &pipeRef, NULL, &cInterface) != 0) {
2035       usbi_err (TRANSFER_CTX (transfer), "endpoint not found on any open interface");
2036
2037       return LIBUSB_ERROR_NOT_FOUND;
2038     }
2039
2040     kresult = (*(cInterface->interface))->ControlRequestAsyncTO (cInterface->interface, pipeRef, &(tpriv->req), darwin_async_io_callback, itransfer);
2041   } else
2042     /* control request on endpoint 0 */
2043     kresult = (*(dpriv->device))->DeviceRequestAsyncTO(dpriv->device, &(tpriv->req), darwin_async_io_callback, itransfer);
2044
2045   if (kresult != kIOReturnSuccess)
2046     usbi_err (TRANSFER_CTX (transfer), "control request failed: %s", darwin_error_str(kresult));
2047
2048   return darwin_to_libusb (kresult);
2049 }
2050
2051 static int darwin_submit_transfer(struct usbi_transfer *itransfer) {
2052   struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2053
2054   switch (transfer->type) {
2055   case LIBUSB_TRANSFER_TYPE_CONTROL:
2056     return submit_control_transfer(itransfer);
2057   case LIBUSB_TRANSFER_TYPE_BULK:
2058   case LIBUSB_TRANSFER_TYPE_INTERRUPT:
2059     return submit_bulk_transfer(itransfer);
2060   case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
2061     return submit_iso_transfer(itransfer);
2062   case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
2063 #if InterfaceVersion >= 550
2064     return submit_stream_transfer(itransfer);
2065 #else
2066     usbi_err (TRANSFER_CTX(transfer), "IOUSBFamily version does not support bulk stream transfers");
2067     return LIBUSB_ERROR_NOT_SUPPORTED;
2068 #endif
2069   default:
2070     usbi_err (TRANSFER_CTX(transfer), "unknown endpoint type %d", transfer->type);
2071     return LIBUSB_ERROR_INVALID_PARAM;
2072   }
2073 }
2074
2075 static int cancel_control_transfer(struct usbi_transfer *itransfer) {
2076   struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2077   struct darwin_cached_device *dpriv = DARWIN_CACHED_DEVICE(transfer->dev_handle->dev);
2078   IOReturn kresult;
2079
2080   usbi_warn (ITRANSFER_CTX (itransfer), "aborting all transactions control pipe");
2081
2082   if (!dpriv->device)
2083     return LIBUSB_ERROR_NO_DEVICE;
2084
2085   kresult = (*(dpriv->device))->USBDeviceAbortPipeZero (dpriv->device);
2086
2087   return darwin_to_libusb (kresult);
2088 }
2089
2090 static int darwin_abort_transfers (struct usbi_transfer *itransfer) {
2091   struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2092   struct darwin_cached_device *dpriv = DARWIN_CACHED_DEVICE(transfer->dev_handle->dev);
2093   struct darwin_interface *cInterface;
2094   uint8_t pipeRef, iface;
2095   IOReturn kresult;
2096
2097   if (ep_to_pipeRef (transfer->dev_handle, transfer->endpoint, &pipeRef, &iface, &cInterface) != 0) {
2098     usbi_err (TRANSFER_CTX (transfer), "endpoint not found on any open interface");
2099
2100     return LIBUSB_ERROR_NOT_FOUND;
2101   }
2102
2103   if (!dpriv->device)
2104     return LIBUSB_ERROR_NO_DEVICE;
2105
2106   usbi_warn (ITRANSFER_CTX (itransfer), "aborting all transactions on interface %d pipe %d", iface, pipeRef);
2107
2108   /* abort transactions */
2109 #if InterfaceVersion >= 550
2110   if (LIBUSB_TRANSFER_TYPE_BULK_STREAM == transfer->type)
2111     (*(cInterface->interface))->AbortStreamsPipe (cInterface->interface, pipeRef, itransfer->stream_id);
2112   else
2113 #endif
2114     (*(cInterface->interface))->AbortPipe (cInterface->interface, pipeRef);
2115
2116   usbi_dbg ("calling clear pipe stall to clear the data toggle bit");
2117
2118   /* newer versions of darwin support clearing additional bits on the device's endpoint */
2119   kresult = (*(cInterface->interface))->ClearPipeStallBothEnds(cInterface->interface, pipeRef);
2120
2121   return darwin_to_libusb (kresult);
2122 }
2123
2124 static int darwin_cancel_transfer(struct usbi_transfer *itransfer) {
2125   struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2126
2127   switch (transfer->type) {
2128   case LIBUSB_TRANSFER_TYPE_CONTROL:
2129     return cancel_control_transfer(itransfer);
2130   case LIBUSB_TRANSFER_TYPE_BULK:
2131   case LIBUSB_TRANSFER_TYPE_INTERRUPT:
2132   case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
2133     return darwin_abort_transfers (itransfer);
2134   default:
2135     usbi_err (TRANSFER_CTX(transfer), "unknown endpoint type %d", transfer->type);
2136     return LIBUSB_ERROR_INVALID_PARAM;
2137   }
2138 }
2139
2140 static void darwin_async_io_callback (void *refcon, IOReturn result, void *arg0) {
2141   struct usbi_transfer *itransfer = (struct usbi_transfer *)refcon;
2142   struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2143   struct darwin_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2144
2145   usbi_dbg ("an async io operation has completed");
2146
2147   /* if requested write a zero packet */
2148   if (kIOReturnSuccess == result && IS_XFEROUT(transfer) && transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) {
2149     struct darwin_interface *cInterface;
2150     uint8_t pipeRef;
2151
2152     (void) ep_to_pipeRef (transfer->dev_handle, transfer->endpoint, &pipeRef, NULL, &cInterface);
2153
2154     (*(cInterface->interface))->WritePipe (cInterface->interface, pipeRef, transfer->buffer, 0);
2155   }
2156
2157   tpriv->result = result;
2158   tpriv->size = (UInt32) (uintptr_t) arg0;
2159
2160   /* signal the core that this transfer is complete */
2161   usbi_signal_transfer_completion(itransfer);
2162 }
2163
2164 static enum libusb_transfer_status darwin_transfer_status (struct usbi_transfer *itransfer, IOReturn result) {
2165   if (itransfer->timeout_flags & USBI_TRANSFER_TIMED_OUT)
2166     result = kIOUSBTransactionTimeout;
2167
2168   switch (result) {
2169   case kIOReturnUnderrun:
2170   case kIOReturnSuccess:
2171     return LIBUSB_TRANSFER_COMPLETED;
2172   case kIOReturnAborted:
2173     return LIBUSB_TRANSFER_CANCELLED;
2174   case kIOUSBPipeStalled:
2175     usbi_dbg ("transfer error: pipe is stalled");
2176     return LIBUSB_TRANSFER_STALL;
2177   case kIOReturnOverrun:
2178     usbi_warn (ITRANSFER_CTX (itransfer), "transfer error: data overrun");
2179     return LIBUSB_TRANSFER_OVERFLOW;
2180   case kIOUSBTransactionTimeout:
2181     usbi_warn (ITRANSFER_CTX (itransfer), "transfer error: timed out");
2182     itransfer->timeout_flags |= USBI_TRANSFER_TIMED_OUT;
2183     return LIBUSB_TRANSFER_TIMED_OUT;
2184   default:
2185     usbi_warn (ITRANSFER_CTX (itransfer), "transfer error: %s (value = 0x%08x)", darwin_error_str (result), result);
2186     return LIBUSB_TRANSFER_ERROR;
2187   }
2188 }
2189
2190 static int darwin_handle_transfer_completion (struct usbi_transfer *itransfer) {
2191   struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2192   struct darwin_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2193   const unsigned char max_transfer_type = LIBUSB_TRANSFER_TYPE_BULK_STREAM;
2194   const char *transfer_types[] = {"control", "isoc", "bulk", "interrupt", "bulk-stream", NULL};
2195   bool is_isoc = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS == transfer->type;
2196
2197   if (transfer->type > max_transfer_type) {
2198     usbi_err (TRANSFER_CTX(transfer), "unknown endpoint type %d", transfer->type);
2199     return LIBUSB_ERROR_INVALID_PARAM;
2200   }
2201
2202   if (NULL == tpriv) {
2203     usbi_err (TRANSFER_CTX(transfer), "malformed request is missing transfer priv");
2204     return LIBUSB_ERROR_INVALID_PARAM;
2205   }
2206
2207   usbi_dbg ("handling transfer completion type %s with kernel status %d", transfer_types[transfer->type], tpriv->result);
2208
2209   if (kIOReturnSuccess == tpriv->result || kIOReturnUnderrun == tpriv->result || kIOUSBTransactionTimeout == tpriv->result) {
2210     if (is_isoc && tpriv->isoc_framelist) {
2211       /* copy isochronous results back */
2212
2213       for (int i = 0; i < transfer->num_iso_packets ; i++) {
2214         struct libusb_iso_packet_descriptor *lib_desc = &transfer->iso_packet_desc[i];
2215         lib_desc->status = darwin_transfer_status (itransfer, tpriv->isoc_framelist[i].frStatus);
2216         lib_desc->actual_length = tpriv->isoc_framelist[i].frActCount;
2217       }
2218     } else if (!is_isoc) {
2219       itransfer->transferred += tpriv->size;
2220     }
2221   }
2222
2223   /* it is ok to handle cancelled transfers without calling usbi_handle_transfer_cancellation (we catch timeout transfers) */
2224   return usbi_handle_transfer_completion (itransfer, darwin_transfer_status (itransfer, tpriv->result));
2225 }
2226
2227 #if !defined(HAVE_CLOCK_GETTIME)
2228 void usbi_get_monotonic_time(struct timespec *tp) {
2229   mach_timespec_t sys_time;
2230
2231   /* use system boot time as reference for the monotonic clock */
2232   clock_get_time (clock_monotonic, &sys_time);
2233
2234   tp->tv_sec  = sys_time.tv_sec;
2235   tp->tv_nsec = sys_time.tv_nsec;
2236 }
2237
2238 void usbi_get_real_time(struct timespec *tp) {
2239   mach_timespec_t sys_time;
2240
2241   /* CLOCK_REALTIME represents time since the epoch */
2242   clock_get_time (clock_realtime, &sys_time);
2243
2244   tp->tv_sec  = sys_time.tv_sec;
2245   tp->tv_nsec = sys_time.tv_nsec;
2246 }
2247 #endif
2248
2249 #if InterfaceVersion >= 550
2250 static int darwin_alloc_streams (struct libusb_device_handle *dev_handle, uint32_t num_streams, unsigned char *endpoints,
2251                                  int num_endpoints) {
2252   struct darwin_interface *cInterface;
2253   UInt32 supportsStreams;
2254   uint8_t pipeRef;
2255   int rc, i;
2256
2257   /* find the minimum number of supported streams on the endpoint list */
2258   for (i = 0 ; i < num_endpoints ; ++i) {
2259     if (0 != (rc = ep_to_pipeRef (dev_handle, endpoints[i], &pipeRef, NULL, &cInterface))) {
2260       return rc;
2261     }
2262
2263     (*(cInterface->interface))->SupportsStreams (cInterface->interface, pipeRef, &supportsStreams);
2264     if (num_streams > supportsStreams)
2265       num_streams = supportsStreams;
2266   }
2267
2268   /* it is an error if any endpoint in endpoints does not support streams */
2269   if (0 == num_streams)
2270     return LIBUSB_ERROR_INVALID_PARAM;
2271
2272   /* create the streams */
2273   for (i = 0 ; i < num_endpoints ; ++i) {
2274     (void) ep_to_pipeRef (dev_handle, endpoints[i], &pipeRef, NULL, &cInterface);
2275
2276     rc = (*(cInterface->interface))->CreateStreams (cInterface->interface, pipeRef, num_streams);
2277     if (kIOReturnSuccess != rc)
2278       return darwin_to_libusb(rc);
2279   }
2280
2281   assert(num_streams <= INT_MAX);
2282   return (int)num_streams;
2283 }
2284
2285 static int darwin_free_streams (struct libusb_device_handle *dev_handle, unsigned char *endpoints, int num_endpoints) {
2286   struct darwin_interface *cInterface;
2287   UInt32 supportsStreams;
2288   uint8_t pipeRef;
2289   int rc;
2290
2291   for (int i = 0 ; i < num_endpoints ; ++i) {
2292     if (0 != (rc = ep_to_pipeRef (dev_handle, endpoints[i], &pipeRef, NULL, &cInterface)))
2293       return rc;
2294
2295     (*(cInterface->interface))->SupportsStreams (cInterface->interface, pipeRef, &supportsStreams);
2296     if (0 == supportsStreams)
2297       return LIBUSB_ERROR_INVALID_PARAM;
2298
2299     rc = (*(cInterface->interface))->CreateStreams (cInterface->interface, pipeRef, 0);
2300     if (kIOReturnSuccess != rc)
2301       return darwin_to_libusb(rc);
2302   }
2303
2304   return LIBUSB_SUCCESS;
2305 }
2306 #endif
2307
2308 #if InterfaceVersion >= 700
2309
2310 /* macOS APIs for getting entitlement values */
2311
2312 #if TARGET_OS_OSX
2313 #include <Security/Security.h>
2314 #else
2315 typedef struct __SecTask *SecTaskRef;
2316 extern SecTaskRef SecTaskCreateFromSelf(CFAllocatorRef allocator);
2317 extern CFTypeRef SecTaskCopyValueForEntitlement(SecTaskRef task, CFStringRef entitlement, CFErrorRef *error);
2318 #endif
2319
2320 static bool darwin_has_capture_entitlements (void) {
2321   SecTaskRef task;
2322   CFTypeRef value;
2323   bool entitled;
2324
2325   task = SecTaskCreateFromSelf (kCFAllocatorDefault);
2326   if (task == NULL) {
2327     return false;
2328   }
2329   value = SecTaskCopyValueForEntitlement(task, CFSTR("com.apple.vm.device-access"), NULL);
2330   CFRelease (task);
2331   entitled = value && (CFGetTypeID (value) == CFBooleanGetTypeID ()) && CFBooleanGetValue (value);
2332   if (value) {
2333     CFRelease (value);
2334   }
2335   return entitled;
2336 }
2337
2338 static int darwin_reload_device (struct libusb_device_handle *dev_handle) {
2339   struct darwin_cached_device *dpriv = DARWIN_CACHED_DEVICE(dev_handle->dev);
2340   enum libusb_error err;
2341
2342   usbi_mutex_lock(&darwin_cached_devices_lock);
2343   (*(dpriv->device))->Release(dpriv->device);
2344   dpriv->device = darwin_device_from_service (dpriv->service);
2345   if (!dpriv->device) {
2346     err = LIBUSB_ERROR_NO_DEVICE;
2347   } else {
2348     err = LIBUSB_SUCCESS;
2349   }
2350   usbi_mutex_unlock(&darwin_cached_devices_lock);
2351
2352   return err;
2353 }
2354
2355 /* On macOS, we capture an entire device at once, not individual interfaces. */
2356
2357 static int darwin_detach_kernel_driver (struct libusb_device_handle *dev_handle, uint8_t interface) {
2358   UNUSED(interface);
2359   struct darwin_cached_device *dpriv = DARWIN_CACHED_DEVICE(dev_handle->dev);
2360   IOReturn kresult;
2361   enum libusb_error err;
2362
2363   if (HAS_CAPTURE_DEVICE()) {
2364   } else {
2365     return LIBUSB_ERROR_NOT_SUPPORTED;
2366   }
2367
2368   if (dpriv->capture_count == 0) {
2369     /* request authorization */
2370     if (darwin_has_capture_entitlements ()) {
2371       kresult = IOServiceAuthorize (dpriv->service, kIOServiceInteractionAllowed);
2372       if (kresult != kIOReturnSuccess) {
2373         usbi_err (HANDLE_CTX (dev_handle), "IOServiceAuthorize: %s", darwin_error_str(kresult));
2374         return darwin_to_libusb (kresult);
2375       }
2376       /* we need start() to be called again for authorization status to refresh */
2377       err = darwin_reload_device (dev_handle);
2378       if (err != LIBUSB_SUCCESS) {
2379         return err;
2380       }
2381     }
2382     /* reset device to release existing drivers */
2383     err = darwin_reenumerate_device (dev_handle, true);
2384     if (err != LIBUSB_SUCCESS) {
2385       return err;
2386     }
2387   }
2388   dpriv->capture_count++;
2389   return LIBUSB_SUCCESS;
2390 }
2391
2392
2393 static int darwin_attach_kernel_driver (struct libusb_device_handle *dev_handle, uint8_t interface) {
2394   UNUSED(interface);
2395   struct darwin_cached_device *dpriv = DARWIN_CACHED_DEVICE(dev_handle->dev);
2396
2397   if (HAS_CAPTURE_DEVICE()) {
2398   } else {
2399     return LIBUSB_ERROR_NOT_SUPPORTED;
2400   }
2401
2402   dpriv->capture_count--;
2403   if (dpriv->capture_count > 0) {
2404     return LIBUSB_SUCCESS;
2405   } else {
2406     dpriv->capture_count = 0;
2407   }
2408   /* reset device to attach kernel drivers */
2409   return darwin_reenumerate_device (dev_handle, false);
2410 }
2411
2412 static int darwin_capture_claim_interface(struct libusb_device_handle *dev_handle, uint8_t iface) {
2413   enum libusb_error ret;
2414   if (dev_handle->auto_detach_kernel_driver) {
2415     ret = darwin_detach_kernel_driver (dev_handle, iface);
2416     if (ret != LIBUSB_SUCCESS) {
2417       return ret;
2418     }
2419   }
2420   return darwin_claim_interface (dev_handle, iface);
2421 }
2422
2423 static int darwin_capture_release_interface(struct libusb_device_handle *dev_handle, uint8_t iface) {
2424   enum libusb_error ret;
2425
2426   ret = darwin_release_interface (dev_handle, iface);
2427   if (ret != LIBUSB_SUCCESS) {
2428     return ret;
2429   }
2430   if (dev_handle->auto_detach_kernel_driver) {
2431     ret = darwin_attach_kernel_driver (dev_handle, iface);
2432   }
2433   return ret;
2434 }
2435
2436 #endif
2437
2438 const struct usbi_os_backend usbi_backend = {
2439         .name = "Darwin",
2440         .caps = USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER,
2441         .init = darwin_init,
2442         .exit = darwin_exit,
2443         .get_active_config_descriptor = darwin_get_active_config_descriptor,
2444         .get_config_descriptor = darwin_get_config_descriptor,
2445         .hotplug_poll = darwin_hotplug_poll,
2446
2447         .open = darwin_open,
2448         .close = darwin_close,
2449         .get_configuration = darwin_get_configuration,
2450         .set_configuration = darwin_set_configuration,
2451
2452         .set_interface_altsetting = darwin_set_interface_altsetting,
2453         .clear_halt = darwin_clear_halt,
2454         .reset_device = darwin_reset_device,
2455
2456 #if InterfaceVersion >= 550
2457         .alloc_streams = darwin_alloc_streams,
2458         .free_streams = darwin_free_streams,
2459 #endif
2460
2461         .kernel_driver_active = darwin_kernel_driver_active,
2462
2463 #if InterfaceVersion >= 700
2464         .detach_kernel_driver = darwin_detach_kernel_driver,
2465         .attach_kernel_driver = darwin_attach_kernel_driver,
2466         .claim_interface = darwin_capture_claim_interface,
2467         .release_interface = darwin_capture_release_interface,
2468 #else
2469         .claim_interface = darwin_claim_interface,
2470         .release_interface = darwin_release_interface,
2471 #endif
2472
2473         .destroy_device = darwin_destroy_device,
2474
2475         .submit_transfer = darwin_submit_transfer,
2476         .cancel_transfer = darwin_cancel_transfer,
2477
2478         .handle_transfer_completion = darwin_handle_transfer_completion,
2479
2480         .device_priv_size = sizeof(struct darwin_device_priv),
2481         .device_handle_priv_size = sizeof(struct darwin_device_handle_priv),
2482         .transfer_priv_size = sizeof(struct darwin_transfer_priv),
2483 };