static int darwin_reset_device(struct libusb_device_handle *dev_handle);
static void darwin_async_io_callback (void *refcon, IOReturn result, void *arg0);
-static int darwin_scan_devices(struct libusb_context *ctx);
-static int process_new_device (struct libusb_context *ctx, io_service_t service);
+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);
#if defined(ENABLE_LOGGING)
-static const char *darwin_error_str (int result) {
+static const char *darwin_error_str (IOReturn result) {
static char string_buffer[50];
switch (result) {
case kIOReturnSuccess:
}
#endif
-static int darwin_to_libusb (int result) {
+static enum libusb_error darwin_to_libusb (IOReturn result) {
switch (result) {
case kIOReturnUnderrun:
case kIOReturnSuccess:
*interface_out = cInterface;
usbi_dbg ("pipe %d on interface %d matches", *pipep, iface);
- return 0;
+ return LIBUSB_SUCCESS;
}
}
}
return LIBUSB_ERROR_NOT_FOUND;
}
-static int usb_setup_device_iterator (io_iterator_t *deviceIterator, UInt32 location) {
+static IOReturn usb_setup_device_iterator (io_iterator_t *deviceIterator, UInt32 location) {
CFMutableDictionaryRef matchingDict = IOServiceMatching(darwin_device_class);
if (!matchingDict)
}
/* Returns 1 on success, 0 on failure. */
-static int get_ioregistry_value_number (io_service_t service, CFStringRef property, CFNumberType type, void *p) {
+static bool get_ioregistry_value_number (io_service_t service, CFStringRef property, CFNumberType type, void *p) {
CFTypeRef cfNumber = IORegistryEntryCreateCFProperty (service, property, kCFAllocatorDefault, 0);
- int ret = 0;
+ Boolean success = 0;
if (cfNumber) {
if (CFGetTypeID(cfNumber) == CFNumberGetTypeID()) {
- ret = CFNumberGetValue(cfNumber, type, p);
+ success = CFNumberGetValue(cfNumber, type, p);
}
CFRelease (cfNumber);
}
- return ret;
+ return (success != 0);
}
-static int get_ioregistry_value_data (io_service_t service, CFStringRef property, ssize_t size, void *p) {
+/* Returns 1 on success, 0 on failure. */
+static bool get_ioregistry_value_data (io_service_t service, CFStringRef property, ssize_t size, void *p) {
CFTypeRef cfData = IORegistryEntryCreateCFProperty (service, property, kCFAllocatorDefault, 0);
- int ret = 0;
+ bool success = false;
if (cfData) {
if (CFGetTypeID (cfData) == CFDataGetTypeID ()) {
}
CFDataGetBytes (cfData, CFRangeMake(0, size), p);
- ret = 1;
+ success = true;
}
CFRelease (cfData);
}
- return ret;
+ return success;
}
static usb_device_t **darwin_device_from_service (io_service_t service)
{
io_cf_plugin_ref_t *plugInInterface = NULL;
usb_device_t **device;
- kern_return_t result;
+ IOReturn kresult;
SInt32 score;
- result = IOCreatePlugInInterfaceForService(service, kIOUSBDeviceUserClientTypeID,
- kIOCFPlugInInterfaceID, &plugInInterface,
- &score);
+ kresult = IOCreatePlugInInterfaceForService(service, kIOUSBDeviceUserClientTypeID,
+ kIOCFPlugInInterfaceID, &plugInInterface,
+ &score);
- if (kIOReturnSuccess != result || !plugInInterface) {
- usbi_dbg ("could not set up plugin for service: %s", darwin_error_str (result));
+ if (kIOReturnSuccess != kresult || !plugInInterface) {
+ usbi_dbg ("could not set up plugin for service: %s", darwin_error_str (kresult));
return NULL;
}
*host_endian = 0;
- return 0;
+ return LIBUSB_SUCCESS;
}
static int get_configuration_index (struct libusb_device *dev, int config_value) {
}
/* check whether the os has configured the device */
-static int darwin_check_configuration (struct libusb_context *ctx, struct darwin_cached_device *dev) {
+static enum libusb_error darwin_check_configuration (struct libusb_context *ctx, struct darwin_cached_device *dev) {
usb_device_t **darwin_device = dev->device;
IOUSBConfigurationDescriptorPtr configDesc;
IOUSBFindInterfaceRequest request;
- kern_return_t kresult;
+ IOReturn kresult;
io_iterator_t interface_iterator;
io_service_t firstInterface;
if (0x05ac == dev->dev_descriptor.idVendor && 0x8005 == dev->dev_descriptor.idProduct) {
usbi_dbg ("ignoring configuration on root hub simulation");
dev->active_config = 0;
- return 0;
+ return LIBUSB_SUCCESS;
}
/* find the first configuration */
request.bAlternateSetting = kIOUSBFindInterfaceDontCare;
kresult = (*(darwin_device))->CreateInterfaceIterator(darwin_device, &request, &interface_iterator);
- if (kresult)
+ if (kresult != kIOReturnSuccess)
return darwin_to_libusb (kresult);
/* iterate once */
usbi_dbg ("active config: %u, first config: %u", dev->active_config, dev->first_config);
- return 0;
+ return LIBUSB_SUCCESS;
}
-static int darwin_request_descriptor (usb_device_t **device, UInt8 desc, UInt8 desc_index, void *buffer, size_t buffer_size) {
+static IOReturn darwin_request_descriptor (usb_device_t **device, UInt8 desc, UInt8 desc_index, void *buffer, size_t buffer_size) {
IOUSBDevRequestTO req;
memset (buffer, 0, buffer_size);
return (*device)->DeviceRequestTO (device, &req);
}
-static int darwin_cache_device_descriptor (struct libusb_context *ctx, struct darwin_cached_device *dev) {
+static enum libusb_error darwin_cache_device_descriptor (struct libusb_context *ctx, struct darwin_cached_device *dev) {
usb_device_t **device = dev->device;
int retries = 1, delay = 30000;
int unsuspended = 0, try_unsuspend = 1, try_reconfigure = 1;
int is_open = 0;
- int ret = 0, ret2;
+ IOReturn ret = 0, ret2;
UInt8 bDeviceClass;
UInt16 idProduct, idVendor;
return LIBUSB_SUCCESS;
}
-static int get_device_port (io_service_t service, UInt8 *port) {
- kern_return_t result;
+/* Returns 1 on success, 0 on failure. */
+static bool get_device_port (io_service_t service, UInt8 *port) {
+ IOReturn kresult;
io_service_t parent;
- int ret = 0;
+ bool ret = false;
if (get_ioregistry_value_number (service, CFSTR("PortNum"), kCFNumberSInt8Type, port)) {
- return 1;
+ return true;
}
- result = IORegistryEntryGetParentEntry (service, kIOServicePlane, &parent);
- if (kIOReturnSuccess == result) {
+ kresult = IORegistryEntryGetParentEntry (service, kIOServicePlane, &parent);
+ if (kIOReturnSuccess == kresult) {
ret = get_ioregistry_value_data (parent, CFSTR("port"), 1, port);
IOObjectRelease (parent);
}
return ret;
}
-static int get_device_parent_sessionID(io_service_t service, UInt64 *parent_sessionID) {
- kern_return_t result;
+/* 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((result = IORegistryEntryGetParentEntry (parent, kIOUSBPlane, &parent)) == kIOReturnSuccess) {
+ while((kresult = IORegistryEntryGetParentEntry (parent, kIOUSBPlane, &parent)) == kIOReturnSuccess) {
if (get_ioregistry_value_number (parent, CFSTR("sessionID"), kCFNumberSInt64Type, parent_sessionID)) {
/* Success */
- return 1;
+ return true;
}
}
/* We ran out of parents */
- return 0;
+ return false;
}
-static int darwin_get_cached_device(struct libusb_context *ctx, io_service_t service,
- struct darwin_cached_device **cached_out) {
+static enum libusb_error darwin_get_cached_device(struct libusb_context *ctx, io_service_t service,
+ struct darwin_cached_device **cached_out) {
struct darwin_cached_device *new_device;
UInt64 sessionID = 0, parent_sessionID = 0;
- int ret = LIBUSB_SUCCESS;
+ enum libusb_error ret = LIBUSB_SUCCESS;
usb_device_t **device;
UInt8 port = 0;
return ret;
}
-static int process_new_device (struct libusb_context *ctx, io_service_t service) {
+static enum libusb_error process_new_device (struct libusb_context *ctx, io_service_t service) {
struct darwin_device_priv *priv;
struct libusb_device *dev = NULL;
struct darwin_cached_device *cached_device;
UInt8 devSpeed;
- int ret = 0;
+ enum libusb_error ret = LIBUSB_SUCCESS;
do {
ret = darwin_get_cached_device (ctx, service, &cached_device);
return ret;
}
-static int darwin_scan_devices(struct libusb_context *ctx) {
+static enum libusb_error darwin_scan_devices(struct libusb_context *ctx) {
io_iterator_t deviceIterator;
io_service_t service;
- kern_return_t kresult;
+ IOReturn kresult;
kresult = usb_setup_device_iterator (&deviceIterator, 0);
if (kresult != kIOReturnSuccess)
IOObjectRelease(deviceIterator);
- return 0;
+ return LIBUSB_SUCCESS;
}
static int darwin_open (struct libusb_device_handle *dev_handle) {
if (priv->is_open) {
/* close the device */
kresult = (*(dpriv->device))->USBDeviceClose(dpriv->device);
- if (kresult) {
+ if (kresult != kIOReturnSuccess) {
/* 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));
*config = (int) dpriv->active_config;
- return 0;
+ return LIBUSB_SUCCESS;
}
-static int darwin_set_configuration(struct libusb_device_handle *dev_handle, int config) {
+static enum libusb_error darwin_set_configuration(struct libusb_device_handle *dev_handle, int config) {
struct darwin_cached_device *dpriv = DARWIN_CACHED_DEVICE(dev_handle->dev);
IOReturn kresult;
int i;
dpriv->active_config = config;
- return 0;
+ return LIBUSB_SUCCESS;
}
-static int darwin_get_interface (usb_device_t **darwin_device, uint8_t ifc, io_service_t *usbInterfacep) {
+static IOReturn darwin_get_interface (usb_device_t **darwin_device, int ifc, io_service_t *usbInterfacep) {
IOUSBFindInterfaceRequest request;
- kern_return_t kresult;
+ IOReturn kresult;
io_iterator_t interface_iterator;
UInt8 bInterfaceNumber;
- int ret;
+ bool ret;
*usbInterfacep = IO_OBJECT_NULL;
request.bAlternateSetting = kIOUSBFindInterfaceDontCare;
kresult = (*(darwin_device))->CreateInterfaceIterator(darwin_device, &request, &interface_iterator);
- if (kresult)
+ if (kresult != kIOReturnSuccess)
return kresult;
while ((*usbInterfacep = IOIteratorNext(interface_iterator))) {
/* done with the interface iterator */
IOObjectRelease(interface_iterator);
- return 0;
+ return kIOReturnSuccess;
}
-static int get_endpoints (struct libusb_device_handle *dev_handle, int iface) {
+static enum libusb_error get_endpoints (struct libusb_device_handle *dev_handle, int iface) {
struct darwin_device_handle_priv *priv = (struct darwin_device_handle_priv *)dev_handle->os_priv;
/* current interface */
struct darwin_interface *cInterface = &priv->interfaces[iface];
- kern_return_t kresult;
+ IOReturn kresult;
UInt8 numep, direction, number;
UInt8 dont_care1, dont_care3;
/* retrieve the total number of endpoints on this interface */
kresult = (*(cInterface->interface))->GetNumEndpoints(cInterface->interface, &numep);
- if (kresult) {
+ if (kresult != kIOReturnSuccess) {
usbi_err (HANDLE_CTX (dev_handle), "can't get number of endpoints for interface: %s", darwin_error_str(kresult));
return darwin_to_libusb (kresult);
}
UInt8 alt_setting;
kresult = (*(cInterface->interface))->GetAlternateSetting (cInterface->interface, &alt_setting);
- if (kresult) {
+ if (kresult != kIOReturnSuccess) {
usbi_err (HANDLE_CTX (dev_handle), "can't get alternate setting for interface");
return darwin_to_libusb (kresult);
}
cInterface->num_endpoints = numep;
- return 0;
+ return LIBUSB_SUCCESS;
}
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;
- IOReturn kresult;
+ IOReturn kresult;
+ enum libusb_error ret;
IOCFPlugInInterface **plugInInterface = NULL;
SInt32 score;
usbi_info (HANDLE_CTX (dev_handle), "no interface found; setting configuration: %d", dpriv->first_config);
/* set the configuration */
- kresult = darwin_set_configuration (dev_handle, dpriv->first_config);
- if (kresult != LIBUSB_SUCCESS) {
+ ret = darwin_set_configuration (dev_handle, dpriv->first_config);
+ if (ret != LIBUSB_SUCCESS) {
usbi_err (HANDLE_CTX (dev_handle), "could not set configuration");
- return kresult;
+ return ret;
}
kresult = darwin_get_interface (dpriv->device, iface, &usbInterface);
- if (kresult) {
+ if (kresult != kIOReturnSuccess) {
usbi_err (HANDLE_CTX (dev_handle), "darwin_get_interface: %s", darwin_error_str(kresult));
return darwin_to_libusb (kresult);
}
/* ignore release error */
(void)IOObjectRelease (usbInterface);
- if (kresult) {
+ if (kresult != kIOReturnSuccess) {
usbi_err (HANDLE_CTX (dev_handle), "IOCreatePlugInInterfaceForService: %s", darwin_error_str(kresult));
return darwin_to_libusb (kresult);
}
/* We no longer need the intermediate plug-in */
/* Use release instead of IODestroyPlugInInterface to avoid stopping IOServices associated with this device */
(*plugInInterface)->Release (plugInInterface);
- if (kresult || !cInterface->interface) {
+ if (kresult != kIOReturnSuccess || !cInterface->interface) {
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);
- if (kresult) {
+ if (kresult != kIOReturnSuccess) {
usbi_err (HANDLE_CTX (dev_handle), "USBInterfaceOpen: %s", darwin_error_str(kresult));
return darwin_to_libusb (kresult);
}
/* update list of endpoints */
- kresult = get_endpoints (dev_handle, iface);
- if (kresult) {
+ ret = get_endpoints (dev_handle, iface);
+ if (ret) {
/* this should not happen */
darwin_release_interface (dev_handle, iface);
usbi_err (HANDLE_CTX (dev_handle), "could not build endpoint table");
- return kresult;
+ return ret;
}
cInterface->cfSource = NULL;
usbi_dbg ("interface opened");
- return 0;
+ return LIBUSB_SUCCESS;
}
static int darwin_release_interface(struct libusb_device_handle *dev_handle, int iface) {
}
kresult = (*(cInterface->interface))->USBInterfaceClose(cInterface->interface);
- if (kresult)
+ if (kresult != kIOReturnSuccess)
usbi_warn (HANDLE_CTX (dev_handle), "USBInterfaceClose: %s", darwin_error_str(kresult));
kresult = (*(cInterface->interface))->Release(cInterface->interface);
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;
+ enum libusb_error ret;
/* current interface */
struct darwin_interface *cInterface = &priv->interfaces[iface];
darwin_reset_device (dev_handle);
/* update list of endpoints */
- kresult = get_endpoints (dev_handle, iface);
- if (kresult) {
+ ret = get_endpoints (dev_handle, iface);
+ if (ret) {
/* this should not happen */
darwin_release_interface (dev_handle, iface);
usbi_err (HANDLE_CTX (dev_handle), "could not build endpoint table");
- return kresult;
+ return ret;
}
return darwin_to_libusb (kresult);
/* newer versions of darwin support clearing additional bits on the device's endpoint */
kresult = (*(cInterface->interface))->ClearPipeStallBothEnds(cInterface->interface, pipeRef);
- if (kresult)
+ if (kresult != kIOReturnSuccess)
usbi_warn (HANDLE_CTX (dev_handle), "ClearPipeStall: %s", darwin_error_str (kresult));
return darwin_to_libusb (kresult);
/* from macOS 10.11 ResetDevice no longer does anything so just use USBDeviceReEnumerate */
kresult = (*(dpriv->device))->USBDeviceReEnumerate (dpriv->device, 0);
- if (kresult) {
+ if (kresult != kIOReturnSuccess) {
usbi_err (HANDLE_CTX (dev_handle), "USBDeviceReEnumerate: %s", darwin_error_str (kresult));
return darwin_to_libusb (kresult);
}
IOReturn kresult;
kresult = darwin_get_interface (dpriv->device, interface, &usbInterface);
- if (kresult) {
+ if (kresult != kIOReturnSuccess) {
usbi_err (HANDLE_CTX (dev_handle), "darwin_get_interface: %s", darwin_error_str(kresult));
return darwin_to_libusb (kresult);
/* Last but not least we need the bus frame number */
kresult = (*(cInterface->interface))->GetBusFrameNumber(cInterface->interface, &frame, &atTime);
- if (kresult) {
+ if (kresult != kIOReturnSuccess) {
usbi_err (TRANSFER_CTX (transfer), "failed to get bus frame number: %d", kresult);
free(tpriv->isoc_framelist);
tpriv->isoc_framelist = NULL;
usbi_signal_transfer_completion(itransfer);
}
-static int darwin_transfer_status (struct usbi_transfer *itransfer, kern_return_t result) {
+static enum libusb_transfer_status darwin_transfer_status (struct usbi_transfer *itransfer, IOReturn result) {
if (itransfer->timeout_flags & USBI_TRANSFER_TIMED_OUT)
result = kIOUSBTransactionTimeout;
static int darwin_handle_transfer_completion (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);
- int isIsoc = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS == transfer->type;
- int isBulk = LIBUSB_TRANSFER_TYPE_BULK == transfer->type;
- int isControl = LIBUSB_TRANSFER_TYPE_CONTROL == transfer->type;
- int isInterrupt = LIBUSB_TRANSFER_TYPE_INTERRUPT == transfer->type;
+ 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;
int i;
if (!isIsoc && !isBulk && !isControl && !isInterrupt) {
for (i = 0; i < transfer->num_iso_packets ; i++) {
struct libusb_iso_packet_descriptor *lib_desc = &transfer->iso_packet_desc[i];
- lib_desc->status = darwin_to_libusb (tpriv->isoc_framelist[i].frStatus);
+ lib_desc->status = darwin_transfer_status (itransfer, tpriv->isoc_framelist[i].frStatus);
lib_desc->actual_length = tpriv->isoc_framelist[i].frActCount;
}
} else if (!isIsoc)
tp->tv_sec = sys_time.tv_sec;
tp->tv_nsec = sys_time.tv_nsec;
- return 0;
+ return LIBUSB_SUCCESS;
#else
switch (clk_id) {
case USBI_CLOCK_MONOTONIC: