Core: Keep a list of contexts created by libusb_init()

* This list can be used by backends to determine which contexts
  need notification of device removal (and in the future device arrival).
  The active_contexts_list is protected from multiple access by the active_contexts_lock.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Core: Cache device descriptors in the core

Discussion: It is necessary to cache the device descriptor in the core to
support reading the descriptors after a device is disconnected. We could
either 1) allow each backend to handle this caching (which would most
certainly duplicate code), or 2) cache the descritors when a device
is added. This patch does the later.

Further discussion: It might be beneficial to cache more than just the
device descriptors in the core. It might also be worthwhile caching the
configuration and BOS descriptors as well.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Fixup .gitignore for examples and tests now living in subdirs

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Add hotplug support.

The internal API is changing as follows:
 - Adding two new functions. usbi_connect_device, and usbi_disconnect_device.
   Backends must call these functions to add them to the context's device list
   at one of two places: initial enumeration (done at init), and on device
   attach and removal. These functions need to be called once per context.
 - Backends that support hotplug should not provide a get_device_list funtion.
   This function is now deprecated and will likely be removed once all backends
   support hotplug.

The external API is changing as follows:
 - Two new functions have been added to register and deregister callbacks for
   hotplug notification: libusb_hotplug_register_callback(),
   libusb_hotplug_deregister_callback(). Hotplug callbacks are called by
   libusb_handle_events(). Details of the new API can be found in libusb.h.
 - A new capability check has been added to check for hotplug support. See
   LIBUSB_CAP_HAS_HOTPLUG.

Aa suggested by Xiaofan add new example has been added to show how to use
the new external hotplug API. See examples/hotplugtest.c.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Add hotplug support to the Darwin backend.

Hotplug events are handled by the async event thread. This thread listens for
two events: kIOTerminatedNotification, and kIOFirstMatchNotification. If
either of these events fires the thread will iterate through the appropriate
iterator and will either enumerate or call usbi_disconnect_device depending
on the event.

While adding hotplug support it was discovered that when reading from the IO
registry we need to use kCFNumberSInt32Type for the device location. Using
kCFNumberLongType would cause locations to be sign-extended and not match
what is stored for the session id. This is now fixed.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Add hotplug support to the Linux backend.

There are two ways to configure hotplug support for Linux: udev, and netlink. It
is strongly recommened that udev support is used on systems that utilize udev.
We reenforce this recommendation by defaulting to --with-udev=yes at configure
time. To enable netlink support run configure with --with-udev=no. If udev
support is enabled all device enumeration is done with udev.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Bump LIBUSBX_API_VERSION for new hotplug API

And document the minimum version under which hotplug is supported

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
darwin: allow control messages on devices we can not exclusively open

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
darwin: dpriv->device will never be NULL for a valid device. remove unnecessary code that handled this case

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
darwin/libusb_reset: reenumerate device if the device descriptor or any configuration descriptor has changed

Some background: the Linux kernel causes a device to be reenumeration when
reset if the device's descriptors have changed. Darwin does not provide
this feature but instead provides a call to force the reenumeration of
a device. This behavior is required to support the firmware update
protocol used by some devices.

This patch updates darwin_reset to check if either the device or
configuration descriptors have changed. If they have darwin_reset will
ask the OS to reenumerate the device and return LIBUSB_ERROR_NOT_FOUND
or LIBUSB_SUCCESS accordingly.

Fixes #169

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
darwin: add support for the LIBUSB_TRANSFER_ADD_ZERO_PACKET transfer flag.

The zero-length packet is sent by the async event thread if all the following
conditions are met:

1) The packet size is not a multiple of maxPacketSize, and
2) the transfer completed successfully.

References #6.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
darwin: don't assume an interval of 1 or high/super speed

This patch updates submit_iso_transfer to use the bInterval value of the
endpoint and the speed of the device to determine the last frame of the
transaction.

Fixes: #165
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
core: release the context device list when the context is deleted.

This commit fixes a memory leak in the context list patch. Devices need
to be unref'd when the context is deleted. This can happen if either 1)
the backend init fails, or 2) the context is destroyed by libusb_exit().

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
darwin: fix memory leaks in the darwin backend.

There were two memory leaks in the darwin backend:
 - A CFString allocated by IORegistryEntryCreateCFProperty in darwin_get_interface()
   was not released.
 - The runloop is retained by darwin_open() but was not release by darwin_close().

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
darwin: clean up old code

For some time only one type of completion message has been in use in the darwin
backend. The commit cleans up this code. This commit also removes some code
that has never done anything useful. If a pipe has an error it probably means
the device was closed so there is nothing to do.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
darwin: return LIBUSB_ERROR_NOT_FOUND when reading the active config descriptor when the device is not configured.

fixes #172

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Core: use C99 flexible array member when possible

Fix clang warning by using C99 flexible array member instead of zero
length array gcc extension

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Add some editor meta-comments for proper tab usage

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Add INSTALL_WIN.txt eol=crlf to .gitattributes

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
examples: Only check for HAVE_SIGACTION once in Makefile.am

Signed-off-by: Hans de Goede <hdegoede@redhat.com>