diff options
author | Nathan Hjelm <hjelmn@me.com> | 2012-11-29 16:19:11 -0700 |
---|---|---|
committer | Hans de Goede <hdegoede@redhat.com> | 2013-05-15 20:43:35 +0200 |
commit | d5c9542cee58583a32a9d2cc5dd1b069793c549e (patch) | |
tree | 2f6f676c609d5cd45ec4497efe3df3dddf5b0b35 /libusb/os | |
parent | 86031e26ad8c1516ded02a06834312951a7a0492 (diff) | |
download | libusb-d5c9542cee58583a32a9d2cc5dd1b069793c549e.tar.gz |
darwin: allow control messages on devices we can not exclusively open
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Diffstat (limited to 'libusb/os')
-rw-r--r-- | libusb/os/darwin_usb.c | 70 |
1 files changed, 23 insertions, 47 deletions
diff --git a/libusb/os/darwin_usb.c b/libusb/os/darwin_usb.c index 7cd1bea..6a81b1c 100644 --- a/libusb/os/darwin_usb.c +++ b/libusb/os/darwin_usb.c @@ -885,56 +885,40 @@ static int darwin_scan_devices(struct libusb_context *ctx) { 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_device_priv *dpriv = (struct darwin_device_priv *)dev_handle->dev->os_priv; - usb_device_t **darwin_device; IOReturn kresult; if (0 == dpriv->open_count) { - kresult = darwin_get_device (dpriv->location, &darwin_device); - if (kresult) { - usbi_err (HANDLE_CTX (dev_handle), "could not find device: %s", darwin_error_str (kresult)); - return darwin_to_libusb (kresult); - } - - dpriv->device = darwin_device; - /* 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)); - switch (kresult) { - case kIOReturnExclusiveAccess: - /* it is possible to perform some actions on a device that is not open so do not return an error */ - priv->is_open = 0; - - break; - default: - (*(dpriv->device))->Release (dpriv->device); - dpriv->device = NULL; + 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 */ + priv->is_open = 0; } else { - /* 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)); + priv->is_open = 1; + } - (*(dpriv->device))->USBDeviceClose (dpriv->device); - (*(dpriv->device))->Release (dpriv->device); + /* 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)); - dpriv->device = NULL; - return darwin_to_libusb (kresult); + if (priv->is_open) { + (*(dpriv->device))->USBDeviceClose (dpriv->device); } - priv->is_open = 1; - - CFRetain (libusb_darwin_acfl); + priv->is_open = 0; - /* add the cfSource to the aync run loop */ - CFRunLoopAddSource(libusb_darwin_acfl, priv->cfSource, kCFRunLoopCommonModes); + return darwin_to_libusb (kresult); } + CFRetain (libusb_darwin_acfl); + /* add the cfSource to the aync run loop */ CFRunLoopAddSource(libusb_darwin_acfl, priv->cfSource, kCFRunLoopCommonModes); } @@ -975,13 +959,14 @@ static void darwin_close (struct libusb_device_handle *dev_handle) { libusb_release_interface (dev_handle, i); if (0 == dpriv->open_count) { - if (priv->is_open) { - /* delete the device's async event source */ - if (priv->cfSource) { - CFRunLoopRemoveSource (libusb_darwin_acfl, priv->cfSource, kCFRunLoopDefaultMode); - CFRelease (priv->cfSource); - } + /* delete the device's async event source */ + if (priv->cfSource) { + CFRunLoopRemoveSource (libusb_darwin_acfl, priv->cfSource, kCFRunLoopDefaultMode); + CFRelease (priv->cfSource); + priv->cfSource = NULL; + } + if (priv->is_open) { /* close the device */ kresult = (*(dpriv->device))->USBDeviceClose(dpriv->device); if (kresult) { @@ -990,15 +975,6 @@ static void darwin_close (struct libusb_device_handle *dev_handle) { usbi_warn (HANDLE_CTX (dev_handle), "USBDeviceClose: %s", darwin_error_str(kresult)); } } - - kresult = (*(dpriv->device))->Release(dpriv->device); - if (kresult) { - /* 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), "Release: %s", darwin_error_str(kresult)); - } - - dpriv->device = NULL; } /* file descriptors are maintained per-instance */ |