libusb 1.0.20-rc3
[platform/upstream/libusb.git] / libusb / io.c
index 420f87a..279288c 100644 (file)
@@ -1316,8 +1316,7 @@ static int disarm_timerfd(struct libusb_context *ctx)
 /* iterates through the flying transfers, and rearms the timerfd based on the
  * next upcoming timeout.
  * must be called with flying_list locked.
- * returns 0 if there was no timeout to arm, 1 if the next timeout was armed,
- * or a LIBUSB_ERROR code on failure.
+ * returns 0 on success or a LIBUSB_ERROR code on failure.
  */
 static int arm_timerfd_for_next_timeout(struct libusb_context *ctx)
 {
@@ -1332,7 +1331,7 @@ static int arm_timerfd_for_next_timeout(struct libusb_context *ctx)
                        goto disarm;
 
                /* act on first transfer that is not already cancelled */
-               if (!(transfer->flags & USBI_TRANSFER_TIMED_OUT)) {
+               if (!(transfer->flags & USBI_TRANSFER_TIMEOUT_HANDLED)) {
                        int r;
                        const struct itimerspec it = { {0, 0},
                                { cur_tv->tv_sec, cur_tv->tv_usec * 1000 } };
@@ -1340,7 +1339,7 @@ static int arm_timerfd_for_next_timeout(struct libusb_context *ctx)
                        r = timerfd_settime(ctx->timerfd, TFD_TIMER_ABSTIME, &it, NULL);
                        if (r < 0)
                                return LIBUSB_ERROR_OTHER;
-                       return 1;
+                       return 0;
                }
        }
 
@@ -1426,8 +1425,8 @@ out:
 
 /* remove a transfer from the active transfers list.
  * This function will *always* remove the transfer from the
- * flying_transfers list. It will return non 0 if it fails to
- * update the timer for the next timeout. */
+ * flying_transfers list. It will return a LIBUSB_ERROR code
+ * if it fails to update the timer for the next timeout. */
 static int remove_from_flying_list(struct usbi_transfer *transfer)
 {
        struct libusb_context *ctx = ITRANSFER_CTX(transfer);
@@ -1624,8 +1623,8 @@ int usbi_handle_transfer_completion(struct usbi_transfer *itransfer,
        int r;
 
        r = remove_from_flying_list(itransfer);
-       if (r)
-               return r;
+       if (r < 0)
+               usbi_err(ITRANSFER_CTX(itransfer), "failed to set timer for next timeout, errno=%d", errno);
 
        usbi_mutex_lock(&itransfer->flags_lock);
        itransfer->flags &= ~USBI_TRANSFER_IN_FLIGHT;
@@ -1654,7 +1653,7 @@ int usbi_handle_transfer_completion(struct usbi_transfer *itransfer,
        if (flags & LIBUSB_TRANSFER_FREE_TRANSFER)
                libusb_free_transfer(transfer);
        libusb_unref_device(handle->dev);
-       return 0;
+       return r;
 }
 
 /* Similar to usbi_handle_transfer_completion() but exclusively for transfers
@@ -1944,6 +1943,7 @@ static void handle_timeout(struct usbi_transfer *itransfer)
                USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
        int r;
 
+       itransfer->flags |= USBI_TRANSFER_TIMEOUT_HANDLED;
        r = libusb_cancel_transfer(transfer);
        if (r == 0)
                itransfer->flags |= USBI_TRANSFER_TIMED_OUT;
@@ -1979,7 +1979,7 @@ static int handle_timeouts_locked(struct libusb_context *ctx)
                        return 0;
 
                /* ignore timeouts we've already handled */
-               if (transfer->flags & (USBI_TRANSFER_TIMED_OUT | USBI_TRANSFER_OS_HANDLES_TIMEOUT))
+               if (transfer->flags & (USBI_TRANSFER_TIMEOUT_HANDLED | USBI_TRANSFER_OS_HANDLES_TIMEOUT))
                        continue;
 
                /* if transfer has non-expired timeout, nothing more to do */
@@ -2506,7 +2506,7 @@ int API_EXPORTED libusb_get_next_timeout(libusb_context *ctx,
 
        /* find next transfer which hasn't already been processed as timed out */
        list_for_each_entry(transfer, &ctx->flying_transfers, list, struct usbi_transfer) {
-               if (transfer->flags & (USBI_TRANSFER_TIMED_OUT | USBI_TRANSFER_OS_HANDLES_TIMEOUT))
+               if (transfer->flags & (USBI_TRANSFER_TIMEOUT_HANDLED | USBI_TRANSFER_OS_HANDLES_TIMEOUT))
                        continue;
 
                /* if we've reached transfers of infinte timeout, we're done looking */