darwin: Retry IOCreatePlugInInterfaceForService on failure

We have a case where a device might not be detected by libusb if we plug
it after initializing the library. We were able to reproduce this issue
pretty consistently on a Mac Mini and several MacBook Pros running very
recent macOS versions (Mojave) using the Balena Fin board
(https://www.balena.io/fin/). I don't know if this happens with other
USB devices.

Enabling debug output revealed the following:

```
[ 7.901582] [00004f07] libusb: debug [darwin_get_cached_device] finding cached device for sessionID 0x2a9b5279f04
[ 7.901764] [00004f07] libusb: debug [darwin_get_cached_device] matching sessionID 0x2a9b5279f04 against cached device with sessionID 0x17dbd029b6a
[ 7.901798] [00004f07] libusb: debug [darwin_get_cached_device] matching sessionID 0x2a9b5279f04 against cached device with sessionID 0x1688763269f
[ 7.901818] [00004f07] libusb: debug [darwin_get_cached_device] matching sessionID 0x2a9b5279f04 against cached device with sessionID 0x4c0aefaa
[ 7.901831] [00004f07] libusb: debug [darwin_get_cached_device] matching sessionID 0x2a9b5279f04 against cached device with sessionID 0x0
[ 7.901845] [00004f07] libusb: debug [darwin_get_cached_device] caching new device with sessionID 0x2a9b5279f04
[ 7.903377] [00004f07] libusb: debug [darwin_device_from_service] could not set up plugin for service: out of resources
[ 8.029152] [00000307] libusb: debug [libusb_get_device_list]
```

The "out of resources" error comes from
`IOCreatePlugInInterfaceForService` which will apparently return this
error if the `Start` method of the interface is not success (see
https://github.com/opensource-apple/IOKitUser/blob/b80a5cbc0ebfb5c4954ef6d757918db0e4dc4b7f/IOCFPlugIn.c#L232).

Retrying `IOCreatePlugInInterfaceForService` makes it work for me.

Closes #542

Signed-off-by: Juan Cruz Viotti <juan@balena.io>
Signed-off-by: Nathan Hjelm <hjelmn@me.com>
2 files changed
tree: 8d1308ea8e0e7ef246f98d3e28f662a2a3f28d5c
  1. .private/
  2. android/
  3. doc/
  4. examples/
  5. libusb/
  6. msvc/
  7. tests/
  8. Xcode/
  9. .gitattributes
  10. .gitignore
  11. .travis.yml
  12. appveyor.yml
  13. appveyor_cygwin.bat
  14. appveyor_minGW.bat
  15. AUTHORS
  16. autogen.sh
  17. bootstrap.sh
  18. Brewfile
  19. ChangeLog
  20. configure.ac
  21. COPYING
  22. INSTALL_WIN.txt
  23. libusb-1.0.pc.in
  24. Makefile.am
  25. NEWS
  26. PORTING
  27. README.git
  28. README.md
  29. TODO
  30. travis-autogen.sh
README.md

libusb

Build Status Build status Coverity Scan Build Status

libusb is a library for USB device access from Linux, macOS, Windows, OpenBSD/NetBSD and Haiku userspace. It is written in C (Haiku backend in C++) and licensed under the GNU Lesser General Public License version 2.1 or, at your option, any later version (see COPYING).

libusb is abstracted internally in such a way that it can hopefully be ported to other operating systems. Please see the PORTING file for more information.

libusb homepage: http://libusb.info/

Developers will wish to consult the API documentation: http://api.libusb.info

Use the mailing list for questions, comments, etc: http://mailing-list.libusb.info

(Please use the mailing list rather than mailing developers directly)