Fix a small typo in comment to free_streams()

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
linux_usbfs: Don't mark init_count as volatile

There is no reason to mark init_count as volatile, it is protected by the
linux_hotplug_startstop_lock. Removing the volatile marking fixes the
following Coverity warning:

*** CID 62574:  Side effect in assertion  (ASSERT_SIDE_EFFECT)
/libusb/os/linux_usbfs.c: 460 in op_exit()
454     	return r;
455     }
456
457     static void op_exit(void)
458     {
459     	usbi_mutex_static_lock(&linux_hotplug_startstop_lock);
>>>     CID 62574:  Side effect in assertion  (ASSERT_SIDE_EFFECT)
>>>     Argument "init_count" of assert() has a side effect because the variable is volatile.  The containing function might work differently in a non-debug build.
460     	assert(init_count != 0);
461     	if (!--init_count) {
462     		/* tear down event handler */
463     		(void)linux_stop_event_monitor();
464     	}
465     	usbi_mutex_static_unlock(&linux_hotplug_startstop_lock);

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
linux_usbfs: op_handle_events: Protect against not finding the device-handle

We scan the list of open devices to find the device-handle based on the fd,
add a check to ensure that we've actually found the handle before continuing.

This fixes the following Coverity warning:

*** CID 62575:  Explicit null dereferenced  (FORWARD_NULL)
/libusb/os/linux_usbfs.c: 2594 in op_handle_events()
2588     			hpriv = _device_handle_priv(handle);
2589     			if (hpriv->fd == pollfd->fd)
2590     				break;
2591     		}
2592
2593     		if (pollfd->revents & POLLERR) {
>>>     CID 62575:  Explicit null dereferenced  (FORWARD_NULL)
>>>     Dereferencing null pointer "hpriv".
2594     			usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->fd);
2595     			usbi_handle_disconnect(handle);
2596     			/* device will still be marked as attached if hotplug
monitor thread
2597     			 * hasn't processed remove event yet */
2598     			usbi_mutex_static_lock(&linux_hotplug_lock);
2599     			if (handle->dev->attached)

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
linux_udev: Fix a Coverity warning

This really is a false positive, but easy enough to silence:

*** CID 62578:  Failure to restore non-local value  (MISSING_RESTORE)
/libusb/os/linux_udev.c: 64 in linux_udev_start_event_monitor()
58     	int r;
59
60     	assert(udev_ctx == NULL);
61     	udev_ctx = udev_new();
62     	if (!udev_ctx) {
63     		usbi_err(NULL, "could not create udev context");
>>>     CID 62578:  Failure to restore non-local value  (MISSING_RESTORE)
>>>     Value of non-local "udev_ctx" that was verified to be "NULL" is not restored as it was along other paths.
64     		return LIBUSB_ERROR_OTHER;
65     	}
66
67     	udev_monitor = udev_monitor_new_from_netlink(udev_ctx, "udev");
68     	if (!udev_monitor) {
69     		usbi_err(NULL, "could not initialize udev monitor");

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
libusb_submit_transfer: Fix possible deadlock

Fix a possible deadlock due to a lock ordering reversal, caught by Coverity:

*** CID 62579:  Thread deadlock  (ORDER_REVERSAL)
/libusb/io.c: 1451 in libusb_submit_transfer()
1445     	r = calculate_timeout(itransfer);
1446     	if (r < 0) {
1447     		r = LIBUSB_ERROR_OTHER;
1448     		goto out;
1449     	}
1450
>>>     CID 62579:  Thread deadlock  (ORDER_REVERSAL)
>>>     Calling "pthread_mutex_lock" acquires lock "libusb_context.flying_transfers_lock" while holding lock "usbi_transfer.lock" (count: 1 / 4).
1451     	usbi_mutex_lock(&ctx->flying_transfers_lock);
1452     	r = add_to_flying_list(itransfer);
1453     	if (r == LIBUSB_SUCCESS) {
1454     		r = usbi_backend->submit_transfer(itransfer);
1455     	}
1456     	if (r != LIBUSB_SUCCESS) {

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
libusb_submit_transfer: Don't reference the device on submission failure

If submission fails libusb_ref_device will never get balanced by an unref
on completion, since there will be no completion.

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

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Made darwin_hotplug_poll static to fix Xcode warning

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
Fixed clang static analyzer warning about dereferencing null

It looks like *ifcp should be the same as iface,
so I just use the latter instead of the former.

Closes pull request #13

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
Fix leak in failure path of libusb_get_max_packet_size()

If libusb_get_max_packet_size() or libusb_get_max_iso_packet_size()
fails to find the endpoint, it leaks the configuration descriptor.  Fix
this.

Signed-off-by: John Sheu <sheu@google.com>
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
libusb 1.0.19

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
usbi_handle_transfer_completion: Remove signal to event waiters

usbi_handle_transfer_completion() is only called by backends when
handling transfer completion. The backend can only make this call
whilst holding the events lock, therefore it is pointless to send
this signal. Any threads waiting to be signaled will be woken up
and will either find that an event handler is still active or will
try to obtain the events lock and be blocked.

Event waiters are automatically signaled in libusb_unlock_events(),
so when the backend is done handling events it will release the lock
and wake up any waiters. At this point the events lock wll be free,
which is the only time waking up waiters makes sense.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
core: Only interrupt event handlers as necessary during libusb_open()

The current behavior of libusb_open() unconditionally interrupts
any event handlers via usbi_fd_notification(). However, not all
backends, namely Win/WinCE, make changes to the pollfd list during
a device open.

This change adds a new CAP for backends (HAS_POLLABLE_DEVICE_FD),
and libusb_open() will only call usbi_fd_notification() if the
backend declares this capability.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
core: Be more efficient by not passing internal fds to backend

There are a number of fds that the core uses internally. Currently
any events on these fds are masked out so that the backend will
skip over them during handle_events().

This change improves upon this by simply not providing these fds
to the backend.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
Misc: Add myself to the README as a maintainer
core: Reuse poll fds across calls to handle_events()

Prior to this patch, the array of poll fds given to poll() was
allocated and freed every time handle_events() was called. This
is unnecessary if the list of poll fds has not changed since the
last call to handle_events(). With this patch, the array and count
of poll fds is stored in the context and only reallocated when the
list of poll fds changes, saving any unnecessary overhead.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
core: Rename count variable for pollfds for consistency with other count variables

Signed-off-by: Chris Dickens <chris.dickens@hp.com>
core: Fix reference count leak for default context

If the default context was created but failed to fully initialize,
the reference count was not decremented appropriately.
Windows: Define WINVER to fix building on MinGW

* For newer versions of MinGW, VerSetConditionMask() is only defined
  if building for Windows XP (0x501) or newer.
core: Fix compiler warnings on MinGW and Visual Studio

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>