core: Add debug statements to help trace transfers
[platform/upstream/libusb.git] / libusb / os / darwin_usb.c
index cf7673d..19174b1 100644 (file)
@@ -1,7 +1,7 @@
 /* -*- Mode: C; indent-tabs-mode:nil -*- */
 /*
  * darwin backend for libusb 1.0
- * Copyright © 2008-2013 Nathan Hjelm <hjelmn@users.sourceforge.net>
+ * Copyright © 2008-2014 Nathan Hjelm <hjelmn@users.sourceforge.net>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -175,7 +175,7 @@ static int ep_to_pipeRef(struct libusb_device_handle *dev_handle, uint8_t ep, ui
           if (interface_out)
             *interface_out = cInterface;
 
-          usbi_dbg ("pipe %d on interface %d matches", *pipep, *ifcp);
+          usbi_dbg ("pipe %d on interface %d matches", *pipep, iface);
           return 0;
         }
       }
@@ -284,6 +284,8 @@ static void darwin_devices_detached (void *ptr, io_iterator_t rem_devices) {
   UInt64 session;
   int ret;
 
+  usbi_mutex_lock(&active_contexts_lock);
+
   while ((device = IOIteratorNext (rem_devices)) != 0) {
     /* get the location from the i/o registry */
     ret = get_ioregistry_value_number (device, CFSTR("sessionID"), kCFNumberSInt64Type, &session);
@@ -291,8 +293,6 @@ static void darwin_devices_detached (void *ptr, io_iterator_t rem_devices) {
     if (!ret)
       continue;
 
-    usbi_mutex_lock(&active_contexts_lock);
-
     list_for_each_entry(ctx, &active_contexts_list, list, struct libusb_context) {
       usbi_dbg ("notifying context %p of device disconnect", ctx);
 
@@ -304,9 +304,20 @@ static void darwin_devices_detached (void *ptr, io_iterator_t rem_devices) {
         libusb_unref_device(dev);
       }
     }
-
-    usbi_mutex_unlock(&active_contexts_lock);
   }
+
+  usbi_mutex_unlock(&active_contexts_lock);
+}
+
+static void darwin_hotplug_poll (void)
+{
+  /* not sure if 5 seconds will be too long/short but it should work ok */
+  mach_timespec_t timeout = {.tv_sec = 5, .tv_nsec = 0};
+
+  /* since a kernel thread may nodify the IOInterators used for
+   * hotplug notidication we can't just clear the iterators.
+   * instead just wait until all IOService providers are quiet */
+  (void) IOKitWaitQuiet (kIOMasterPortDefault, &timeout);
 }
 
 static void darwin_clear_iterator (io_iterator_t iter) {
@@ -1127,7 +1138,7 @@ static int get_endpoints (struct libusb_device_handle *dev_handle, int iface) {
 
     usbi_dbg ("interface: %i pipe %i: dir: %i number: %i", iface, i, direction, number);
 
-    cInterface->endpoint_addrs[i - 1] = ((direction << 7 & LIBUSB_ENDPOINT_DIR_MASK) | (number & LIBUSB_ENDPOINT_ADDRESS_MASK));
+    cInterface->endpoint_addrs[i - 1] = (((kUSBIn == direction) << kUSBRqDirnShift) | (number & LIBUSB_ENDPOINT_ADDRESS_MASK));
   }
 
   cInterface->num_endpoints = numep;
@@ -1959,6 +1970,7 @@ const struct usbi_os_backend darwin_backend = {
         .get_device_descriptor = darwin_get_device_descriptor,
         .get_active_config_descriptor = darwin_get_active_config_descriptor,
         .get_config_descriptor = darwin_get_config_descriptor,
+        .hotplug_poll = darwin_hotplug_poll,
 
         .open = darwin_open,
         .close = darwin_close,