2013-09-30 18:36:54 +13:00
|
|
|
/* -*- Mode: C; indent-tabs-mode:nil -*- */
|
|
|
|
/*
|
2014-12-27 15:39:17 +01:00
|
|
|
* darwin backend for libusb 1.0
|
2019-05-01 11:47:48 +02:00
|
|
|
* Copyright © 2008-2019 Nathan Hjelm <hjelmn@users.sourceforge.net>
|
|
|
|
* Copyright © 2019 Google LLC. All rights reserved.
|
2013-09-30 18:36:54 +13:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2019-05-01 11:47:48 +02:00
|
|
|
#include <assert.h>
|
2016-11-20 14:07:07 +01:00
|
|
|
#include <time.h>
|
2013-09-30 18:36:54 +13:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2016-11-20 14:07:07 +01:00
|
|
|
#include <sys/sysctl.h>
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
#include <mach/clock.h>
|
|
|
|
#include <mach/clock_types.h>
|
|
|
|
#include <mach/mach_host.h>
|
|
|
|
#include <mach/mach_port.h>
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
/* Suppress warnings about the use of the deprecated objc_registerThreadWithCollector
|
|
|
|
* function. Its use is also conditionalized to only older deployment targets. */
|
|
|
|
#define OBJC_SILENCE_GC_DEPRECATIONS 1
|
|
|
|
|
2013-09-30 18:36:54 +13:00
|
|
|
#include <AvailabilityMacros.h>
|
2016-11-20 14:07:07 +01:00
|
|
|
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060 && MAC_OS_X_VERSION_MIN_REQUIRED < 101200
|
2013-09-30 18:36:54 +13:00
|
|
|
#include <objc/objc-auto.h>
|
|
|
|
#endif
|
|
|
|
|
2016-11-20 14:07:07 +01:00
|
|
|
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
|
|
|
|
/* Apple deprecated the darwin atomics in 10.12 in favor of C11 atomics */
|
|
|
|
#include <stdatomic.h>
|
|
|
|
#define libusb_darwin_atomic_fetch_add(x, y) atomic_fetch_add(x, y)
|
|
|
|
|
|
|
|
_Atomic int32_t initCount = ATOMIC_VAR_INIT(0);
|
|
|
|
#else
|
|
|
|
/* use darwin atomics if the target is older than 10.12 */
|
|
|
|
#include <libkern/OSAtomic.h>
|
|
|
|
|
|
|
|
/* OSAtomicAdd32Barrier returns the new value */
|
|
|
|
#define libusb_darwin_atomic_fetch_add(x, y) (OSAtomicAdd32Barrier(y, x) - y)
|
|
|
|
|
|
|
|
static volatile int32_t initCount = 0;
|
2019-05-01 11:47:48 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* On 10.12 and later, use newly available clock_*() functions */
|
|
|
|
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
|
|
|
|
#define OSX_USE_CLOCK_GETTIME 1
|
|
|
|
#else
|
|
|
|
#define OSX_USE_CLOCK_GETTIME 0
|
2016-11-20 14:07:07 +01:00
|
|
|
#endif
|
|
|
|
|
2013-09-30 18:36:54 +13:00
|
|
|
#include "darwin_usb.h"
|
|
|
|
|
|
|
|
/* async event thread */
|
|
|
|
static pthread_mutex_t libusb_darwin_at_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
static pthread_cond_t libusb_darwin_at_cond = PTHREAD_COND_INITIALIZER;
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
#if !OSX_USE_CLOCK_GETTIME
|
2013-09-30 18:36:54 +13:00
|
|
|
static clock_serv_t clock_realtime;
|
|
|
|
static clock_serv_t clock_monotonic;
|
2019-05-01 11:47:48 +02:00
|
|
|
#endif
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
static CFRunLoopRef libusb_darwin_acfl = NULL; /* event cf loop */
|
2016-11-20 14:07:07 +01:00
|
|
|
static CFRunLoopSourceRef libusb_darwin_acfls = NULL; /* shutdown signal for event cf loop */
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
static usbi_mutex_t darwin_cached_devices_lock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
static struct list_head darwin_cached_devices = {&darwin_cached_devices, &darwin_cached_devices};
|
2019-05-01 11:47:48 +02:00
|
|
|
static const char *darwin_device_class = kIOUSBDeviceClassName;
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
#define DARWIN_CACHED_DEVICE(a) ((struct darwin_cached_device *) (((struct darwin_device_priv *)((a)->os_priv))->dev))
|
|
|
|
|
|
|
|
/* async event thread */
|
|
|
|
static pthread_t libusb_darwin_at;
|
|
|
|
|
|
|
|
static int darwin_get_config_descriptor(struct libusb_device *dev, uint8_t config_index, unsigned char *buffer, size_t len, int *host_endian);
|
|
|
|
static int darwin_claim_interface(struct libusb_device_handle *dev_handle, int iface);
|
|
|
|
static int darwin_release_interface(struct libusb_device_handle *dev_handle, int iface);
|
|
|
|
static int darwin_reset_device(struct libusb_device_handle *dev_handle);
|
|
|
|
static void darwin_async_io_callback (void *refcon, IOReturn result, void *arg0);
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
static enum libusb_error darwin_scan_devices(struct libusb_context *ctx);
|
|
|
|
static enum libusb_error process_new_device (struct libusb_context *ctx, io_service_t service);
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
#if defined(ENABLE_LOGGING)
|
2019-05-01 11:47:48 +02:00
|
|
|
static const char *darwin_error_str (IOReturn result) {
|
2014-12-27 15:39:17 +01:00
|
|
|
static char string_buffer[50];
|
2013-09-30 18:36:54 +13:00
|
|
|
switch (result) {
|
|
|
|
case kIOReturnSuccess:
|
|
|
|
return "no error";
|
|
|
|
case kIOReturnNotOpen:
|
|
|
|
return "device not opened for exclusive access";
|
|
|
|
case kIOReturnNoDevice:
|
|
|
|
return "no connection to an IOService";
|
|
|
|
case kIOUSBNoAsyncPortErr:
|
|
|
|
return "no async port has been opened for interface";
|
|
|
|
case kIOReturnExclusiveAccess:
|
|
|
|
return "another process has device opened for exclusive access";
|
|
|
|
case kIOUSBPipeStalled:
|
|
|
|
return "pipe is stalled";
|
|
|
|
case kIOReturnError:
|
|
|
|
return "could not establish a connection to the Darwin kernel";
|
|
|
|
case kIOUSBTransactionTimeout:
|
|
|
|
return "transaction timed out";
|
|
|
|
case kIOReturnBadArgument:
|
|
|
|
return "invalid argument";
|
|
|
|
case kIOReturnAborted:
|
|
|
|
return "transaction aborted";
|
|
|
|
case kIOReturnNotResponding:
|
|
|
|
return "device not responding";
|
|
|
|
case kIOReturnOverrun:
|
|
|
|
return "data overrun";
|
|
|
|
case kIOReturnCannotWire:
|
|
|
|
return "physical memory can not be wired down";
|
|
|
|
case kIOReturnNoResources:
|
|
|
|
return "out of resources";
|
|
|
|
case kIOUSBHighSpeedSplitError:
|
|
|
|
return "high speed split error";
|
|
|
|
default:
|
2014-12-27 15:39:17 +01:00
|
|
|
snprintf(string_buffer, sizeof(string_buffer), "unknown error (0x%x)", result);
|
|
|
|
return string_buffer;
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
static enum libusb_error darwin_to_libusb (IOReturn result) {
|
2013-09-30 18:36:54 +13:00
|
|
|
switch (result) {
|
|
|
|
case kIOReturnUnderrun:
|
|
|
|
case kIOReturnSuccess:
|
|
|
|
return LIBUSB_SUCCESS;
|
|
|
|
case kIOReturnNotOpen:
|
|
|
|
case kIOReturnNoDevice:
|
|
|
|
return LIBUSB_ERROR_NO_DEVICE;
|
|
|
|
case kIOReturnExclusiveAccess:
|
|
|
|
return LIBUSB_ERROR_ACCESS;
|
|
|
|
case kIOUSBPipeStalled:
|
|
|
|
return LIBUSB_ERROR_PIPE;
|
|
|
|
case kIOReturnBadArgument:
|
|
|
|
return LIBUSB_ERROR_INVALID_PARAM;
|
|
|
|
case kIOUSBTransactionTimeout:
|
|
|
|
return LIBUSB_ERROR_TIMEOUT;
|
|
|
|
case kIOReturnNotResponding:
|
|
|
|
case kIOReturnAborted:
|
|
|
|
case kIOReturnError:
|
|
|
|
case kIOUSBNoAsyncPortErr:
|
|
|
|
default:
|
|
|
|
return LIBUSB_ERROR_OTHER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this function must be called with the darwin_cached_devices_lock held */
|
|
|
|
static void darwin_deref_cached_device(struct darwin_cached_device *cached_dev) {
|
|
|
|
cached_dev->refcount--;
|
|
|
|
/* free the device and remove it from the cache */
|
|
|
|
if (0 == cached_dev->refcount) {
|
|
|
|
list_del(&cached_dev->list);
|
|
|
|
|
|
|
|
(*(cached_dev->device))->Release(cached_dev->device);
|
|
|
|
free (cached_dev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void darwin_ref_cached_device(struct darwin_cached_device *cached_dev) {
|
|
|
|
cached_dev->refcount++;
|
|
|
|
}
|
|
|
|
|
2014-12-27 15:39:17 +01:00
|
|
|
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) {
|
2013-09-30 18:36:54 +13:00
|
|
|
struct darwin_device_handle_priv *priv = (struct darwin_device_handle_priv *)dev_handle->os_priv;
|
|
|
|
|
|
|
|
/* current interface */
|
|
|
|
struct darwin_interface *cInterface;
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
uint8_t i, iface;
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
usbi_dbg ("converting ep address 0x%02x to pipeRef and interface", ep);
|
|
|
|
|
|
|
|
for (iface = 0 ; iface < USB_MAXINTERFACES ; iface++) {
|
|
|
|
cInterface = &priv->interfaces[iface];
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
if (dev_handle->claimed_interfaces & (1U << iface)) {
|
2013-09-30 18:36:54 +13:00
|
|
|
for (i = 0 ; i < cInterface->num_endpoints ; i++) {
|
|
|
|
if (cInterface->endpoint_addrs[i] == ep) {
|
|
|
|
*pipep = i + 1;
|
2014-12-27 15:39:17 +01:00
|
|
|
|
|
|
|
if (ifcp)
|
|
|
|
*ifcp = iface;
|
|
|
|
|
|
|
|
if (interface_out)
|
|
|
|
*interface_out = cInterface;
|
|
|
|
|
|
|
|
usbi_dbg ("pipe %d on interface %d matches", *pipep, iface);
|
2019-05-01 11:47:48 +02:00
|
|
|
return LIBUSB_SUCCESS;
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No pipe found with the correct endpoint address */
|
|
|
|
usbi_warn (HANDLE_CTX(dev_handle), "no pipeRef found with endpoint address 0x%02x.", ep);
|
|
|
|
|
2014-12-27 15:39:17 +01:00
|
|
|
return LIBUSB_ERROR_NOT_FOUND;
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
static IOReturn usb_setup_device_iterator (io_iterator_t *deviceIterator, UInt32 location) {
|
2016-11-20 14:07:07 +01:00
|
|
|
CFMutableDictionaryRef matchingDict = IOServiceMatching(darwin_device_class);
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
if (!matchingDict)
|
|
|
|
return kIOReturnError;
|
|
|
|
|
|
|
|
if (location) {
|
|
|
|
CFMutableDictionaryRef propertyMatchDict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
|
|
|
|
&kCFTypeDictionaryKeyCallBacks,
|
|
|
|
&kCFTypeDictionaryValueCallBacks);
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
/* there are no unsigned CFNumber types so treat the value as signed. the OS seems to do this
|
|
|
|
internally (CFNumberType of locationID is kCFNumberSInt32Type) */
|
|
|
|
CFTypeRef locationCF = CFNumberCreate (NULL, kCFNumberSInt32Type, &location);
|
2013-09-30 18:36:54 +13:00
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
if (propertyMatchDict && locationCF) {
|
2013-09-30 18:36:54 +13:00
|
|
|
CFDictionarySetValue (propertyMatchDict, CFSTR(kUSBDevicePropertyLocationID), locationCF);
|
|
|
|
CFDictionarySetValue (matchingDict, CFSTR(kIOPropertyMatchKey), propertyMatchDict);
|
|
|
|
}
|
|
|
|
/* else we can still proceed as long as the caller accounts for the possibility of other devices in the iterator */
|
2019-05-01 11:47:48 +02:00
|
|
|
|
|
|
|
/* release our references as per the Create Rule */
|
|
|
|
if (propertyMatchDict)
|
|
|
|
CFRelease (propertyMatchDict);
|
|
|
|
if (locationCF)
|
|
|
|
CFRelease (locationCF);
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
return IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, deviceIterator);
|
|
|
|
}
|
|
|
|
|
2014-12-27 15:39:17 +01:00
|
|
|
/* Returns 1 on success, 0 on failure. */
|
2019-05-01 11:47:48 +02:00
|
|
|
static bool get_ioregistry_value_number (io_service_t service, CFStringRef property, CFNumberType type, void *p) {
|
2013-09-30 18:36:54 +13:00
|
|
|
CFTypeRef cfNumber = IORegistryEntryCreateCFProperty (service, property, kCFAllocatorDefault, 0);
|
2019-05-01 11:47:48 +02:00
|
|
|
Boolean success = 0;
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
if (cfNumber) {
|
|
|
|
if (CFGetTypeID(cfNumber) == CFNumberGetTypeID()) {
|
2019-05-01 11:47:48 +02:00
|
|
|
success = CFNumberGetValue(cfNumber, type, p);
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
CFRelease (cfNumber);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
return (success != 0);
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
/* Returns 1 on success, 0 on failure. */
|
|
|
|
static bool get_ioregistry_value_data (io_service_t service, CFStringRef property, ssize_t size, void *p) {
|
2016-11-20 14:07:07 +01:00
|
|
|
CFTypeRef cfData = IORegistryEntryCreateCFProperty (service, property, kCFAllocatorDefault, 0);
|
2019-05-01 11:47:48 +02:00
|
|
|
bool success = false;
|
2016-11-20 14:07:07 +01:00
|
|
|
|
|
|
|
if (cfData) {
|
|
|
|
if (CFGetTypeID (cfData) == CFDataGetTypeID ()) {
|
|
|
|
CFIndex length = CFDataGetLength (cfData);
|
|
|
|
if (length < size) {
|
|
|
|
size = length;
|
|
|
|
}
|
|
|
|
|
|
|
|
CFDataGetBytes (cfData, CFRangeMake(0, size), p);
|
2019-05-01 11:47:48 +02:00
|
|
|
success = true;
|
2016-11-20 14:07:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CFRelease (cfData);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
return success;
|
2016-11-20 14:07:07 +01:00
|
|
|
}
|
|
|
|
|
2013-09-30 18:36:54 +13:00
|
|
|
static usb_device_t **darwin_device_from_service (io_service_t service)
|
|
|
|
{
|
|
|
|
io_cf_plugin_ref_t *plugInInterface = NULL;
|
|
|
|
usb_device_t **device;
|
2019-05-01 11:47:48 +02:00
|
|
|
IOReturn kresult;
|
2013-09-30 18:36:54 +13:00
|
|
|
SInt32 score;
|
2019-05-01 11:47:48 +02:00
|
|
|
const int max_retries = 5;
|
|
|
|
|
|
|
|
/* The IOCreatePlugInInterfaceForService function might consistently return
|
|
|
|
an "out of resources" error with certain USB devices the first time we run
|
|
|
|
it. The reason is still unclear, but retrying fixes the problem */
|
|
|
|
for (int count = 0; count < max_retries; count++) {
|
|
|
|
kresult = IOCreatePlugInInterfaceForService(service, kIOUSBDeviceUserClientTypeID,
|
|
|
|
kIOCFPlugInInterfaceID, &plugInInterface,
|
|
|
|
&score);
|
|
|
|
if (kIOReturnSuccess == kresult && plugInInterface) {
|
|
|
|
break;
|
|
|
|
}
|
2013-09-30 18:36:54 +13:00
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
usbi_dbg ("set up plugin for service retry: %s", darwin_error_str (kresult));
|
2013-09-30 18:36:54 +13:00
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
/* sleep for a little while before trying again */
|
|
|
|
nanosleep(&(struct timespec){.tv_sec = 0, .tv_nsec = 1000}, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kIOReturnSuccess != kresult || !plugInInterface) {
|
|
|
|
usbi_dbg ("could not set up plugin for service: %s", darwin_error_str (kresult));
|
2013-09-30 18:36:54 +13:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)(*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(DeviceInterfaceID),
|
|
|
|
(LPVOID)&device);
|
|
|
|
/* Use release instead of IODestroyPlugInInterface to avoid stopping IOServices associated with this device */
|
|
|
|
(*plugInInterface)->Release (plugInInterface);
|
|
|
|
|
|
|
|
return device;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void darwin_devices_attached (void *ptr, io_iterator_t add_devices) {
|
2019-05-01 11:47:48 +02:00
|
|
|
UNUSED(ptr);
|
2013-09-30 18:36:54 +13:00
|
|
|
struct libusb_context *ctx;
|
|
|
|
io_service_t service;
|
|
|
|
|
|
|
|
usbi_mutex_lock(&active_contexts_lock);
|
|
|
|
|
|
|
|
while ((service = IOIteratorNext(add_devices))) {
|
|
|
|
/* add this device to each active context's device list */
|
|
|
|
list_for_each_entry(ctx, &active_contexts_list, list, struct libusb_context) {
|
2019-05-01 11:47:48 +02:00
|
|
|
process_new_device (ctx, service);
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
IOObjectRelease(service);
|
|
|
|
}
|
|
|
|
|
|
|
|
usbi_mutex_unlock(&active_contexts_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void darwin_devices_detached (void *ptr, io_iterator_t rem_devices) {
|
2019-05-01 11:47:48 +02:00
|
|
|
UNUSED(ptr);
|
2013-09-30 18:36:54 +13:00
|
|
|
struct libusb_device *dev = NULL;
|
|
|
|
struct libusb_context *ctx;
|
2016-08-25 18:27:40 -07:00
|
|
|
struct darwin_cached_device *old_device;
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
io_service_t device;
|
|
|
|
UInt64 session;
|
|
|
|
int ret;
|
|
|
|
|
2014-12-27 15:39:17 +01:00
|
|
|
usbi_mutex_lock(&active_contexts_lock);
|
|
|
|
|
2013-09-30 18:36:54 +13:00
|
|
|
while ((device = IOIteratorNext (rem_devices)) != 0) {
|
|
|
|
/* get the location from the i/o registry */
|
|
|
|
ret = get_ioregistry_value_number (device, CFSTR("sessionID"), kCFNumberSInt64Type, &session);
|
|
|
|
IOObjectRelease (device);
|
|
|
|
if (!ret)
|
|
|
|
continue;
|
|
|
|
|
2016-08-25 18:27:40 -07:00
|
|
|
/* we need to match darwin_ref_cached_device call made in darwin_get_cached_device function
|
|
|
|
otherwise no cached device will ever get freed */
|
|
|
|
usbi_mutex_lock(&darwin_cached_devices_lock);
|
|
|
|
list_for_each_entry(old_device, &darwin_cached_devices, list, struct darwin_cached_device) {
|
|
|
|
if (old_device->session == session) {
|
2019-05-01 11:47:48 +02:00
|
|
|
if (old_device->in_reenumerate) {
|
|
|
|
/* device is re-enumerating. do not dereference the device at this time. libusb_reset_device()
|
|
|
|
* will deref if needed. */
|
|
|
|
usbi_dbg ("detected device detatched due to re-enumeration");
|
|
|
|
} else {
|
|
|
|
darwin_deref_cached_device (old_device);
|
|
|
|
}
|
2016-08-25 18:27:40 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
usbi_mutex_unlock(&darwin_cached_devices_lock);
|
2019-05-01 11:47:48 +02:00
|
|
|
if (old_device->in_reenumerate) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-08-25 18:27:40 -07:00
|
|
|
|
2013-09-30 18:36:54 +13:00
|
|
|
list_for_each_entry(ctx, &active_contexts_list, list, struct libusb_context) {
|
|
|
|
usbi_dbg ("notifying context %p of device disconnect", ctx);
|
|
|
|
|
2014-12-27 15:39:17 +01:00
|
|
|
dev = usbi_get_device_by_session_id(ctx, (unsigned long) session);
|
2013-09-30 18:36:54 +13:00
|
|
|
if (dev) {
|
|
|
|
/* signal the core that this device has been disconnected. the core will tear down this device
|
|
|
|
when the reference count reaches 0 */
|
|
|
|
usbi_disconnect_device(dev);
|
2014-12-27 15:39:17 +01:00
|
|
|
libusb_unref_device(dev);
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-27 15:39:17 +01:00
|
|
|
|
|
|
|
usbi_mutex_unlock(&active_contexts_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void darwin_hotplug_poll (void)
|
|
|
|
{
|
2019-05-01 11:47:48 +02:00
|
|
|
/* not sure if 1 ms will be too long/short but it should work ok */
|
|
|
|
mach_timespec_t timeout = {.tv_sec = 0, .tv_nsec = 1000000ul};
|
2014-12-27 15:39:17 +01:00
|
|
|
|
|
|
|
/* 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);
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
static void darwin_clear_iterator (io_iterator_t iter) {
|
|
|
|
io_service_t device;
|
|
|
|
|
|
|
|
while ((device = IOIteratorNext (iter)) != 0)
|
|
|
|
IOObjectRelease (device);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *darwin_event_thread_main (void *arg0) {
|
|
|
|
IOReturn kresult;
|
|
|
|
struct libusb_context *ctx = (struct libusb_context *)arg0;
|
|
|
|
CFRunLoopRef runloop;
|
|
|
|
|
2016-11-20 14:07:07 +01:00
|
|
|
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
|
2013-09-30 18:36:54 +13:00
|
|
|
/* Set this thread's name, so it can be seen in the debugger
|
|
|
|
and crash reports. */
|
|
|
|
pthread_setname_np ("org.libusb.device-hotplug");
|
2016-11-20 14:07:07 +01:00
|
|
|
#endif
|
2013-09-30 18:36:54 +13:00
|
|
|
|
2016-11-20 14:07:07 +01:00
|
|
|
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060 && MAC_OS_X_VERSION_MIN_REQUIRED < 101200
|
2013-09-30 18:36:54 +13:00
|
|
|
/* Tell the Objective-C garbage collector about this thread.
|
|
|
|
This is required because, unlike NSThreads, pthreads are
|
|
|
|
not automatically registered. Although we don't use
|
2016-11-20 14:07:07 +01:00
|
|
|
Objective-C, we use CoreFoundation, which does.
|
|
|
|
Garbage collection support was entirely removed in 10.12,
|
|
|
|
so don't bother there. */
|
2013-09-30 18:36:54 +13:00
|
|
|
objc_registerThreadWithCollector();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* hotplug (device arrival/removal) sources */
|
2016-11-20 14:07:07 +01:00
|
|
|
CFRunLoopSourceContext libusb_shutdown_cfsourcectx;
|
2013-09-30 18:36:54 +13:00
|
|
|
CFRunLoopSourceRef libusb_notification_cfsource;
|
|
|
|
io_notification_port_t libusb_notification_port;
|
|
|
|
io_iterator_t libusb_rem_device_iterator;
|
|
|
|
io_iterator_t libusb_add_device_iterator;
|
|
|
|
|
|
|
|
usbi_dbg ("creating hotplug event source");
|
|
|
|
|
|
|
|
runloop = CFRunLoopGetCurrent ();
|
|
|
|
CFRetain (runloop);
|
|
|
|
|
2016-11-20 14:07:07 +01:00
|
|
|
/* add the shutdown cfsource to the run loop */
|
|
|
|
memset(&libusb_shutdown_cfsourcectx, 0, sizeof(libusb_shutdown_cfsourcectx));
|
|
|
|
libusb_shutdown_cfsourcectx.info = runloop;
|
|
|
|
libusb_shutdown_cfsourcectx.perform = (void (*)(void *))CFRunLoopStop;
|
|
|
|
libusb_darwin_acfls = CFRunLoopSourceCreate(NULL, 0, &libusb_shutdown_cfsourcectx);
|
|
|
|
CFRunLoopAddSource(runloop, libusb_darwin_acfls, kCFRunLoopDefaultMode);
|
|
|
|
|
2013-09-30 18:36:54 +13:00
|
|
|
/* add the notification port to the run loop */
|
|
|
|
libusb_notification_port = IONotificationPortCreate (kIOMasterPortDefault);
|
|
|
|
libusb_notification_cfsource = IONotificationPortGetRunLoopSource (libusb_notification_port);
|
|
|
|
CFRunLoopAddSource(runloop, libusb_notification_cfsource, kCFRunLoopDefaultMode);
|
|
|
|
|
|
|
|
/* create notifications for removed devices */
|
|
|
|
kresult = IOServiceAddMatchingNotification (libusb_notification_port, kIOTerminatedNotification,
|
2016-11-20 14:07:07 +01:00
|
|
|
IOServiceMatching(darwin_device_class),
|
2014-12-27 15:39:17 +01:00
|
|
|
darwin_devices_detached,
|
|
|
|
ctx, &libusb_rem_device_iterator);
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
if (kresult != kIOReturnSuccess) {
|
|
|
|
usbi_err (ctx, "could not add hotplug event source: %s", darwin_error_str (kresult));
|
|
|
|
|
|
|
|
pthread_exit (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create notifications for attached devices */
|
|
|
|
kresult = IOServiceAddMatchingNotification(libusb_notification_port, kIOFirstMatchNotification,
|
2016-11-20 14:07:07 +01:00
|
|
|
IOServiceMatching(darwin_device_class),
|
2014-12-27 15:39:17 +01:00
|
|
|
darwin_devices_attached,
|
|
|
|
ctx, &libusb_add_device_iterator);
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
if (kresult != kIOReturnSuccess) {
|
|
|
|
usbi_err (ctx, "could not add hotplug event source: %s", darwin_error_str (kresult));
|
|
|
|
|
|
|
|
pthread_exit (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* arm notifiers */
|
|
|
|
darwin_clear_iterator (libusb_rem_device_iterator);
|
|
|
|
darwin_clear_iterator (libusb_add_device_iterator);
|
|
|
|
|
|
|
|
usbi_dbg ("darwin event thread ready to receive events");
|
|
|
|
|
|
|
|
/* signal the main thread that the hotplug runloop has been created. */
|
|
|
|
pthread_mutex_lock (&libusb_darwin_at_mutex);
|
|
|
|
libusb_darwin_acfl = runloop;
|
|
|
|
pthread_cond_signal (&libusb_darwin_at_cond);
|
|
|
|
pthread_mutex_unlock (&libusb_darwin_at_mutex);
|
|
|
|
|
|
|
|
/* run the runloop */
|
|
|
|
CFRunLoopRun();
|
|
|
|
|
|
|
|
usbi_dbg ("darwin event thread exiting");
|
|
|
|
|
|
|
|
/* remove the notification cfsource */
|
|
|
|
CFRunLoopRemoveSource(runloop, libusb_notification_cfsource, kCFRunLoopDefaultMode);
|
|
|
|
|
2016-11-20 14:07:07 +01:00
|
|
|
/* remove the shutdown cfsource */
|
|
|
|
CFRunLoopRemoveSource(runloop, libusb_darwin_acfls, kCFRunLoopDefaultMode);
|
|
|
|
|
2013-09-30 18:36:54 +13:00
|
|
|
/* delete notification port */
|
|
|
|
IONotificationPortDestroy (libusb_notification_port);
|
|
|
|
|
|
|
|
/* delete iterators */
|
|
|
|
IOObjectRelease (libusb_rem_device_iterator);
|
|
|
|
IOObjectRelease (libusb_add_device_iterator);
|
|
|
|
|
2016-11-20 14:07:07 +01:00
|
|
|
CFRelease (libusb_darwin_acfls);
|
2013-09-30 18:36:54 +13:00
|
|
|
CFRelease (runloop);
|
|
|
|
|
2016-11-20 14:07:07 +01:00
|
|
|
libusb_darwin_acfls = NULL;
|
2013-09-30 18:36:54 +13:00
|
|
|
libusb_darwin_acfl = NULL;
|
|
|
|
|
|
|
|
pthread_exit (NULL);
|
|
|
|
}
|
|
|
|
|
2014-12-27 15:39:17 +01:00
|
|
|
/* cleanup function to destroy cached devices */
|
|
|
|
static void __attribute__((destructor)) _darwin_finalize(void) {
|
2013-09-30 18:36:54 +13:00
|
|
|
struct darwin_cached_device *dev, *next;
|
|
|
|
|
|
|
|
usbi_mutex_lock(&darwin_cached_devices_lock);
|
|
|
|
list_for_each_entry_safe(dev, next, &darwin_cached_devices, list, struct darwin_cached_device) {
|
|
|
|
darwin_deref_cached_device(dev);
|
|
|
|
}
|
|
|
|
usbi_mutex_unlock(&darwin_cached_devices_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int darwin_init(struct libusb_context *ctx) {
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = darwin_scan_devices (ctx);
|
|
|
|
if (LIBUSB_SUCCESS != rc) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2016-11-20 14:07:07 +01:00
|
|
|
if (libusb_darwin_atomic_fetch_add (&initCount, 1) == 0) {
|
2019-05-01 11:47:48 +02:00
|
|
|
#if !OSX_USE_CLOCK_GETTIME
|
|
|
|
/* create the clocks that will be used if clock_gettime() is not available */
|
|
|
|
host_name_port_t host_self;
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
host_self = mach_host_self();
|
|
|
|
host_get_clock_service(host_self, CALENDAR_CLOCK, &clock_realtime);
|
|
|
|
host_get_clock_service(host_self, SYSTEM_CLOCK, &clock_monotonic);
|
|
|
|
mach_port_deallocate(mach_task_self(), host_self);
|
2019-05-01 11:47:48 +02:00
|
|
|
#endif
|
2013-09-30 18:36:54 +13:00
|
|
|
|
2014-12-27 15:39:17 +01:00
|
|
|
pthread_create (&libusb_darwin_at, NULL, darwin_event_thread_main, ctx);
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
pthread_mutex_lock (&libusb_darwin_at_mutex);
|
|
|
|
while (!libusb_darwin_acfl)
|
|
|
|
pthread_cond_wait (&libusb_darwin_at_cond, &libusb_darwin_at_mutex);
|
|
|
|
pthread_mutex_unlock (&libusb_darwin_at_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
static void darwin_exit (struct libusb_context *ctx) {
|
|
|
|
UNUSED(ctx);
|
2016-11-20 14:07:07 +01:00
|
|
|
if (libusb_darwin_atomic_fetch_add (&initCount, -1) == 1) {
|
2019-05-01 11:47:48 +02:00
|
|
|
#if !OSX_USE_CLOCK_GETTIME
|
2013-09-30 18:36:54 +13:00
|
|
|
mach_port_deallocate(mach_task_self(), clock_realtime);
|
|
|
|
mach_port_deallocate(mach_task_self(), clock_monotonic);
|
2019-05-01 11:47:48 +02:00
|
|
|
#endif
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
/* stop the event runloop and wait for the thread to terminate. */
|
2016-11-20 14:07:07 +01:00
|
|
|
CFRunLoopSourceSignal(libusb_darwin_acfls);
|
|
|
|
CFRunLoopWakeUp (libusb_darwin_acfl);
|
2013-09-30 18:36:54 +13:00
|
|
|
pthread_join (libusb_darwin_at, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int darwin_get_device_descriptor(struct libusb_device *dev, unsigned char *buffer, int *host_endian) {
|
|
|
|
struct darwin_cached_device *priv = DARWIN_CACHED_DEVICE(dev);
|
|
|
|
|
|
|
|
/* return cached copy */
|
|
|
|
memmove (buffer, &(priv->dev_descriptor), DEVICE_DESC_LENGTH);
|
|
|
|
|
|
|
|
*host_endian = 0;
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
return LIBUSB_SUCCESS;
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
static int get_configuration_index (struct libusb_device *dev, int config_value) {
|
|
|
|
struct darwin_cached_device *priv = DARWIN_CACHED_DEVICE(dev);
|
|
|
|
UInt8 i, numConfig;
|
|
|
|
IOUSBConfigurationDescriptorPtr desc;
|
|
|
|
IOReturn kresult;
|
|
|
|
|
|
|
|
/* is there a simpler way to determine the index? */
|
|
|
|
kresult = (*(priv->device))->GetNumberOfConfigurations (priv->device, &numConfig);
|
|
|
|
if (kresult != kIOReturnSuccess)
|
|
|
|
return darwin_to_libusb (kresult);
|
|
|
|
|
|
|
|
for (i = 0 ; i < numConfig ; i++) {
|
|
|
|
(*(priv->device))->GetConfigurationDescriptorPtr (priv->device, i, &desc);
|
|
|
|
|
|
|
|
if (desc->bConfigurationValue == config_value)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* configuration not found */
|
|
|
|
return LIBUSB_ERROR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int darwin_get_active_config_descriptor(struct libusb_device *dev, unsigned char *buffer, size_t len, int *host_endian) {
|
|
|
|
struct darwin_cached_device *priv = DARWIN_CACHED_DEVICE(dev);
|
|
|
|
int config_index;
|
|
|
|
|
|
|
|
if (0 == priv->active_config)
|
|
|
|
return LIBUSB_ERROR_NOT_FOUND;
|
|
|
|
|
|
|
|
config_index = get_configuration_index (dev, priv->active_config);
|
|
|
|
if (config_index < 0)
|
|
|
|
return config_index;
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
assert(config_index >= 0 && config_index <= UINT8_MAX);
|
|
|
|
return darwin_get_config_descriptor (dev, (UInt8)config_index, buffer, len, host_endian);
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
static int darwin_get_config_descriptor(struct libusb_device *dev, uint8_t config_index, unsigned char *buffer, size_t len, int *host_endian) {
|
|
|
|
struct darwin_cached_device *priv = DARWIN_CACHED_DEVICE(dev);
|
|
|
|
IOUSBConfigurationDescriptorPtr desc;
|
|
|
|
IOReturn kresult;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!priv || !priv->device)
|
|
|
|
return LIBUSB_ERROR_OTHER;
|
|
|
|
|
|
|
|
kresult = (*priv->device)->GetConfigurationDescriptorPtr (priv->device, config_index, &desc);
|
|
|
|
if (kresult == kIOReturnSuccess) {
|
|
|
|
/* copy descriptor */
|
|
|
|
if (libusb_le16_to_cpu(desc->wTotalLength) < len)
|
|
|
|
len = libusb_le16_to_cpu(desc->wTotalLength);
|
|
|
|
|
|
|
|
memmove (buffer, desc, len);
|
|
|
|
|
|
|
|
/* GetConfigurationDescriptorPtr returns the descriptor in USB bus order */
|
|
|
|
*host_endian = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = darwin_to_libusb (kresult);
|
|
|
|
if (ret != LIBUSB_SUCCESS)
|
|
|
|
return ret;
|
|
|
|
|
2014-12-27 15:39:17 +01:00
|
|
|
return (int) len;
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check whether the os has configured the device */
|
2019-05-01 11:47:48 +02:00
|
|
|
static enum libusb_error darwin_check_configuration (struct libusb_context *ctx, struct darwin_cached_device *dev) {
|
2013-09-30 18:36:54 +13:00
|
|
|
usb_device_t **darwin_device = dev->device;
|
|
|
|
|
|
|
|
IOUSBConfigurationDescriptorPtr configDesc;
|
|
|
|
IOUSBFindInterfaceRequest request;
|
2019-05-01 11:47:48 +02:00
|
|
|
IOReturn kresult;
|
2013-09-30 18:36:54 +13:00
|
|
|
io_iterator_t interface_iterator;
|
|
|
|
io_service_t firstInterface;
|
|
|
|
|
|
|
|
if (dev->dev_descriptor.bNumConfigurations < 1) {
|
|
|
|
usbi_err (ctx, "device has no configurations");
|
|
|
|
return LIBUSB_ERROR_OTHER; /* no configurations at this speed so we can't use it */
|
|
|
|
}
|
|
|
|
|
2016-11-20 14:07:07 +01:00
|
|
|
/* checking the configuration of a root hub simulation takes ~1 s in 10.11. the device is
|
|
|
|
not usable anyway */
|
|
|
|
if (0x05ac == dev->dev_descriptor.idVendor && 0x8005 == dev->dev_descriptor.idProduct) {
|
|
|
|
usbi_dbg ("ignoring configuration on root hub simulation");
|
|
|
|
dev->active_config = 0;
|
2019-05-01 11:47:48 +02:00
|
|
|
return LIBUSB_SUCCESS;
|
2016-11-20 14:07:07 +01:00
|
|
|
}
|
|
|
|
|
2013-09-30 18:36:54 +13:00
|
|
|
/* find the first configuration */
|
|
|
|
kresult = (*darwin_device)->GetConfigurationDescriptorPtr (darwin_device, 0, &configDesc);
|
|
|
|
dev->first_config = (kIOReturnSuccess == kresult) ? configDesc->bConfigurationValue : 1;
|
|
|
|
|
|
|
|
/* check if the device is already configured. there is probably a better way than iterating over the
|
|
|
|
to accomplish this (the trick is we need to avoid a call to GetConfigurations since buggy devices
|
|
|
|
might lock up on the device request) */
|
|
|
|
|
|
|
|
/* Setup the Interface Request */
|
|
|
|
request.bInterfaceClass = kIOUSBFindInterfaceDontCare;
|
|
|
|
request.bInterfaceSubClass = kIOUSBFindInterfaceDontCare;
|
|
|
|
request.bInterfaceProtocol = kIOUSBFindInterfaceDontCare;
|
|
|
|
request.bAlternateSetting = kIOUSBFindInterfaceDontCare;
|
|
|
|
|
|
|
|
kresult = (*(darwin_device))->CreateInterfaceIterator(darwin_device, &request, &interface_iterator);
|
2019-05-01 11:47:48 +02:00
|
|
|
if (kresult != kIOReturnSuccess)
|
2013-09-30 18:36:54 +13:00
|
|
|
return darwin_to_libusb (kresult);
|
|
|
|
|
|
|
|
/* iterate once */
|
|
|
|
firstInterface = IOIteratorNext(interface_iterator);
|
|
|
|
|
|
|
|
/* done with the interface iterator */
|
|
|
|
IOObjectRelease(interface_iterator);
|
|
|
|
|
|
|
|
if (firstInterface) {
|
|
|
|
IOObjectRelease (firstInterface);
|
|
|
|
|
|
|
|
/* device is configured */
|
|
|
|
if (dev->dev_descriptor.bNumConfigurations == 1)
|
|
|
|
/* to avoid problems with some devices get the configurations value from the configuration descriptor */
|
|
|
|
dev->active_config = dev->first_config;
|
|
|
|
else
|
|
|
|
/* devices with more than one configuration should work with GetConfiguration */
|
|
|
|
(*darwin_device)->GetConfiguration (darwin_device, &dev->active_config);
|
|
|
|
} else
|
|
|
|
/* not configured */
|
|
|
|
dev->active_config = 0;
|
|
|
|
|
|
|
|
usbi_dbg ("active config: %u, first config: %u", dev->active_config, dev->first_config);
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
return LIBUSB_SUCCESS;
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
static IOReturn darwin_request_descriptor (usb_device_t **device, UInt8 desc, UInt8 desc_index, void *buffer, size_t buffer_size) {
|
2013-09-30 18:36:54 +13:00
|
|
|
IOUSBDevRequestTO req;
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
assert(buffer_size <= UINT16_MAX);
|
|
|
|
|
2013-09-30 18:36:54 +13:00
|
|
|
memset (buffer, 0, buffer_size);
|
|
|
|
|
|
|
|
/* Set up request for descriptor/ */
|
|
|
|
req.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
|
|
|
|
req.bRequest = kUSBRqGetDescriptor;
|
2019-05-01 11:47:48 +02:00
|
|
|
req.wValue = (UInt16)(desc << 8);
|
2013-09-30 18:36:54 +13:00
|
|
|
req.wIndex = desc_index;
|
2019-05-01 11:47:48 +02:00
|
|
|
req.wLength = (UInt16)buffer_size;
|
2013-09-30 18:36:54 +13:00
|
|
|
req.pData = buffer;
|
|
|
|
req.noDataTimeout = 20;
|
|
|
|
req.completionTimeout = 100;
|
|
|
|
|
|
|
|
return (*device)->DeviceRequestTO (device, &req);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
static enum libusb_error darwin_cache_device_descriptor (struct libusb_context *ctx, struct darwin_cached_device *dev) {
|
2013-09-30 18:36:54 +13:00
|
|
|
usb_device_t **device = dev->device;
|
2019-05-01 11:47:48 +02:00
|
|
|
int retries = 1;
|
|
|
|
long delay = 30000; // microseconds
|
2013-09-30 18:36:54 +13:00
|
|
|
int unsuspended = 0, try_unsuspend = 1, try_reconfigure = 1;
|
|
|
|
int is_open = 0;
|
2019-05-01 11:47:48 +02:00
|
|
|
IOReturn ret = 0, ret2;
|
2013-09-30 18:36:54 +13:00
|
|
|
UInt8 bDeviceClass;
|
|
|
|
UInt16 idProduct, idVendor;
|
|
|
|
|
|
|
|
dev->can_enumerate = 0;
|
|
|
|
|
|
|
|
(*device)->GetDeviceClass (device, &bDeviceClass);
|
|
|
|
(*device)->GetDeviceProduct (device, &idProduct);
|
|
|
|
(*device)->GetDeviceVendor (device, &idVendor);
|
|
|
|
|
|
|
|
/* According to Apple's documentation the device must be open for DeviceRequest but we may not be able to open some
|
|
|
|
* devices and Apple's USB Prober doesn't bother to open the device before issuing a descriptor request. Still,
|
|
|
|
* to follow the spec as closely as possible, try opening the device */
|
|
|
|
is_open = ((*device)->USBDeviceOpenSeize(device) == kIOReturnSuccess);
|
|
|
|
|
|
|
|
do {
|
|
|
|
/**** retrieve device descriptor ****/
|
|
|
|
ret = darwin_request_descriptor (device, kUSBDeviceDesc, 0, &dev->dev_descriptor, sizeof(dev->dev_descriptor));
|
|
|
|
|
|
|
|
if (kIOReturnOverrun == ret && kUSBDeviceDesc == dev->dev_descriptor.bDescriptorType)
|
|
|
|
/* received an overrun error but we still received a device descriptor */
|
|
|
|
ret = kIOReturnSuccess;
|
|
|
|
|
|
|
|
if (kIOUSBVendorIDAppleComputer == idVendor) {
|
|
|
|
/* NTH: don't bother retrying or unsuspending Apple devices */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kIOReturnSuccess == ret && (0 == dev->dev_descriptor.bNumConfigurations ||
|
|
|
|
0 == dev->dev_descriptor.bcdUSB)) {
|
|
|
|
/* work around for incorrectly configured devices */
|
|
|
|
if (try_reconfigure && is_open) {
|
|
|
|
usbi_dbg("descriptor appears to be invalid. resetting configuration before trying again...");
|
|
|
|
|
|
|
|
/* set the first configuration */
|
|
|
|
(*device)->SetConfiguration(device, 1);
|
|
|
|
|
|
|
|
/* don't try to reconfigure again */
|
|
|
|
try_reconfigure = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = kIOUSBPipeStalled;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kIOReturnSuccess != ret && is_open && try_unsuspend) {
|
|
|
|
/* device may be suspended. unsuspend it and try again */
|
|
|
|
#if DeviceVersion >= 320
|
|
|
|
UInt32 info = 0;
|
|
|
|
|
|
|
|
/* IOUSBFamily 320+ provides a way to detect device suspension but earlier versions do not */
|
|
|
|
(void)(*device)->GetUSBDeviceInformation (device, &info);
|
|
|
|
|
|
|
|
/* note that the device was suspended */
|
2019-05-01 11:47:48 +02:00
|
|
|
if (info & (1U << kUSBInformationDeviceIsSuspendedBit) || 0 == info)
|
2013-09-30 18:36:54 +13:00
|
|
|
try_unsuspend = 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (try_unsuspend) {
|
|
|
|
/* try to unsuspend the device */
|
|
|
|
ret2 = (*device)->USBDeviceSuspend (device, 0);
|
|
|
|
if (kIOReturnSuccess != ret2) {
|
|
|
|
/* prevent log spew from poorly behaving devices. this indicates the
|
|
|
|
os actually had trouble communicating with the device */
|
|
|
|
usbi_dbg("could not retrieve device descriptor. failed to unsuspend: %s",darwin_error_str(ret2));
|
|
|
|
} else
|
|
|
|
unsuspended = 1;
|
|
|
|
|
|
|
|
try_unsuspend = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kIOReturnSuccess != ret) {
|
2019-05-01 11:47:48 +02:00
|
|
|
usbi_dbg("kernel responded with code: 0x%08x. sleeping for %ld ms before trying again", ret, delay/1000);
|
2013-09-30 18:36:54 +13:00
|
|
|
/* sleep for a little while before trying again */
|
2019-05-01 11:47:48 +02:00
|
|
|
nanosleep(&(struct timespec){delay / 1000000, (delay * 1000) % 1000000000}, NULL);
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
} while (kIOReturnSuccess != ret && retries--);
|
|
|
|
|
|
|
|
if (unsuspended)
|
|
|
|
/* resuspend the device */
|
|
|
|
(void)(*device)->USBDeviceSuspend (device, 1);
|
|
|
|
|
|
|
|
if (is_open)
|
|
|
|
(void) (*device)->USBDeviceClose (device);
|
|
|
|
|
|
|
|
if (ret != kIOReturnSuccess) {
|
|
|
|
/* a debug message was already printed out for this error */
|
|
|
|
if (LIBUSB_CLASS_HUB == bDeviceClass)
|
|
|
|
usbi_dbg ("could not retrieve device descriptor %.4x:%.4x: %s (%x). skipping device",
|
|
|
|
idVendor, idProduct, darwin_error_str (ret), ret);
|
|
|
|
else
|
|
|
|
usbi_warn (ctx, "could not retrieve device descriptor %.4x:%.4x: %s (%x). skipping device",
|
|
|
|
idVendor, idProduct, darwin_error_str (ret), ret);
|
2014-12-27 15:39:17 +01:00
|
|
|
return darwin_to_libusb (ret);
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/* catch buggy hubs (which appear to be virtual). Apple's own USB prober has problems with these devices. */
|
|
|
|
if (libusb_le16_to_cpu (dev->dev_descriptor.idProduct) != idProduct) {
|
|
|
|
/* not a valid device */
|
|
|
|
usbi_warn (ctx, "idProduct from iokit (%04x) does not match idProduct in descriptor (%04x). skipping device",
|
|
|
|
idProduct, libusb_le16_to_cpu (dev->dev_descriptor.idProduct));
|
2014-12-27 15:39:17 +01:00
|
|
|
return LIBUSB_ERROR_NO_DEVICE;
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
usbi_dbg ("cached device descriptor:");
|
|
|
|
usbi_dbg (" bDescriptorType: 0x%02x", dev->dev_descriptor.bDescriptorType);
|
|
|
|
usbi_dbg (" bcdUSB: 0x%04x", dev->dev_descriptor.bcdUSB);
|
|
|
|
usbi_dbg (" bDeviceClass: 0x%02x", dev->dev_descriptor.bDeviceClass);
|
|
|
|
usbi_dbg (" bDeviceSubClass: 0x%02x", dev->dev_descriptor.bDeviceSubClass);
|
|
|
|
usbi_dbg (" bDeviceProtocol: 0x%02x", dev->dev_descriptor.bDeviceProtocol);
|
|
|
|
usbi_dbg (" bMaxPacketSize0: 0x%02x", dev->dev_descriptor.bMaxPacketSize0);
|
|
|
|
usbi_dbg (" idVendor: 0x%04x", dev->dev_descriptor.idVendor);
|
|
|
|
usbi_dbg (" idProduct: 0x%04x", dev->dev_descriptor.idProduct);
|
|
|
|
usbi_dbg (" bcdDevice: 0x%04x", dev->dev_descriptor.bcdDevice);
|
|
|
|
usbi_dbg (" iManufacturer: 0x%02x", dev->dev_descriptor.iManufacturer);
|
|
|
|
usbi_dbg (" iProduct: 0x%02x", dev->dev_descriptor.iProduct);
|
|
|
|
usbi_dbg (" iSerialNumber: 0x%02x", dev->dev_descriptor.iSerialNumber);
|
|
|
|
usbi_dbg (" bNumConfigurations: 0x%02x", dev->dev_descriptor.bNumConfigurations);
|
|
|
|
|
|
|
|
dev->can_enumerate = 1;
|
|
|
|
|
2014-12-27 15:39:17 +01:00
|
|
|
return LIBUSB_SUCCESS;
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
/* Returns 1 on success, 0 on failure. */
|
|
|
|
static bool get_device_port (io_service_t service, UInt8 *port) {
|
|
|
|
IOReturn kresult;
|
2016-11-20 14:07:07 +01:00
|
|
|
io_service_t parent;
|
2019-05-01 11:47:48 +02:00
|
|
|
bool ret = false;
|
2016-11-20 14:07:07 +01:00
|
|
|
|
|
|
|
if (get_ioregistry_value_number (service, CFSTR("PortNum"), kCFNumberSInt8Type, port)) {
|
2019-05-01 11:47:48 +02:00
|
|
|
return true;
|
2016-11-20 14:07:07 +01:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
kresult = IORegistryEntryGetParentEntry (service, kIOServicePlane, &parent);
|
|
|
|
if (kIOReturnSuccess == kresult) {
|
2016-11-20 14:07:07 +01:00
|
|
|
ret = get_ioregistry_value_data (parent, CFSTR("port"), 1, port);
|
|
|
|
IOObjectRelease (parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
/* Returns 1 on success, 0 on failure. */
|
|
|
|
static bool get_device_parent_sessionID(io_service_t service, UInt64 *parent_sessionID) {
|
|
|
|
IOReturn kresult;
|
|
|
|
io_service_t parent;
|
|
|
|
|
|
|
|
/* Walk up the tree in the IOService plane until we find a parent that has a sessionID */
|
|
|
|
parent = service;
|
|
|
|
while((kresult = IORegistryEntryGetParentEntry (parent, kIOUSBPlane, &parent)) == kIOReturnSuccess) {
|
|
|
|
if (get_ioregistry_value_number (parent, CFSTR("sessionID"), kCFNumberSInt64Type, parent_sessionID)) {
|
|
|
|
/* Success */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We ran out of parents */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum libusb_error darwin_get_cached_device(struct libusb_context *ctx, io_service_t service,
|
|
|
|
struct darwin_cached_device **cached_out) {
|
2013-09-30 18:36:54 +13:00
|
|
|
struct darwin_cached_device *new_device;
|
2014-12-27 15:39:17 +01:00
|
|
|
UInt64 sessionID = 0, parent_sessionID = 0;
|
2019-05-01 11:47:48 +02:00
|
|
|
UInt32 locationID = 0;
|
|
|
|
enum libusb_error ret = LIBUSB_SUCCESS;
|
2013-09-30 18:36:54 +13:00
|
|
|
usb_device_t **device;
|
|
|
|
UInt8 port = 0;
|
2019-05-01 11:47:48 +02:00
|
|
|
bool reuse_device = false;
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
/* get some info from the io registry */
|
|
|
|
(void) get_ioregistry_value_number (service, CFSTR("sessionID"), kCFNumberSInt64Type, &sessionID);
|
2019-05-01 11:47:48 +02:00
|
|
|
(void) get_ioregistry_value_number (service, CFSTR("locationID"), kCFNumberSInt32Type, &locationID);
|
2016-11-20 14:07:07 +01:00
|
|
|
if (!get_device_port (service, &port)) {
|
|
|
|
usbi_dbg("could not get connected port number");
|
|
|
|
}
|
2013-09-30 18:36:54 +13:00
|
|
|
|
2014-12-27 15:39:17 +01:00
|
|
|
usbi_dbg("finding cached device for sessionID 0x%" PRIx64, sessionID);
|
2013-09-30 18:36:54 +13:00
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
if (get_device_parent_sessionID(service, &parent_sessionID)) {
|
|
|
|
usbi_dbg("parent sessionID: 0x%" PRIx64, parent_sessionID);
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
usbi_mutex_lock(&darwin_cached_devices_lock);
|
|
|
|
do {
|
2014-12-27 15:39:17 +01:00
|
|
|
*cached_out = NULL;
|
|
|
|
|
2013-09-30 18:36:54 +13:00
|
|
|
list_for_each_entry(new_device, &darwin_cached_devices, list, struct darwin_cached_device) {
|
2019-05-01 11:47:48 +02:00
|
|
|
usbi_dbg("matching sessionID/locationID 0x%" PRIx64 "/0x%x against cached device with sessionID/locationID 0x%" PRIx64 "/0x%x",
|
|
|
|
sessionID, locationID, new_device->session, new_device->location);
|
|
|
|
if (new_device->location == locationID && new_device->in_reenumerate) {
|
|
|
|
usbi_dbg ("found cached device with matching location that is being re-enumerated");
|
|
|
|
new_device->session = sessionID;
|
|
|
|
reuse_device = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-09-30 18:36:54 +13:00
|
|
|
if (new_device->session == sessionID) {
|
|
|
|
usbi_dbg("using cached device for device");
|
|
|
|
*cached_out = new_device;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*cached_out)
|
|
|
|
break;
|
|
|
|
|
2014-12-27 15:39:17 +01:00
|
|
|
usbi_dbg("caching new device with sessionID 0x%" PRIx64, sessionID);
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
device = darwin_device_from_service (service);
|
|
|
|
if (!device) {
|
|
|
|
ret = LIBUSB_ERROR_NO_DEVICE;
|
2014-12-27 15:39:17 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
if (!reuse_device) {
|
|
|
|
new_device = calloc (1, sizeof (*new_device));
|
|
|
|
if (!new_device) {
|
|
|
|
ret = LIBUSB_ERROR_NO_MEM;
|
|
|
|
break;
|
|
|
|
}
|
2013-09-30 18:36:54 +13:00
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
/* add this device to the cached device list */
|
|
|
|
list_add(&new_device->list, &darwin_cached_devices);
|
2013-09-30 18:36:54 +13:00
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
(*device)->GetDeviceAddress (device, (USBDeviceAddress *)&new_device->address);
|
2013-09-30 18:36:54 +13:00
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
/* keep a reference to this device */
|
|
|
|
darwin_ref_cached_device(new_device);
|
|
|
|
|
|
|
|
new_device->session = sessionID;
|
|
|
|
(*device)->GetLocationID (device, &new_device->location);
|
|
|
|
new_device->port = port;
|
|
|
|
new_device->parent_session = parent_sessionID;
|
|
|
|
}
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
new_device->device = device;
|
|
|
|
|
|
|
|
/* cache the device descriptor */
|
|
|
|
ret = darwin_cache_device_descriptor(ctx, new_device);
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (new_device->can_enumerate) {
|
|
|
|
snprintf(new_device->sys_path, 20, "%03i-%04x-%04x-%02x-%02x", new_device->address,
|
|
|
|
new_device->dev_descriptor.idVendor, new_device->dev_descriptor.idProduct,
|
|
|
|
new_device->dev_descriptor.bDeviceClass, new_device->dev_descriptor.bDeviceSubClass);
|
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
usbi_mutex_unlock(&darwin_cached_devices_lock);
|
|
|
|
|
|
|
|
/* keep track of devices regardless of if we successfully enumerate them to
|
|
|
|
prevent them from being enumerated multiple times */
|
|
|
|
|
|
|
|
*cached_out = new_device;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
static enum libusb_error process_new_device (struct libusb_context *ctx, io_service_t service) {
|
2013-09-30 18:36:54 +13:00
|
|
|
struct darwin_device_priv *priv;
|
|
|
|
struct libusb_device *dev = NULL;
|
|
|
|
struct darwin_cached_device *cached_device;
|
|
|
|
UInt8 devSpeed;
|
2019-05-01 11:47:48 +02:00
|
|
|
enum libusb_error ret = LIBUSB_SUCCESS;
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
do {
|
|
|
|
ret = darwin_get_cached_device (ctx, service, &cached_device);
|
2014-12-27 15:39:17 +01:00
|
|
|
if (ret < 0 || !cached_device->can_enumerate) {
|
2013-09-30 18:36:54 +13:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check current active configuration (and cache the first configuration value--
|
|
|
|
which may be used by claim_interface) */
|
|
|
|
ret = darwin_check_configuration (ctx, cached_device);
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
|
2014-12-27 15:39:17 +01:00
|
|
|
usbi_dbg ("allocating new device in context %p for with session 0x%" PRIx64,
|
2013-09-30 18:36:54 +13:00
|
|
|
ctx, cached_device->session);
|
|
|
|
|
2014-12-27 15:39:17 +01:00
|
|
|
dev = usbi_alloc_device(ctx, (unsigned long) cached_device->session);
|
2013-09-30 18:36:54 +13:00
|
|
|
if (!dev) {
|
|
|
|
return LIBUSB_ERROR_NO_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv = (struct darwin_device_priv *)dev->os_priv;
|
|
|
|
|
|
|
|
priv->dev = cached_device;
|
|
|
|
darwin_ref_cached_device (priv->dev);
|
|
|
|
|
|
|
|
if (cached_device->parent_session > 0) {
|
2014-12-27 15:39:17 +01:00
|
|
|
dev->parent_dev = usbi_get_device_by_session_id (ctx, (unsigned long) cached_device->parent_session);
|
2013-09-30 18:36:54 +13:00
|
|
|
} else {
|
|
|
|
dev->parent_dev = NULL;
|
|
|
|
}
|
|
|
|
dev->port_number = cached_device->port;
|
|
|
|
dev->bus_number = cached_device->location >> 24;
|
2019-05-01 11:47:48 +02:00
|
|
|
assert(cached_device->address <= UINT8_MAX);
|
|
|
|
dev->device_address = (uint8_t)cached_device->address;
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
(*(priv->dev->device))->GetDeviceSpeed (priv->dev->device, &devSpeed);
|
|
|
|
|
|
|
|
switch (devSpeed) {
|
|
|
|
case kUSBDeviceSpeedLow: dev->speed = LIBUSB_SPEED_LOW; break;
|
|
|
|
case kUSBDeviceSpeedFull: dev->speed = LIBUSB_SPEED_FULL; break;
|
|
|
|
case kUSBDeviceSpeedHigh: dev->speed = LIBUSB_SPEED_HIGH; break;
|
2019-05-01 11:47:48 +02:00
|
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
|
2013-09-30 18:36:54 +13:00
|
|
|
case kUSBDeviceSpeedSuper: dev->speed = LIBUSB_SPEED_SUPER; break;
|
2019-05-01 11:47:48 +02:00
|
|
|
#endif
|
|
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 101200
|
|
|
|
case kUSBDeviceSpeedSuperPlus: dev->speed = LIBUSB_SPEED_SUPER_PLUS; break;
|
2013-09-30 18:36:54 +13:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
usbi_warn (ctx, "Got unknown device speed %d", devSpeed);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = usbi_sanitize_device (dev);
|
|
|
|
if (ret < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
usbi_dbg ("found device with address %d port = %d parent = %p at %p", dev->device_address,
|
|
|
|
dev->port_number, (void *) dev->parent_dev, priv->dev->sys_path);
|
2019-05-01 11:47:48 +02:00
|
|
|
|
2013-09-30 18:36:54 +13:00
|
|
|
} while (0);
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
if (cached_device->in_reenumerate) {
|
|
|
|
usbi_dbg ("cached device in reset state. reset complete...");
|
|
|
|
cached_device->in_reenumerate = false;
|
|
|
|
} else if (0 == ret) {
|
2013-09-30 18:36:54 +13:00
|
|
|
usbi_connect_device (dev);
|
|
|
|
} else {
|
|
|
|
libusb_unref_device (dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
static enum libusb_error darwin_scan_devices(struct libusb_context *ctx) {
|
2013-09-30 18:36:54 +13:00
|
|
|
io_iterator_t deviceIterator;
|
|
|
|
io_service_t service;
|
2019-05-01 11:47:48 +02:00
|
|
|
IOReturn kresult;
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
kresult = usb_setup_device_iterator (&deviceIterator, 0);
|
|
|
|
if (kresult != kIOReturnSuccess)
|
|
|
|
return darwin_to_libusb (kresult);
|
|
|
|
|
|
|
|
while ((service = IOIteratorNext (deviceIterator))) {
|
|
|
|
(void) process_new_device (ctx, service);
|
|
|
|
|
|
|
|
IOObjectRelease(service);
|
|
|
|
}
|
|
|
|
|
|
|
|
IOObjectRelease(deviceIterator);
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
return LIBUSB_SUCCESS;
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
static int darwin_open (struct libusb_device_handle *dev_handle) {
|
|
|
|
struct darwin_device_handle_priv *priv = (struct darwin_device_handle_priv *)dev_handle->os_priv;
|
|
|
|
struct darwin_cached_device *dpriv = DARWIN_CACHED_DEVICE(dev_handle->dev);
|
|
|
|
IOReturn kresult;
|
|
|
|
|
|
|
|
if (0 == dpriv->open_count) {
|
|
|
|
/* try to open the device */
|
|
|
|
kresult = (*(dpriv->device))->USBDeviceOpenSeize (dpriv->device);
|
|
|
|
if (kresult != kIOReturnSuccess) {
|
|
|
|
usbi_warn (HANDLE_CTX (dev_handle), "USBDeviceOpen: %s", darwin_error_str(kresult));
|
|
|
|
|
|
|
|
if (kIOReturnExclusiveAccess != kresult) {
|
|
|
|
return darwin_to_libusb (kresult);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* it is possible to perform some actions on a device that is not open so do not return an error */
|
2019-05-01 11:47:48 +02:00
|
|
|
priv->is_open = false;
|
2013-09-30 18:36:54 +13:00
|
|
|
} else {
|
2019-05-01 11:47:48 +02:00
|
|
|
priv->is_open = true;
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/* create async event source */
|
|
|
|
kresult = (*(dpriv->device))->CreateDeviceAsyncEventSource (dpriv->device, &priv->cfSource);
|
|
|
|
if (kresult != kIOReturnSuccess) {
|
|
|
|
usbi_err (HANDLE_CTX (dev_handle), "CreateDeviceAsyncEventSource: %s", darwin_error_str(kresult));
|
|
|
|
|
|
|
|
if (priv->is_open) {
|
|
|
|
(*(dpriv->device))->USBDeviceClose (dpriv->device);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
priv->is_open = false;
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
return darwin_to_libusb (kresult);
|
|
|
|
}
|
|
|
|
|
|
|
|
CFRetain (libusb_darwin_acfl);
|
|
|
|
|
|
|
|
/* add the cfSource to the aync run loop */
|
|
|
|
CFRunLoopAddSource(libusb_darwin_acfl, priv->cfSource, kCFRunLoopCommonModes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* device opened successfully */
|
|
|
|
dpriv->open_count++;
|
|
|
|
|
|
|
|
usbi_dbg ("device open for access");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void darwin_close (struct libusb_device_handle *dev_handle) {
|
|
|
|
struct darwin_device_handle_priv *priv = (struct darwin_device_handle_priv *)dev_handle->os_priv;
|
|
|
|
struct darwin_cached_device *dpriv = DARWIN_CACHED_DEVICE(dev_handle->dev);
|
|
|
|
IOReturn kresult;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (dpriv->open_count == 0) {
|
|
|
|
/* something is probably very wrong if this is the case */
|
2016-08-25 18:27:40 -07:00
|
|
|
usbi_err (HANDLE_CTX (dev_handle), "Close called on a device that was not open!");
|
2013-09-30 18:36:54 +13:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dpriv->open_count--;
|
|
|
|
|
|
|
|
/* make sure all interfaces are released */
|
|
|
|
for (i = 0 ; i < USB_MAXINTERFACES ; i++)
|
2019-05-01 11:47:48 +02:00
|
|
|
if (dev_handle->claimed_interfaces & (1U << i))
|
2013-09-30 18:36:54 +13:00
|
|
|
libusb_release_interface (dev_handle, i);
|
|
|
|
|
|
|
|
if (0 == dpriv->open_count) {
|
|
|
|
/* delete the device's async event source */
|
|
|
|
if (priv->cfSource) {
|
|
|
|
CFRunLoopRemoveSource (libusb_darwin_acfl, priv->cfSource, kCFRunLoopDefaultMode);
|
|
|
|
CFRelease (priv->cfSource);
|
|
|
|
priv->cfSource = NULL;
|
|
|
|
CFRelease (libusb_darwin_acfl);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->is_open) {
|
|
|
|
/* close the device */
|
|
|
|
kresult = (*(dpriv->device))->USBDeviceClose(dpriv->device);
|
2019-05-01 11:47:48 +02:00
|
|
|
if (kresult != kIOReturnSuccess) {
|
2013-09-30 18:36:54 +13:00
|
|
|
/* Log the fact that we had a problem closing the file, however failing a
|
|
|
|
* close isn't really an error, so return success anyway */
|
|
|
|
usbi_warn (HANDLE_CTX (dev_handle), "USBDeviceClose: %s", darwin_error_str(kresult));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int darwin_get_configuration(struct libusb_device_handle *dev_handle, int *config) {
|
|
|
|
struct darwin_cached_device *dpriv = DARWIN_CACHED_DEVICE(dev_handle->dev);
|
|
|
|
|
|
|
|
*config = (int) dpriv->active_config;
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
return LIBUSB_SUCCESS;
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
static enum libusb_error darwin_set_configuration(struct libusb_device_handle *dev_handle, int config) {
|
2013-09-30 18:36:54 +13:00
|
|
|
struct darwin_cached_device *dpriv = DARWIN_CACHED_DEVICE(dev_handle->dev);
|
|
|
|
IOReturn kresult;
|
|
|
|
int i;
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
assert(config >= 0 && config <= UINT8_MAX);
|
|
|
|
|
2013-09-30 18:36:54 +13:00
|
|
|
/* Setting configuration will invalidate the interface, so we need
|
|
|
|
to reclaim it. First, dispose of existing interfaces, if any. */
|
|
|
|
for (i = 0 ; i < USB_MAXINTERFACES ; i++)
|
2019-05-01 11:47:48 +02:00
|
|
|
if (dev_handle->claimed_interfaces & (1U << i))
|
2013-09-30 18:36:54 +13:00
|
|
|
darwin_release_interface (dev_handle, i);
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
kresult = (*(dpriv->device))->SetConfiguration (dpriv->device, (UInt8)config);
|
2013-09-30 18:36:54 +13:00
|
|
|
if (kresult != kIOReturnSuccess)
|
|
|
|
return darwin_to_libusb (kresult);
|
|
|
|
|
|
|
|
/* Reclaim any interfaces. */
|
|
|
|
for (i = 0 ; i < USB_MAXINTERFACES ; i++)
|
2019-05-01 11:47:48 +02:00
|
|
|
if (dev_handle->claimed_interfaces & (1U << i))
|
2013-09-30 18:36:54 +13:00
|
|
|
darwin_claim_interface (dev_handle, i);
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
dpriv->active_config = (UInt8)config;
|
2013-09-30 18:36:54 +13:00
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
return LIBUSB_SUCCESS;
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
static IOReturn darwin_get_interface (usb_device_t **darwin_device, int ifc, io_service_t *usbInterfacep) {
|
2013-09-30 18:36:54 +13:00
|
|
|
IOUSBFindInterfaceRequest request;
|
2019-05-01 11:47:48 +02:00
|
|
|
IOReturn kresult;
|
2013-09-30 18:36:54 +13:00
|
|
|
io_iterator_t interface_iterator;
|
|
|
|
UInt8 bInterfaceNumber;
|
2019-05-01 11:47:48 +02:00
|
|
|
bool ret;
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
*usbInterfacep = IO_OBJECT_NULL;
|
|
|
|
|
|
|
|
/* Setup the Interface Request */
|
|
|
|
request.bInterfaceClass = kIOUSBFindInterfaceDontCare;
|
|
|
|
request.bInterfaceSubClass = kIOUSBFindInterfaceDontCare;
|
|
|
|
request.bInterfaceProtocol = kIOUSBFindInterfaceDontCare;
|
|
|
|
request.bAlternateSetting = kIOUSBFindInterfaceDontCare;
|
|
|
|
|
|
|
|
kresult = (*(darwin_device))->CreateInterfaceIterator(darwin_device, &request, &interface_iterator);
|
2019-05-01 11:47:48 +02:00
|
|
|
if (kresult != kIOReturnSuccess)
|
2013-09-30 18:36:54 +13:00
|
|
|
return kresult;
|
|
|
|
|
|
|
|
while ((*usbInterfacep = IOIteratorNext(interface_iterator))) {
|
|
|
|
/* find the interface number */
|
|
|
|
ret = get_ioregistry_value_number (*usbInterfacep, CFSTR("bInterfaceNumber"), kCFNumberSInt8Type,
|
|
|
|
&bInterfaceNumber);
|
|
|
|
|
|
|
|
if (ret && bInterfaceNumber == ifc) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
(void) IOObjectRelease (*usbInterfacep);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* done with the interface iterator */
|
|
|
|
IOObjectRelease(interface_iterator);
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
return kIOReturnSuccess;
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
static enum libusb_error get_endpoints (struct libusb_device_handle *dev_handle, int iface) {
|
2013-09-30 18:36:54 +13:00
|
|
|
struct darwin_device_handle_priv *priv = (struct darwin_device_handle_priv *)dev_handle->os_priv;
|
|
|
|
|
|
|
|
/* current interface */
|
|
|
|
struct darwin_interface *cInterface = &priv->interfaces[iface];
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
IOReturn kresult;
|
2013-09-30 18:36:54 +13:00
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
UInt8 numep, direction, number;
|
|
|
|
UInt8 dont_care1, dont_care3;
|
|
|
|
UInt16 dont_care2;
|
2016-11-20 14:07:07 +01:00
|
|
|
int rc;
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
usbi_dbg ("building table of endpoints.");
|
|
|
|
|
|
|
|
/* retrieve the total number of endpoints on this interface */
|
|
|
|
kresult = (*(cInterface->interface))->GetNumEndpoints(cInterface->interface, &numep);
|
2019-05-01 11:47:48 +02:00
|
|
|
if (kresult != kIOReturnSuccess) {
|
2013-09-30 18:36:54 +13:00
|
|
|
usbi_err (HANDLE_CTX (dev_handle), "can't get number of endpoints for interface: %s", darwin_error_str(kresult));
|
|
|
|
return darwin_to_libusb (kresult);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* iterate through pipe references */
|
2019-05-01 11:47:48 +02:00
|
|
|
for (UInt8 i = 1 ; i <= numep ; i++) {
|
2013-09-30 18:36:54 +13:00
|
|
|
kresult = (*(cInterface->interface))->GetPipeProperties(cInterface->interface, i, &direction, &number, &dont_care1,
|
|
|
|
&dont_care2, &dont_care3);
|
|
|
|
|
|
|
|
if (kresult != kIOReturnSuccess) {
|
2016-11-20 14:07:07 +01:00
|
|
|
/* probably a buggy device. try to get the endpoint address from the descriptors */
|
|
|
|
struct libusb_config_descriptor *config;
|
|
|
|
const struct libusb_endpoint_descriptor *endpoint_desc;
|
|
|
|
UInt8 alt_setting;
|
2013-09-30 18:36:54 +13:00
|
|
|
|
2016-11-20 14:07:07 +01:00
|
|
|
kresult = (*(cInterface->interface))->GetAlternateSetting (cInterface->interface, &alt_setting);
|
2019-05-01 11:47:48 +02:00
|
|
|
if (kresult != kIOReturnSuccess) {
|
2016-11-20 14:07:07 +01:00
|
|
|
usbi_err (HANDLE_CTX (dev_handle), "can't get alternate setting for interface");
|
|
|
|
return darwin_to_libusb (kresult);
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = libusb_get_active_config_descriptor (dev_handle->dev, &config);
|
|
|
|
if (LIBUSB_SUCCESS != rc) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint_desc = config->interface[iface].altsetting[alt_setting].endpoint + i - 1;
|
2013-09-30 18:36:54 +13:00
|
|
|
|
2016-11-20 14:07:07 +01:00
|
|
|
cInterface->endpoint_addrs[i - 1] = endpoint_desc->bEndpointAddress;
|
|
|
|
} else {
|
2019-05-01 11:47:48 +02:00
|
|
|
cInterface->endpoint_addrs[i - 1] = (UInt8)(((kUSBIn == direction) << kUSBRqDirnShift) | (number & LIBUSB_ENDPOINT_ADDRESS_MASK));
|
2016-11-20 14:07:07 +01:00
|
|
|
}
|
2013-09-30 18:36:54 +13:00
|
|
|
|
2016-11-20 14:07:07 +01:00
|
|
|
usbi_dbg ("interface: %i pipe %i: dir: %i number: %i", iface, i, cInterface->endpoint_addrs[i - 1] >> kUSBRqDirnShift,
|
|
|
|
cInterface->endpoint_addrs[i - 1] & LIBUSB_ENDPOINT_ADDRESS_MASK);
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
cInterface->num_endpoints = numep;
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
return LIBUSB_SUCCESS;
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
static int darwin_claim_interface(struct libusb_device_handle *dev_handle, int iface) {
|
|
|
|
struct darwin_cached_device *dpriv = DARWIN_CACHED_DEVICE(dev_handle->dev);
|
|
|
|
struct darwin_device_handle_priv *priv = (struct darwin_device_handle_priv *)dev_handle->os_priv;
|
|
|
|
io_service_t usbInterface = IO_OBJECT_NULL;
|
2019-05-01 11:47:48 +02:00
|
|
|
IOReturn kresult;
|
|
|
|
enum libusb_error ret;
|
2013-09-30 18:36:54 +13:00
|
|
|
IOCFPlugInInterface **plugInInterface = NULL;
|
|
|
|
SInt32 score;
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
assert(iface >= 0 && iface <= UINT8_MAX);
|
|
|
|
|
2013-09-30 18:36:54 +13:00
|
|
|
/* current interface */
|
|
|
|
struct darwin_interface *cInterface = &priv->interfaces[iface];
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
kresult = darwin_get_interface (dpriv->device, (uint8_t)iface, &usbInterface);
|
2013-09-30 18:36:54 +13:00
|
|
|
if (kresult != kIOReturnSuccess)
|
|
|
|
return darwin_to_libusb (kresult);
|
|
|
|
|
|
|
|
/* make sure we have an interface */
|
|
|
|
if (!usbInterface && dpriv->first_config != 0) {
|
|
|
|
usbi_info (HANDLE_CTX (dev_handle), "no interface found; setting configuration: %d", dpriv->first_config);
|
|
|
|
|
|
|
|
/* set the configuration */
|
2019-05-01 11:47:48 +02:00
|
|
|
ret = darwin_set_configuration (dev_handle, dpriv->first_config);
|
|
|
|
if (ret != LIBUSB_SUCCESS) {
|
2013-09-30 18:36:54 +13:00
|
|
|
usbi_err (HANDLE_CTX (dev_handle), "could not set configuration");
|
2019-05-01 11:47:48 +02:00
|
|
|
return ret;
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
kresult = darwin_get_interface (dpriv->device, (uint8_t)iface, &usbInterface);
|
|
|
|
if (kresult != kIOReturnSuccess) {
|
2013-09-30 18:36:54 +13:00
|
|
|
usbi_err (HANDLE_CTX (dev_handle), "darwin_get_interface: %s", darwin_error_str(kresult));
|
|
|
|
return darwin_to_libusb (kresult);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!usbInterface) {
|
|
|
|
usbi_err (HANDLE_CTX (dev_handle), "interface not found");
|
|
|
|
return LIBUSB_ERROR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get an interface to the device's interface */
|
|
|
|
kresult = IOCreatePlugInInterfaceForService (usbInterface, kIOUSBInterfaceUserClientTypeID,
|
|
|
|
kIOCFPlugInInterfaceID, &plugInInterface, &score);
|
|
|
|
|
|
|
|
/* ignore release error */
|
|
|
|
(void)IOObjectRelease (usbInterface);
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
if (kresult != kIOReturnSuccess) {
|
2013-09-30 18:36:54 +13:00
|
|
|
usbi_err (HANDLE_CTX (dev_handle), "IOCreatePlugInInterfaceForService: %s", darwin_error_str(kresult));
|
|
|
|
return darwin_to_libusb (kresult);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!plugInInterface) {
|
|
|
|
usbi_err (HANDLE_CTX (dev_handle), "plugin interface not found");
|
|
|
|
return LIBUSB_ERROR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do the actual claim */
|
|
|
|
kresult = (*plugInInterface)->QueryInterface(plugInInterface,
|
2019-05-01 11:47:48 +02:00
|
|
|
CFUUIDGetUUIDBytes(InterfaceInterfaceID),
|
2013-09-30 18:36:54 +13:00
|
|
|
(LPVOID)&cInterface->interface);
|
|
|
|
/* We no longer need the intermediate plug-in */
|
|
|
|
/* Use release instead of IODestroyPlugInInterface to avoid stopping IOServices associated with this device */
|
|
|
|
(*plugInInterface)->Release (plugInInterface);
|
2019-05-01 11:47:48 +02:00
|
|
|
if (kresult != kIOReturnSuccess || !cInterface->interface) {
|
2013-09-30 18:36:54 +13:00
|
|
|
usbi_err (HANDLE_CTX (dev_handle), "QueryInterface: %s", darwin_error_str(kresult));
|
|
|
|
return darwin_to_libusb (kresult);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* claim the interface */
|
|
|
|
kresult = (*(cInterface->interface))->USBInterfaceOpen(cInterface->interface);
|
2019-05-01 11:47:48 +02:00
|
|
|
if (kresult != kIOReturnSuccess) {
|
2013-09-30 18:36:54 +13:00
|
|
|
usbi_err (HANDLE_CTX (dev_handle), "USBInterfaceOpen: %s", darwin_error_str(kresult));
|
|
|
|
return darwin_to_libusb (kresult);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* update list of endpoints */
|
2019-05-01 11:47:48 +02:00
|
|
|
ret = get_endpoints (dev_handle, iface);
|
|
|
|
if (ret) {
|
2013-09-30 18:36:54 +13:00
|
|
|
/* this should not happen */
|
|
|
|
darwin_release_interface (dev_handle, iface);
|
|
|
|
usbi_err (HANDLE_CTX (dev_handle), "could not build endpoint table");
|
2019-05-01 11:47:48 +02:00
|
|
|
return ret;
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
cInterface->cfSource = NULL;
|
|
|
|
|
|
|
|
/* create async event source */
|
|
|
|
kresult = (*(cInterface->interface))->CreateInterfaceAsyncEventSource (cInterface->interface, &cInterface->cfSource);
|
|
|
|
if (kresult != kIOReturnSuccess) {
|
|
|
|
usbi_err (HANDLE_CTX (dev_handle), "could not create async event source");
|
|
|
|
|
|
|
|
/* can't continue without an async event source */
|
|
|
|
(void)darwin_release_interface (dev_handle, iface);
|
|
|
|
|
|
|
|
return darwin_to_libusb (kresult);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add the cfSource to the async thread's run loop */
|
|
|
|
CFRunLoopAddSource(libusb_darwin_acfl, cInterface->cfSource, kCFRunLoopDefaultMode);
|
|
|
|
|
|
|
|
usbi_dbg ("interface opened");
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
return LIBUSB_SUCCESS;
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
static int darwin_release_interface(struct libusb_device_handle *dev_handle, int iface) {
|
|
|
|
struct darwin_device_handle_priv *priv = (struct darwin_device_handle_priv *)dev_handle->os_priv;
|
|
|
|
IOReturn kresult;
|
|
|
|
|
|
|
|
/* current interface */
|
|
|
|
struct darwin_interface *cInterface = &priv->interfaces[iface];
|
|
|
|
|
|
|
|
/* Check to see if an interface is open */
|
|
|
|
if (!cInterface->interface)
|
|
|
|
return LIBUSB_SUCCESS;
|
|
|
|
|
|
|
|
/* clean up endpoint data */
|
|
|
|
cInterface->num_endpoints = 0;
|
|
|
|
|
|
|
|
/* delete the interface's async event source */
|
|
|
|
if (cInterface->cfSource) {
|
|
|
|
CFRunLoopRemoveSource (libusb_darwin_acfl, cInterface->cfSource, kCFRunLoopDefaultMode);
|
|
|
|
CFRelease (cInterface->cfSource);
|
|
|
|
}
|
|
|
|
|
|
|
|
kresult = (*(cInterface->interface))->USBInterfaceClose(cInterface->interface);
|
2019-05-01 11:47:48 +02:00
|
|
|
if (kresult != kIOReturnSuccess)
|
2013-09-30 18:36:54 +13:00
|
|
|
usbi_warn (HANDLE_CTX (dev_handle), "USBInterfaceClose: %s", darwin_error_str(kresult));
|
|
|
|
|
|
|
|
kresult = (*(cInterface->interface))->Release(cInterface->interface);
|
|
|
|
if (kresult != kIOReturnSuccess)
|
|
|
|
usbi_warn (HANDLE_CTX (dev_handle), "Release: %s", darwin_error_str(kresult));
|
|
|
|
|
2016-11-20 14:07:07 +01:00
|
|
|
cInterface->interface = (usb_interface_t **) IO_OBJECT_NULL;
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
return darwin_to_libusb (kresult);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int darwin_set_interface_altsetting(struct libusb_device_handle *dev_handle, int iface, int altsetting) {
|
|
|
|
struct darwin_device_handle_priv *priv = (struct darwin_device_handle_priv *)dev_handle->os_priv;
|
|
|
|
IOReturn kresult;
|
2019-05-01 11:47:48 +02:00
|
|
|
enum libusb_error ret;
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
/* current interface */
|
|
|
|
struct darwin_interface *cInterface = &priv->interfaces[iface];
|
|
|
|
|
|
|
|
if (!cInterface->interface)
|
|
|
|
return LIBUSB_ERROR_NO_DEVICE;
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
assert(altsetting >= 0 && altsetting <= UINT8_MAX);
|
|
|
|
kresult = (*(cInterface->interface))->SetAlternateInterface (cInterface->interface, (UInt8)altsetting);
|
2013-09-30 18:36:54 +13:00
|
|
|
if (kresult != kIOReturnSuccess)
|
|
|
|
darwin_reset_device (dev_handle);
|
|
|
|
|
|
|
|
/* update list of endpoints */
|
2019-05-01 11:47:48 +02:00
|
|
|
ret = get_endpoints (dev_handle, iface);
|
|
|
|
if (ret) {
|
2013-09-30 18:36:54 +13:00
|
|
|
/* this should not happen */
|
|
|
|
darwin_release_interface (dev_handle, iface);
|
|
|
|
usbi_err (HANDLE_CTX (dev_handle), "could not build endpoint table");
|
2019-05-01 11:47:48 +02:00
|
|
|
return ret;
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
return darwin_to_libusb (kresult);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int darwin_clear_halt(struct libusb_device_handle *dev_handle, unsigned char endpoint) {
|
|
|
|
/* current interface */
|
|
|
|
struct darwin_interface *cInterface;
|
|
|
|
IOReturn kresult;
|
2014-12-27 15:39:17 +01:00
|
|
|
uint8_t pipeRef;
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
/* determine the interface/endpoint to use */
|
2014-12-27 15:39:17 +01:00
|
|
|
if (ep_to_pipeRef (dev_handle, endpoint, &pipeRef, NULL, &cInterface) != 0) {
|
2013-09-30 18:36:54 +13:00
|
|
|
usbi_err (HANDLE_CTX (dev_handle), "endpoint not found on any open interface");
|
|
|
|
|
|
|
|
return LIBUSB_ERROR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* newer versions of darwin support clearing additional bits on the device's endpoint */
|
|
|
|
kresult = (*(cInterface->interface))->ClearPipeStallBothEnds(cInterface->interface, pipeRef);
|
2019-05-01 11:47:48 +02:00
|
|
|
if (kresult != kIOReturnSuccess)
|
2013-09-30 18:36:54 +13:00
|
|
|
usbi_warn (HANDLE_CTX (dev_handle), "ClearPipeStall: %s", darwin_error_str (kresult));
|
|
|
|
|
|
|
|
return darwin_to_libusb (kresult);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
static int darwin_restore_state (struct libusb_device_handle *dev_handle, int8_t active_config,
|
|
|
|
unsigned long claimed_interfaces) {
|
|
|
|
struct darwin_cached_device *dpriv = DARWIN_CACHED_DEVICE(dev_handle->dev);
|
|
|
|
struct darwin_device_handle_priv *priv = (struct darwin_device_handle_priv *)dev_handle->os_priv;
|
|
|
|
int open_count = dpriv->open_count;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* clear claimed interfaces temporarily */
|
|
|
|
dev_handle->claimed_interfaces = 0;
|
|
|
|
|
|
|
|
/* close and re-open the device */
|
|
|
|
priv->is_open = false;
|
|
|
|
dpriv->open_count = 1;
|
|
|
|
|
|
|
|
/* clean up open interfaces */
|
|
|
|
(void) darwin_close (dev_handle);
|
|
|
|
|
|
|
|
/* re-open the device */
|
|
|
|
ret = darwin_open (dev_handle);
|
|
|
|
dpriv->open_count = open_count;
|
|
|
|
if (LIBUSB_SUCCESS != ret) {
|
|
|
|
/* could not restore configuration */
|
|
|
|
return LIBUSB_ERROR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dpriv->active_config != active_config) {
|
|
|
|
usbi_dbg ("darwin/restore_state: restoring configuration %d...", active_config);
|
|
|
|
|
|
|
|
ret = darwin_set_configuration (dev_handle, active_config);
|
|
|
|
if (LIBUSB_SUCCESS != ret) {
|
|
|
|
usbi_dbg ("darwin/restore_state: could not restore configuration");
|
|
|
|
return LIBUSB_ERROR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
usbi_dbg ("darwin/restore_state: reclaiming interfaces");
|
|
|
|
|
|
|
|
if (claimed_interfaces) {
|
|
|
|
for (int iface = 0 ; iface < USB_MAXINTERFACES ; ++iface) {
|
|
|
|
if (!(claimed_interfaces & (1U << iface))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
usbi_dbg ("darwin/restore_state: re-claiming interface %d", iface);
|
|
|
|
|
|
|
|
ret = darwin_claim_interface (dev_handle, iface);
|
|
|
|
if (LIBUSB_SUCCESS != ret) {
|
|
|
|
usbi_dbg ("darwin/restore_state: could not claim interface %d", iface);
|
|
|
|
return LIBUSB_ERROR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev_handle->claimed_interfaces |= 1U << iface;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
usbi_dbg ("darwin/restore_state: device state restored");
|
|
|
|
|
|
|
|
return LIBUSB_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2013-09-30 18:36:54 +13:00
|
|
|
static int darwin_reset_device(struct libusb_device_handle *dev_handle) {
|
|
|
|
struct darwin_cached_device *dpriv = DARWIN_CACHED_DEVICE(dev_handle->dev);
|
2019-05-01 11:47:48 +02:00
|
|
|
unsigned long claimed_interfaces = dev_handle->claimed_interfaces;
|
|
|
|
int8_t active_config = dpriv->active_config;
|
2013-09-30 18:36:54 +13:00
|
|
|
IOUSBDeviceDescriptor descriptor;
|
|
|
|
IOUSBConfigurationDescriptorPtr cached_configuration;
|
2019-05-01 11:47:48 +02:00
|
|
|
IOUSBConfigurationDescriptor *cached_configurations;
|
2013-09-30 18:36:54 +13:00
|
|
|
IOReturn kresult;
|
2019-05-01 11:47:48 +02:00
|
|
|
UInt8 i;
|
2013-09-30 18:36:54 +13:00
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
if (dpriv->in_reenumerate) {
|
|
|
|
/* ack, two (or more) threads are trying to reset the device! abort! */
|
|
|
|
return LIBUSB_ERROR_NOT_FOUND;
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
dpriv->in_reenumerate = true;
|
2013-09-30 18:36:54 +13:00
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
/* store copies of descriptors so they can be compared after the reset */
|
|
|
|
memcpy (&descriptor, &dpriv->dev_descriptor, sizeof (descriptor));
|
|
|
|
cached_configurations = alloca (sizeof (*cached_configurations) * descriptor.bNumConfigurations);
|
2013-09-30 18:36:54 +13:00
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
for (i = 0 ; i < descriptor.bNumConfigurations ; ++i) {
|
|
|
|
(*(dpriv->device))->GetConfigurationDescriptorPtr (dpriv->device, i, &cached_configuration);
|
|
|
|
memcpy (cached_configurations + i, cached_configuration, sizeof (cached_configurations[i]));
|
|
|
|
}
|
2013-09-30 18:36:54 +13:00
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
/* from macOS 10.11 ResetDevice no longer does anything so just use USBDeviceReEnumerate */
|
|
|
|
kresult = (*(dpriv->device))->USBDeviceReEnumerate (dpriv->device, 0);
|
|
|
|
if (kresult != kIOReturnSuccess) {
|
|
|
|
usbi_err (HANDLE_CTX (dev_handle), "USBDeviceReEnumerate: %s", darwin_error_str (kresult));
|
|
|
|
dpriv->in_reenumerate = false;
|
|
|
|
return darwin_to_libusb (kresult);
|
|
|
|
}
|
2013-09-30 18:36:54 +13:00
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
usbi_dbg ("darwin/reset_device: waiting for re-enumeration to complete...");
|
2013-09-30 18:36:54 +13:00
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
while (dpriv->in_reenumerate) {
|
|
|
|
struct timespec delay = {.tv_sec = 0, .tv_nsec = 1000};
|
|
|
|
nanosleep (&delay, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* compare descriptors */
|
|
|
|
usbi_dbg ("darwin/reset_device: checking whether descriptors changed");
|
2013-09-30 18:36:54 +13:00
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
if (memcmp (&descriptor, &dpriv->dev_descriptor, sizeof (descriptor))) {
|
|
|
|
/* device descriptor changed. need to return not found. */
|
|
|
|
usbi_dbg ("darwin/reset_device: device descriptor changed");
|
2013-09-30 18:36:54 +13:00
|
|
|
return LIBUSB_ERROR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
for (i = 0 ; i < descriptor.bNumConfigurations ; ++i) {
|
|
|
|
(void) (*(dpriv->device))->GetConfigurationDescriptorPtr (dpriv->device, i, &cached_configuration);
|
|
|
|
if (memcmp (cached_configuration, cached_configurations + i, sizeof (cached_configurations[i]))) {
|
|
|
|
usbi_dbg ("darwin/reset_device: configuration descriptor %d changed", i);
|
|
|
|
return LIBUSB_ERROR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
usbi_dbg ("darwin/reset_device: device reset complete. restoring state...");
|
2013-09-30 18:36:54 +13:00
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
return darwin_restore_state (dev_handle, active_config, claimed_interfaces);
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
static int darwin_kernel_driver_active(struct libusb_device_handle *dev_handle, int interface) {
|
|
|
|
struct darwin_cached_device *dpriv = DARWIN_CACHED_DEVICE(dev_handle->dev);
|
|
|
|
io_service_t usbInterface;
|
|
|
|
CFTypeRef driver;
|
|
|
|
IOReturn kresult;
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
assert(interface >= 0 && interface <= UINT8_MAX);
|
|
|
|
kresult = darwin_get_interface (dpriv->device, (uint8_t)interface, &usbInterface);
|
|
|
|
if (kresult != kIOReturnSuccess) {
|
2013-09-30 18:36:54 +13:00
|
|
|
usbi_err (HANDLE_CTX (dev_handle), "darwin_get_interface: %s", darwin_error_str(kresult));
|
|
|
|
|
|
|
|
return darwin_to_libusb (kresult);
|
|
|
|
}
|
|
|
|
|
|
|
|
driver = IORegistryEntryCreateCFProperty (usbInterface, kIOBundleIdentifierKey, kCFAllocatorDefault, 0);
|
|
|
|
IOObjectRelease (usbInterface);
|
|
|
|
|
|
|
|
if (driver) {
|
|
|
|
CFRelease (driver);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* no driver */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* attaching/detaching kernel drivers is not currently supported (maybe in the future?) */
|
|
|
|
static int darwin_attach_kernel_driver (struct libusb_device_handle *dev_handle, int interface) {
|
2016-11-20 14:07:07 +01:00
|
|
|
UNUSED(dev_handle);
|
|
|
|
UNUSED(interface);
|
2013-09-30 18:36:54 +13:00
|
|
|
return LIBUSB_ERROR_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int darwin_detach_kernel_driver (struct libusb_device_handle *dev_handle, int interface) {
|
2016-11-20 14:07:07 +01:00
|
|
|
UNUSED(dev_handle);
|
|
|
|
UNUSED(interface);
|
2013-09-30 18:36:54 +13:00
|
|
|
return LIBUSB_ERROR_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void darwin_destroy_device(struct libusb_device *dev) {
|
|
|
|
struct darwin_device_priv *dpriv = (struct darwin_device_priv *) dev->os_priv;
|
|
|
|
|
|
|
|
if (dpriv->dev) {
|
|
|
|
/* need to hold the lock in case this is the last reference to the device */
|
|
|
|
usbi_mutex_lock(&darwin_cached_devices_lock);
|
|
|
|
darwin_deref_cached_device (dpriv->dev);
|
|
|
|
dpriv->dev = NULL;
|
|
|
|
usbi_mutex_unlock(&darwin_cached_devices_lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int submit_bulk_transfer(struct usbi_transfer *itransfer) {
|
|
|
|
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
|
|
|
|
|
|
|
IOReturn ret;
|
|
|
|
uint8_t transferType;
|
|
|
|
/* None of the values below are used in libusbx for bulk transfers */
|
2014-12-27 15:39:17 +01:00
|
|
|
uint8_t direction, number, interval, pipeRef;
|
2013-09-30 18:36:54 +13:00
|
|
|
uint16_t maxPacketSize;
|
|
|
|
|
|
|
|
struct darwin_interface *cInterface;
|
|
|
|
|
2014-12-27 15:39:17 +01:00
|
|
|
if (ep_to_pipeRef (transfer->dev_handle, transfer->endpoint, &pipeRef, NULL, &cInterface) != 0) {
|
2013-09-30 18:36:54 +13:00
|
|
|
usbi_err (TRANSFER_CTX (transfer), "endpoint not found on any open interface");
|
|
|
|
|
|
|
|
return LIBUSB_ERROR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2014-12-27 15:39:17 +01:00
|
|
|
ret = (*(cInterface->interface))->GetPipeProperties (cInterface->interface, pipeRef, &direction, &number,
|
|
|
|
&transferType, &maxPacketSize, &interval);
|
2013-09-30 18:36:54 +13:00
|
|
|
|
2014-12-27 15:39:17 +01:00
|
|
|
if (ret) {
|
|
|
|
usbi_err (TRANSFER_CTX (transfer), "bulk transfer failed (dir = %s): %s (code = 0x%08x)", IS_XFERIN(transfer) ? "In" : "Out",
|
|
|
|
darwin_error_str(ret), ret);
|
|
|
|
return darwin_to_libusb (ret);
|
|
|
|
}
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
if (0 != (transfer->length % maxPacketSize)) {
|
|
|
|
/* do not need a zero packet */
|
|
|
|
transfer->flags &= ~LIBUSB_TRANSFER_ADD_ZERO_PACKET;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* submit the request */
|
|
|
|
/* timeouts are unavailable on interrupt endpoints */
|
|
|
|
if (transferType == kUSBInterrupt) {
|
|
|
|
if (IS_XFERIN(transfer))
|
|
|
|
ret = (*(cInterface->interface))->ReadPipeAsync(cInterface->interface, pipeRef, transfer->buffer,
|
2019-05-01 11:47:48 +02:00
|
|
|
(UInt32)transfer->length, darwin_async_io_callback, itransfer);
|
2013-09-30 18:36:54 +13:00
|
|
|
else
|
|
|
|
ret = (*(cInterface->interface))->WritePipeAsync(cInterface->interface, pipeRef, transfer->buffer,
|
2019-05-01 11:47:48 +02:00
|
|
|
(UInt32)transfer->length, darwin_async_io_callback, itransfer);
|
2013-09-30 18:36:54 +13:00
|
|
|
} else {
|
2016-11-20 14:07:07 +01:00
|
|
|
itransfer->timeout_flags |= USBI_TRANSFER_OS_HANDLES_TIMEOUT;
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
if (IS_XFERIN(transfer))
|
|
|
|
ret = (*(cInterface->interface))->ReadPipeAsyncTO(cInterface->interface, pipeRef, transfer->buffer,
|
2019-05-01 11:47:48 +02:00
|
|
|
(UInt32)transfer->length, transfer->timeout, transfer->timeout,
|
|
|
|
darwin_async_io_callback, itransfer);
|
2013-09-30 18:36:54 +13:00
|
|
|
else
|
|
|
|
ret = (*(cInterface->interface))->WritePipeAsyncTO(cInterface->interface, pipeRef, transfer->buffer,
|
2019-05-01 11:47:48 +02:00
|
|
|
(UInt32)transfer->length, transfer->timeout, transfer->timeout,
|
|
|
|
darwin_async_io_callback, itransfer);
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
usbi_err (TRANSFER_CTX (transfer), "bulk transfer failed (dir = %s): %s (code = 0x%08x)", IS_XFERIN(transfer) ? "In" : "Out",
|
|
|
|
darwin_error_str(ret), ret);
|
|
|
|
|
|
|
|
return darwin_to_libusb (ret);
|
|
|
|
}
|
|
|
|
|
2014-12-27 15:39:17 +01:00
|
|
|
#if InterfaceVersion >= 550
|
|
|
|
static int submit_stream_transfer(struct usbi_transfer *itransfer) {
|
|
|
|
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
|
|
|
struct darwin_interface *cInterface;
|
|
|
|
uint8_t pipeRef;
|
|
|
|
IOReturn ret;
|
|
|
|
|
|
|
|
if (ep_to_pipeRef (transfer->dev_handle, transfer->endpoint, &pipeRef, NULL, &cInterface) != 0) {
|
|
|
|
usbi_err (TRANSFER_CTX (transfer), "endpoint not found on any open interface");
|
|
|
|
|
|
|
|
return LIBUSB_ERROR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2016-11-20 14:07:07 +01:00
|
|
|
itransfer->timeout_flags |= USBI_TRANSFER_OS_HANDLES_TIMEOUT;
|
2014-12-27 15:39:17 +01:00
|
|
|
|
|
|
|
if (IS_XFERIN(transfer))
|
|
|
|
ret = (*(cInterface->interface))->ReadStreamsPipeAsyncTO(cInterface->interface, pipeRef, itransfer->stream_id,
|
2019-05-01 11:47:48 +02:00
|
|
|
transfer->buffer, (UInt32)transfer->length, transfer->timeout,
|
|
|
|
transfer->timeout, darwin_async_io_callback, itransfer);
|
2014-12-27 15:39:17 +01:00
|
|
|
else
|
|
|
|
ret = (*(cInterface->interface))->WriteStreamsPipeAsyncTO(cInterface->interface, pipeRef, itransfer->stream_id,
|
2019-05-01 11:47:48 +02:00
|
|
|
transfer->buffer, (UInt32)transfer->length, transfer->timeout,
|
|
|
|
transfer->timeout, darwin_async_io_callback, itransfer);
|
2014-12-27 15:39:17 +01:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
usbi_err (TRANSFER_CTX (transfer), "bulk stream transfer failed (dir = %s): %s (code = 0x%08x)", IS_XFERIN(transfer) ? "In" : "Out",
|
|
|
|
darwin_error_str(ret), ret);
|
|
|
|
|
|
|
|
return darwin_to_libusb (ret);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-09-30 18:36:54 +13:00
|
|
|
static int submit_iso_transfer(struct usbi_transfer *itransfer) {
|
|
|
|
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
|
|
|
struct darwin_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
|
|
|
|
|
|
|
|
IOReturn kresult;
|
2014-12-27 15:39:17 +01:00
|
|
|
uint8_t direction, number, interval, pipeRef, transferType;
|
2013-09-30 18:36:54 +13:00
|
|
|
uint16_t maxPacketSize;
|
|
|
|
UInt64 frame;
|
|
|
|
AbsoluteTime atTime;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
struct darwin_interface *cInterface;
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
/* construct an array of IOUSBIsocFrames, reuse the old one if the sizes are the same */
|
|
|
|
if (tpriv->num_iso_packets != transfer->num_iso_packets) {
|
2013-09-30 18:36:54 +13:00
|
|
|
free(tpriv->isoc_framelist);
|
|
|
|
tpriv->isoc_framelist = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tpriv->isoc_framelist) {
|
|
|
|
tpriv->num_iso_packets = transfer->num_iso_packets;
|
2019-05-01 11:47:48 +02:00
|
|
|
tpriv->isoc_framelist = (IOUSBIsocFrame*) calloc ((size_t)transfer->num_iso_packets, sizeof(IOUSBIsocFrame));
|
2013-09-30 18:36:54 +13:00
|
|
|
if (!tpriv->isoc_framelist)
|
|
|
|
return LIBUSB_ERROR_NO_MEM;
|
|
|
|
}
|
|
|
|
|
2014-12-27 15:39:17 +01:00
|
|
|
/* copy the frame list from the libusb descriptor (the structures differ only is member order) */
|
2019-05-01 11:47:48 +02:00
|
|
|
for (i = 0 ; i < transfer->num_iso_packets ; i++) {
|
|
|
|
unsigned int length = transfer->iso_packet_desc[i].length;
|
|
|
|
assert(length <= UINT16_MAX);
|
|
|
|
tpriv->isoc_framelist[i].frReqCount = (UInt16)length;
|
|
|
|
}
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
/* determine the interface/endpoint to use */
|
2014-12-27 15:39:17 +01:00
|
|
|
if (ep_to_pipeRef (transfer->dev_handle, transfer->endpoint, &pipeRef, NULL, &cInterface) != 0) {
|
2013-09-30 18:36:54 +13:00
|
|
|
usbi_err (TRANSFER_CTX (transfer), "endpoint not found on any open interface");
|
|
|
|
|
|
|
|
return LIBUSB_ERROR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* determine the properties of this endpoint and the speed of the device */
|
|
|
|
(*(cInterface->interface))->GetPipeProperties (cInterface->interface, pipeRef, &direction, &number,
|
|
|
|
&transferType, &maxPacketSize, &interval);
|
|
|
|
|
|
|
|
/* Last but not least we need the bus frame number */
|
|
|
|
kresult = (*(cInterface->interface))->GetBusFrameNumber(cInterface->interface, &frame, &atTime);
|
2019-05-01 11:47:48 +02:00
|
|
|
if (kresult != kIOReturnSuccess) {
|
2013-09-30 18:36:54 +13:00
|
|
|
usbi_err (TRANSFER_CTX (transfer), "failed to get bus frame number: %d", kresult);
|
|
|
|
free(tpriv->isoc_framelist);
|
|
|
|
tpriv->isoc_framelist = NULL;
|
|
|
|
|
|
|
|
return darwin_to_libusb (kresult);
|
|
|
|
}
|
|
|
|
|
|
|
|
(*(cInterface->interface))->GetPipeProperties (cInterface->interface, pipeRef, &direction, &number,
|
|
|
|
&transferType, &maxPacketSize, &interval);
|
|
|
|
|
|
|
|
/* schedule for a frame a little in the future */
|
|
|
|
frame += 4;
|
|
|
|
|
|
|
|
if (cInterface->frames[transfer->endpoint] && frame < cInterface->frames[transfer->endpoint])
|
|
|
|
frame = cInterface->frames[transfer->endpoint];
|
|
|
|
|
|
|
|
/* submit the request */
|
|
|
|
if (IS_XFERIN(transfer))
|
|
|
|
kresult = (*(cInterface->interface))->ReadIsochPipeAsync(cInterface->interface, pipeRef, transfer->buffer, frame,
|
2019-05-01 11:47:48 +02:00
|
|
|
(UInt32)transfer->num_iso_packets, tpriv->isoc_framelist, darwin_async_io_callback,
|
2013-09-30 18:36:54 +13:00
|
|
|
itransfer);
|
|
|
|
else
|
|
|
|
kresult = (*(cInterface->interface))->WriteIsochPipeAsync(cInterface->interface, pipeRef, transfer->buffer, frame,
|
2019-05-01 11:47:48 +02:00
|
|
|
(UInt32)transfer->num_iso_packets, tpriv->isoc_framelist, darwin_async_io_callback,
|
2013-09-30 18:36:54 +13:00
|
|
|
itransfer);
|
|
|
|
|
|
|
|
if (LIBUSB_SPEED_FULL == transfer->dev_handle->dev->speed)
|
|
|
|
/* Full speed */
|
2019-05-01 11:47:48 +02:00
|
|
|
cInterface->frames[transfer->endpoint] = frame + (UInt32)transfer->num_iso_packets * (1U << (interval - 1));
|
2013-09-30 18:36:54 +13:00
|
|
|
else
|
|
|
|
/* High/super speed */
|
2019-05-01 11:47:48 +02:00
|
|
|
cInterface->frames[transfer->endpoint] = frame + (UInt32)transfer->num_iso_packets * (1U << (interval - 1)) / 8;
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
if (kresult != kIOReturnSuccess) {
|
|
|
|
usbi_err (TRANSFER_CTX (transfer), "isochronous transfer failed (dir: %s): %s", IS_XFERIN(transfer) ? "In" : "Out",
|
|
|
|
darwin_error_str(kresult));
|
|
|
|
free (tpriv->isoc_framelist);
|
|
|
|
tpriv->isoc_framelist = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return darwin_to_libusb (kresult);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int submit_control_transfer(struct usbi_transfer *itransfer) {
|
|
|
|
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
|
|
|
struct libusb_control_setup *setup = (struct libusb_control_setup *) transfer->buffer;
|
|
|
|
struct darwin_cached_device *dpriv = DARWIN_CACHED_DEVICE(transfer->dev_handle->dev);
|
|
|
|
struct darwin_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
|
|
|
|
|
|
|
|
IOReturn kresult;
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
memset(&tpriv->req, 0, sizeof(tpriv->req));
|
2013-09-30 18:36:54 +13:00
|
|
|
|
2016-11-20 14:07:07 +01:00
|
|
|
/* IOUSBDeviceInterface expects the request in cpu endianness */
|
2013-09-30 18:36:54 +13:00
|
|
|
tpriv->req.bmRequestType = setup->bmRequestType;
|
|
|
|
tpriv->req.bRequest = setup->bRequest;
|
|
|
|
/* these values should be in bus order from libusb_fill_control_setup */
|
|
|
|
tpriv->req.wValue = OSSwapLittleToHostInt16 (setup->wValue);
|
|
|
|
tpriv->req.wIndex = OSSwapLittleToHostInt16 (setup->wIndex);
|
|
|
|
tpriv->req.wLength = OSSwapLittleToHostInt16 (setup->wLength);
|
2014-12-27 15:39:17 +01:00
|
|
|
/* data is stored after the libusb control block */
|
2013-09-30 18:36:54 +13:00
|
|
|
tpriv->req.pData = transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
|
|
|
|
tpriv->req.completionTimeout = transfer->timeout;
|
|
|
|
tpriv->req.noDataTimeout = transfer->timeout;
|
|
|
|
|
2016-11-20 14:07:07 +01:00
|
|
|
itransfer->timeout_flags |= USBI_TRANSFER_OS_HANDLES_TIMEOUT;
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
/* all transfers in libusb-1.0 are async */
|
|
|
|
|
|
|
|
if (transfer->endpoint) {
|
|
|
|
struct darwin_interface *cInterface;
|
2014-12-27 15:39:17 +01:00
|
|
|
uint8_t pipeRef;
|
2013-09-30 18:36:54 +13:00
|
|
|
|
2014-12-27 15:39:17 +01:00
|
|
|
if (ep_to_pipeRef (transfer->dev_handle, transfer->endpoint, &pipeRef, NULL, &cInterface) != 0) {
|
2013-09-30 18:36:54 +13:00
|
|
|
usbi_err (TRANSFER_CTX (transfer), "endpoint not found on any open interface");
|
|
|
|
|
|
|
|
return LIBUSB_ERROR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
kresult = (*(cInterface->interface))->ControlRequestAsyncTO (cInterface->interface, pipeRef, &(tpriv->req), darwin_async_io_callback, itransfer);
|
|
|
|
} else
|
|
|
|
/* control request on endpoint 0 */
|
|
|
|
kresult = (*(dpriv->device))->DeviceRequestAsyncTO(dpriv->device, &(tpriv->req), darwin_async_io_callback, itransfer);
|
|
|
|
|
|
|
|
if (kresult != kIOReturnSuccess)
|
|
|
|
usbi_err (TRANSFER_CTX (transfer), "control request failed: %s", darwin_error_str(kresult));
|
|
|
|
|
|
|
|
return darwin_to_libusb (kresult);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int darwin_submit_transfer(struct usbi_transfer *itransfer) {
|
|
|
|
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
|
|
|
|
|
|
|
switch (transfer->type) {
|
|
|
|
case LIBUSB_TRANSFER_TYPE_CONTROL:
|
|
|
|
return submit_control_transfer(itransfer);
|
|
|
|
case LIBUSB_TRANSFER_TYPE_BULK:
|
|
|
|
case LIBUSB_TRANSFER_TYPE_INTERRUPT:
|
|
|
|
return submit_bulk_transfer(itransfer);
|
|
|
|
case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
|
|
|
|
return submit_iso_transfer(itransfer);
|
2014-12-27 15:39:17 +01:00
|
|
|
case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
|
|
|
|
#if InterfaceVersion >= 550
|
|
|
|
return submit_stream_transfer(itransfer);
|
|
|
|
#else
|
|
|
|
usbi_err (TRANSFER_CTX(transfer), "IOUSBFamily version does not support bulk stream transfers");
|
|
|
|
return LIBUSB_ERROR_NOT_SUPPORTED;
|
|
|
|
#endif
|
2013-09-30 18:36:54 +13:00
|
|
|
default:
|
|
|
|
usbi_err (TRANSFER_CTX(transfer), "unknown endpoint type %d", transfer->type);
|
|
|
|
return LIBUSB_ERROR_INVALID_PARAM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cancel_control_transfer(struct usbi_transfer *itransfer) {
|
|
|
|
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
|
|
|
struct darwin_cached_device *dpriv = DARWIN_CACHED_DEVICE(transfer->dev_handle->dev);
|
|
|
|
IOReturn kresult;
|
|
|
|
|
|
|
|
usbi_warn (ITRANSFER_CTX (itransfer), "aborting all transactions control pipe");
|
|
|
|
|
|
|
|
if (!dpriv->device)
|
|
|
|
return LIBUSB_ERROR_NO_DEVICE;
|
|
|
|
|
|
|
|
kresult = (*(dpriv->device))->USBDeviceAbortPipeZero (dpriv->device);
|
|
|
|
|
|
|
|
return darwin_to_libusb (kresult);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int darwin_abort_transfers (struct usbi_transfer *itransfer) {
|
|
|
|
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
|
|
|
struct darwin_cached_device *dpriv = DARWIN_CACHED_DEVICE(transfer->dev_handle->dev);
|
|
|
|
struct darwin_interface *cInterface;
|
|
|
|
uint8_t pipeRef, iface;
|
|
|
|
IOReturn kresult;
|
|
|
|
|
2014-12-27 15:39:17 +01:00
|
|
|
if (ep_to_pipeRef (transfer->dev_handle, transfer->endpoint, &pipeRef, &iface, &cInterface) != 0) {
|
2013-09-30 18:36:54 +13:00
|
|
|
usbi_err (TRANSFER_CTX (transfer), "endpoint not found on any open interface");
|
|
|
|
|
|
|
|
return LIBUSB_ERROR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dpriv->device)
|
|
|
|
return LIBUSB_ERROR_NO_DEVICE;
|
|
|
|
|
|
|
|
usbi_warn (ITRANSFER_CTX (itransfer), "aborting all transactions on interface %d pipe %d", iface, pipeRef);
|
|
|
|
|
|
|
|
/* abort transactions */
|
2014-12-27 15:39:17 +01:00
|
|
|
#if InterfaceVersion >= 550
|
|
|
|
if (LIBUSB_TRANSFER_TYPE_BULK_STREAM == transfer->type)
|
|
|
|
(*(cInterface->interface))->AbortStreamsPipe (cInterface->interface, pipeRef, itransfer->stream_id);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
(*(cInterface->interface))->AbortPipe (cInterface->interface, pipeRef);
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
usbi_dbg ("calling clear pipe stall to clear the data toggle bit");
|
|
|
|
|
|
|
|
/* newer versions of darwin support clearing additional bits on the device's endpoint */
|
|
|
|
kresult = (*(cInterface->interface))->ClearPipeStallBothEnds(cInterface->interface, pipeRef);
|
|
|
|
|
|
|
|
return darwin_to_libusb (kresult);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int darwin_cancel_transfer(struct usbi_transfer *itransfer) {
|
|
|
|
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
|
|
|
|
|
|
|
switch (transfer->type) {
|
|
|
|
case LIBUSB_TRANSFER_TYPE_CONTROL:
|
|
|
|
return cancel_control_transfer(itransfer);
|
|
|
|
case LIBUSB_TRANSFER_TYPE_BULK:
|
|
|
|
case LIBUSB_TRANSFER_TYPE_INTERRUPT:
|
|
|
|
case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
|
|
|
|
return darwin_abort_transfers (itransfer);
|
|
|
|
default:
|
|
|
|
usbi_err (TRANSFER_CTX(transfer), "unknown endpoint type %d", transfer->type);
|
|
|
|
return LIBUSB_ERROR_INVALID_PARAM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void darwin_clear_transfer_priv (struct usbi_transfer *itransfer) {
|
|
|
|
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
|
|
|
struct darwin_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
|
|
|
|
|
|
|
|
if (transfer->type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS && tpriv->isoc_framelist) {
|
|
|
|
free (tpriv->isoc_framelist);
|
|
|
|
tpriv->isoc_framelist = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void darwin_async_io_callback (void *refcon, IOReturn result, void *arg0) {
|
|
|
|
struct usbi_transfer *itransfer = (struct usbi_transfer *)refcon;
|
|
|
|
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
2016-08-25 18:27:40 -07:00
|
|
|
struct darwin_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
usbi_dbg ("an async io operation has completed");
|
|
|
|
|
|
|
|
/* if requested write a zero packet */
|
|
|
|
if (kIOReturnSuccess == result && IS_XFEROUT(transfer) && transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) {
|
|
|
|
struct darwin_interface *cInterface;
|
2014-12-27 15:39:17 +01:00
|
|
|
uint8_t pipeRef;
|
2013-09-30 18:36:54 +13:00
|
|
|
|
2014-12-27 15:39:17 +01:00
|
|
|
(void) ep_to_pipeRef (transfer->dev_handle, transfer->endpoint, &pipeRef, NULL, &cInterface);
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
(*(cInterface->interface))->WritePipe (cInterface->interface, pipeRef, transfer->buffer, 0);
|
|
|
|
}
|
|
|
|
|
2016-08-25 18:27:40 -07:00
|
|
|
tpriv->result = result;
|
|
|
|
tpriv->size = (UInt32) (uintptr_t) arg0;
|
|
|
|
|
|
|
|
/* signal the core that this transfer is complete */
|
|
|
|
usbi_signal_transfer_completion(itransfer);
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
static enum libusb_transfer_status darwin_transfer_status (struct usbi_transfer *itransfer, IOReturn result) {
|
2016-11-20 14:07:07 +01:00
|
|
|
if (itransfer->timeout_flags & USBI_TRANSFER_TIMED_OUT)
|
2013-09-30 18:36:54 +13:00
|
|
|
result = kIOUSBTransactionTimeout;
|
|
|
|
|
|
|
|
switch (result) {
|
|
|
|
case kIOReturnUnderrun:
|
|
|
|
case kIOReturnSuccess:
|
|
|
|
return LIBUSB_TRANSFER_COMPLETED;
|
|
|
|
case kIOReturnAborted:
|
|
|
|
return LIBUSB_TRANSFER_CANCELLED;
|
|
|
|
case kIOUSBPipeStalled:
|
|
|
|
usbi_dbg ("transfer error: pipe is stalled");
|
|
|
|
return LIBUSB_TRANSFER_STALL;
|
|
|
|
case kIOReturnOverrun:
|
|
|
|
usbi_warn (ITRANSFER_CTX (itransfer), "transfer error: data overrun");
|
|
|
|
return LIBUSB_TRANSFER_OVERFLOW;
|
|
|
|
case kIOUSBTransactionTimeout:
|
|
|
|
usbi_warn (ITRANSFER_CTX (itransfer), "transfer error: timed out");
|
2016-11-20 14:07:07 +01:00
|
|
|
itransfer->timeout_flags |= USBI_TRANSFER_TIMED_OUT;
|
2013-09-30 18:36:54 +13:00
|
|
|
return LIBUSB_TRANSFER_TIMED_OUT;
|
|
|
|
default:
|
|
|
|
usbi_warn (ITRANSFER_CTX (itransfer), "transfer error: %s (value = 0x%08x)", darwin_error_str (result), result);
|
|
|
|
return LIBUSB_TRANSFER_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-25 18:27:40 -07:00
|
|
|
static int darwin_handle_transfer_completion (struct usbi_transfer *itransfer) {
|
2013-09-30 18:36:54 +13:00
|
|
|
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
|
|
|
struct darwin_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
|
2019-05-01 11:47:48 +02:00
|
|
|
bool isIsoc = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS == transfer->type;
|
|
|
|
bool isBulk = LIBUSB_TRANSFER_TYPE_BULK == transfer->type;
|
|
|
|
bool isControl = LIBUSB_TRANSFER_TYPE_CONTROL == transfer->type;
|
|
|
|
bool isInterrupt = LIBUSB_TRANSFER_TYPE_INTERRUPT == transfer->type;
|
2013-09-30 18:36:54 +13:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!isIsoc && !isBulk && !isControl && !isInterrupt) {
|
|
|
|
usbi_err (TRANSFER_CTX(transfer), "unknown endpoint type %d", transfer->type);
|
2016-08-25 18:27:40 -07:00
|
|
|
return LIBUSB_ERROR_INVALID_PARAM;
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
usbi_dbg ("handling %s completion with kernel status %d",
|
2016-08-25 18:27:40 -07:00
|
|
|
isControl ? "control" : isBulk ? "bulk" : isIsoc ? "isoc" : "interrupt", tpriv->result);
|
2013-09-30 18:36:54 +13:00
|
|
|
|
2016-08-25 18:27:40 -07:00
|
|
|
if (kIOReturnSuccess == tpriv->result || kIOReturnUnderrun == tpriv->result) {
|
2013-09-30 18:36:54 +13:00
|
|
|
if (isIsoc && tpriv->isoc_framelist) {
|
|
|
|
/* copy isochronous results back */
|
|
|
|
|
|
|
|
for (i = 0; i < transfer->num_iso_packets ; i++) {
|
|
|
|
struct libusb_iso_packet_descriptor *lib_desc = &transfer->iso_packet_desc[i];
|
2019-05-01 11:47:48 +02:00
|
|
|
lib_desc->status = darwin_transfer_status (itransfer, tpriv->isoc_framelist[i].frStatus);
|
2013-09-30 18:36:54 +13:00
|
|
|
lib_desc->actual_length = tpriv->isoc_framelist[i].frActCount;
|
|
|
|
}
|
|
|
|
} else if (!isIsoc)
|
2016-08-25 18:27:40 -07:00
|
|
|
itransfer->transferred += tpriv->size;
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/* it is ok to handle cancelled transfers without calling usbi_handle_transfer_cancellation (we catch timeout transfers) */
|
2016-08-25 18:27:40 -07:00
|
|
|
return usbi_handle_transfer_completion (itransfer, darwin_transfer_status (itransfer, tpriv->result));
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
static int darwin_clock_gettime(int clk_id, struct timespec *tp) {
|
2019-05-01 11:47:48 +02:00
|
|
|
#if !OSX_USE_CLOCK_GETTIME
|
2013-09-30 18:36:54 +13:00
|
|
|
mach_timespec_t sys_time;
|
|
|
|
clock_serv_t clock_ref;
|
|
|
|
|
|
|
|
switch (clk_id) {
|
|
|
|
case USBI_CLOCK_REALTIME:
|
|
|
|
/* CLOCK_REALTIME represents time since the epoch */
|
|
|
|
clock_ref = clock_realtime;
|
|
|
|
break;
|
|
|
|
case USBI_CLOCK_MONOTONIC:
|
|
|
|
/* use system boot time as reference for the monotonic clock */
|
|
|
|
clock_ref = clock_monotonic;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return LIBUSB_ERROR_INVALID_PARAM;
|
|
|
|
}
|
|
|
|
|
|
|
|
clock_get_time (clock_ref, &sys_time);
|
|
|
|
|
|
|
|
tp->tv_sec = sys_time.tv_sec;
|
|
|
|
tp->tv_nsec = sys_time.tv_nsec;
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
return LIBUSB_SUCCESS;
|
|
|
|
#else
|
|
|
|
switch (clk_id) {
|
|
|
|
case USBI_CLOCK_MONOTONIC:
|
|
|
|
return clock_gettime(CLOCK_MONOTONIC, tp);
|
|
|
|
case USBI_CLOCK_REALTIME:
|
|
|
|
return clock_gettime(CLOCK_REALTIME, tp);
|
|
|
|
default:
|
|
|
|
return LIBUSB_ERROR_INVALID_PARAM;
|
|
|
|
}
|
|
|
|
#endif
|
2013-09-30 18:36:54 +13:00
|
|
|
}
|
|
|
|
|
2014-12-27 15:39:17 +01:00
|
|
|
#if InterfaceVersion >= 550
|
|
|
|
static int darwin_alloc_streams (struct libusb_device_handle *dev_handle, uint32_t num_streams, unsigned char *endpoints,
|
|
|
|
int num_endpoints) {
|
|
|
|
struct darwin_interface *cInterface;
|
|
|
|
UInt32 supportsStreams;
|
|
|
|
uint8_t pipeRef;
|
|
|
|
int rc, i;
|
|
|
|
|
|
|
|
/* find the mimimum number of supported streams on the endpoint list */
|
|
|
|
for (i = 0 ; i < num_endpoints ; ++i) {
|
|
|
|
if (0 != (rc = ep_to_pipeRef (dev_handle, endpoints[i], &pipeRef, NULL, &cInterface))) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*(cInterface->interface))->SupportsStreams (cInterface->interface, pipeRef, &supportsStreams);
|
|
|
|
if (num_streams > supportsStreams)
|
|
|
|
num_streams = supportsStreams;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* it is an error if any endpoint in endpoints does not support streams */
|
|
|
|
if (0 == num_streams)
|
|
|
|
return LIBUSB_ERROR_INVALID_PARAM;
|
|
|
|
|
|
|
|
/* create the streams */
|
|
|
|
for (i = 0 ; i < num_endpoints ; ++i) {
|
|
|
|
(void) ep_to_pipeRef (dev_handle, endpoints[i], &pipeRef, NULL, &cInterface);
|
|
|
|
|
|
|
|
rc = (*(cInterface->interface))->CreateStreams (cInterface->interface, pipeRef, num_streams);
|
|
|
|
if (kIOReturnSuccess != rc)
|
|
|
|
return darwin_to_libusb(rc);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
assert(num_streams <= INT_MAX);
|
|
|
|
return (int)num_streams;
|
2014-12-27 15:39:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int darwin_free_streams (struct libusb_device_handle *dev_handle, unsigned char *endpoints, int num_endpoints) {
|
|
|
|
struct darwin_interface *cInterface;
|
|
|
|
UInt32 supportsStreams;
|
|
|
|
uint8_t pipeRef;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
for (int i = 0 ; i < num_endpoints ; ++i) {
|
|
|
|
if (0 != (rc = ep_to_pipeRef (dev_handle, endpoints[i], &pipeRef, NULL, &cInterface)))
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
(*(cInterface->interface))->SupportsStreams (cInterface->interface, pipeRef, &supportsStreams);
|
|
|
|
if (0 == supportsStreams)
|
|
|
|
return LIBUSB_ERROR_INVALID_PARAM;
|
|
|
|
|
|
|
|
rc = (*(cInterface->interface))->CreateStreams (cInterface->interface, pipeRef, 0);
|
|
|
|
if (kIOReturnSuccess != rc)
|
|
|
|
return darwin_to_libusb(rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
return LIBUSB_SUCCESS;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-05-01 11:47:48 +02:00
|
|
|
const struct usbi_os_backend usbi_backend = {
|
2013-09-30 18:36:54 +13:00
|
|
|
.name = "Darwin",
|
|
|
|
.caps = 0,
|
|
|
|
.init = darwin_init,
|
|
|
|
.exit = darwin_exit,
|
|
|
|
.get_device_list = NULL, /* not needed */
|
|
|
|
.get_device_descriptor = darwin_get_device_descriptor,
|
|
|
|
.get_active_config_descriptor = darwin_get_active_config_descriptor,
|
|
|
|
.get_config_descriptor = darwin_get_config_descriptor,
|
2014-12-27 15:39:17 +01:00
|
|
|
.hotplug_poll = darwin_hotplug_poll,
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
.open = darwin_open,
|
|
|
|
.close = darwin_close,
|
|
|
|
.get_configuration = darwin_get_configuration,
|
|
|
|
.set_configuration = darwin_set_configuration,
|
|
|
|
.claim_interface = darwin_claim_interface,
|
|
|
|
.release_interface = darwin_release_interface,
|
|
|
|
|
|
|
|
.set_interface_altsetting = darwin_set_interface_altsetting,
|
|
|
|
.clear_halt = darwin_clear_halt,
|
|
|
|
.reset_device = darwin_reset_device,
|
|
|
|
|
2014-12-27 15:39:17 +01:00
|
|
|
#if InterfaceVersion >= 550
|
|
|
|
.alloc_streams = darwin_alloc_streams,
|
|
|
|
.free_streams = darwin_free_streams,
|
|
|
|
#endif
|
|
|
|
|
2013-09-30 18:36:54 +13:00
|
|
|
.kernel_driver_active = darwin_kernel_driver_active,
|
|
|
|
.detach_kernel_driver = darwin_detach_kernel_driver,
|
|
|
|
.attach_kernel_driver = darwin_attach_kernel_driver,
|
|
|
|
|
|
|
|
.destroy_device = darwin_destroy_device,
|
|
|
|
|
|
|
|
.submit_transfer = darwin_submit_transfer,
|
|
|
|
.cancel_transfer = darwin_cancel_transfer,
|
|
|
|
.clear_transfer_priv = darwin_clear_transfer_priv,
|
|
|
|
|
2016-08-25 18:27:40 -07:00
|
|
|
.handle_transfer_completion = darwin_handle_transfer_completion,
|
2013-09-30 18:36:54 +13:00
|
|
|
|
|
|
|
.clock_gettime = darwin_clock_gettime,
|
|
|
|
|
|
|
|
.device_priv_size = sizeof(struct darwin_device_priv),
|
|
|
|
.device_handle_priv_size = sizeof(struct darwin_device_handle_priv),
|
|
|
|
.transfer_priv_size = sizeof(struct darwin_transfer_priv),
|
|
|
|
};
|