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