From ad5ea4fe1a4c2f4c91011c8409ad3e09c24f4cf6 Mon Sep 17 00:00:00 2001 From: hselasky Date: Tue, 4 Feb 2014 10:22:28 +0000 Subject: [PATCH] MFC r260315: Implement two new libusb API functions. PR: usb/185454 git-svn-id: svn://svn.freebsd.org/base/stable/9@261483 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- lib/libusb/Makefile | 2 ++ lib/libusb/libusb.3 | 33 +++++++++++++++++++++++++++++++-- lib/libusb/libusb.h | 2 ++ lib/libusb/libusb10_io.c | 38 ++++++++++++++++++++++++++++++-------- 4 files changed, 65 insertions(+), 10 deletions(-) diff --git a/lib/libusb/Makefile b/lib/libusb/Makefile index 04a49c119..93180d80a 100644 --- a/lib/libusb/Makefile +++ b/lib/libusb/Makefile @@ -98,6 +98,8 @@ MLINKS += libusb.3 libusb_event_handler_active.3 MLINKS += libusb.3 libusb_lock_event_waiters.3 MLINKS += libusb.3 libusb_unlock_event_waiters.3 MLINKS += libusb.3 libusb_wait_for_event.3 +MLINKS += libusb.3 libusb_handle_events_timeout_completed.3 +MLINKS += libusb.3 libusb_handle_events_completed.3 MLINKS += libusb.3 libusb_handle_events_timeout.3 MLINKS += libusb.3 libusb_handle_events.3 MLINKS += libusb.3 libusb_handle_events_locked.3 diff --git a/lib/libusb/libusb.3 b/lib/libusb/libusb.3 index 898c88f72..785c8c475 100644 --- a/lib/libusb/libusb.3 +++ b/lib/libusb/libusb.3 @@ -485,11 +485,40 @@ transfer completes or another thread stops event handling, and 1 if the timeout expired. .Pp .Ft int +.Fn libusb_handle_events_timeout_completed "libusb_context *ctx" "struct timeval *tv" "int *completed" +Handle any pending events by checking if timeouts have expired and by +checking the set of file descriptors for activity. +If the +.Fa completed +argument is not equal to NULL, this function will +loop until a transfer completion callback sets the variable pointed to +by the +.Fa completed +argument to non-zero. +If the +.Fa tv +argument is not equal to NULL, this function will return +LIBUSB_ERROR_TIMEOUT after the given timeout. +Returns 0 on success, or a LIBUSB_ERROR code on failure or timeout. +.Pp +.Ft int +.Fn libusb_handle_events_completed "libusb_context *ctx" "int *completed" +Handle any pending events by checking the set of file descriptors for activity. +If the +.Fa completed +argument is not equal to NULL, this function will +loop until a transfer completion callback sets the variable pointed to +by the +.Fa completed +argument to non-zero. +Returns 0 on success, or a LIBUSB_ERROR code on failure. +.Pp +.Ft int .Fn libusb_handle_events_timeout "libusb_context *ctx" "struct timeval *tv" Handle any pending events by checking if timeouts have expired and by checking the set of file descriptors for activity. Returns 0 on success, or a -LIBUSB_ERROR code on failure. +LIBUSB_ERROR code on failure or timeout. .Pp .Ft int .Fn libusb_handle_events "libusb_context *ctx" @@ -508,7 +537,7 @@ Must be called with the event lock held. Determine the next internal timeout that libusb needs to handle. Returns 0 if there are no pending timeouts, 1 if a timeout was returned, or a LIBUSB_ERROR -code on failure. +code on failure or timeout. .Pp .Ft void .Fn libusb_set_pollfd_notifiers "libusb_context *ctx" "libusb_pollfd_added_cb added_cb" "libusb_pollfd_removed_cb remove_cb" "void *user_data" diff --git a/lib/libusb/libusb.h b/lib/libusb/libusb.h index 99b88e4a0..5ce29c128 100644 --- a/lib/libusb/libusb.h +++ b/lib/libusb/libusb.h @@ -436,6 +436,8 @@ int libusb_event_handler_active(libusb_context * ctx); void libusb_lock_event_waiters(libusb_context * ctx); void libusb_unlock_event_waiters(libusb_context * ctx); int libusb_wait_for_event(libusb_context * ctx, struct timeval *tv); +int libusb_handle_events_timeout_completed(libusb_context * ctx, struct timeval *tv, int *completed); +int libusb_handle_events_completed(libusb_context * ctx, int *completed); int libusb_handle_events_timeout(libusb_context * ctx, struct timeval *tv); int libusb_handle_events(libusb_context * ctx); int libusb_handle_events_locked(libusb_context * ctx, struct timeval *tv); diff --git a/lib/libusb/libusb10_io.c b/lib/libusb/libusb10_io.c index 84555d144..066f5c76e 100644 --- a/lib/libusb/libusb10_io.c +++ b/lib/libusb/libusb10_io.c @@ -331,29 +331,50 @@ libusb_wait_for_event(libusb_context *ctx, struct timeval *tv) } int -libusb_handle_events_timeout(libusb_context *ctx, struct timeval *tv) +libusb_handle_events_timeout_completed(libusb_context *ctx, + struct timeval *tv, int *completed) { - int err; + int err = 0; ctx = GET_CONTEXT(ctx); - DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout enter"); + DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout_completed enter"); libusb_lock_events(ctx); - err = libusb_handle_events_locked(ctx, tv); + while (1) { + if (completed != NULL) { + if (*completed != 0 || err != 0) + break; + } + err = libusb_handle_events_locked(ctx, tv); + if (completed == NULL) + break; + } libusb_unlock_events(ctx); - DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout leave"); + DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout_completed exit"); return (err); } +int +libusb_handle_events_completed(libusb_context *ctx, int *completed) +{ + return (libusb_handle_events_timeout_completed(ctx, NULL, completed)); +} + +int +libusb_handle_events_timeout(libusb_context *ctx, struct timeval *tv) +{ + return (libusb_handle_events_timeout_completed(ctx, tv, NULL)); +} + int libusb_handle_events(libusb_context *ctx) { - return (libusb_handle_events_timeout(ctx, NULL)); + return (libusb_handle_events_timeout_completed(ctx, NULL, NULL)); } int @@ -366,8 +387,9 @@ libusb_handle_events_locked(libusb_context *ctx, struct timeval *tv) if (libusb_event_handling_ok(ctx)) { err = libusb10_handle_events_sub(ctx, tv); } else { - libusb_wait_for_event(ctx, tv); - err = 0; + err = libusb_wait_for_event(ctx, tv); + if (err != 0) + err = LIBUSB_ERROR_TIMEOUT; } return (err); } -- 2.45.0