diff --git a/lib/libusb/libusb.h b/lib/libusb/libusb.h --- a/lib/libusb/libusb.h +++ b/lib/libusb/libusb.h @@ -260,7 +260,8 @@ LIBUSB_OPTION_USE_USBDK = 1, LIBUSB_OPTION_NO_DEVICE_DISCOVERY = 2, LIBUSB_OPTION_WEAK_AUTHORITY = 2, - LIBUSB_OPTION_MAX = 3, + LIBUSB_OPTION_LOG_CB = 5, + LIBUSB_OPTION_MAX = 4, }; /* libusb structures */ @@ -271,6 +272,11 @@ struct libusb_device_handle; struct libusb_hotplug_callback_handle_struct; +typedef struct libusb_context libusb_context; + +typedef void (*libusb_log_cb)(libusb_context *ctx, enum libusb_log_level, + const char *str); + struct libusb_pollfd { int fd; short events; @@ -593,6 +599,7 @@ int libusb_free_streams(libusb_device_handle *dev, unsigned char *endpoints, int num_endpoints); void libusb_transfer_set_stream_id(struct libusb_transfer *transfer, uint32_t stream_id); uint32_t libusb_transfer_get_stream_id(struct libusb_transfer *transfer); +int libusb_set_option(libusb_context *ctx, enum libusb_option option, ...); #if 0 { /* indent fix */ diff --git a/lib/libusb/libusb10.h b/lib/libusb/libusb10.h --- a/lib/libusb/libusb10.h +++ b/lib/libusb/libusb10.h @@ -117,6 +117,8 @@ libusb_pollfd_added_cb fd_added_cb; libusb_pollfd_removed_cb fd_removed_cb; void *fd_cb_user_data; + libusb_log_cb log_cb; + int no_discovery; }; struct libusb_device { diff --git a/lib/libusb/libusb10.c b/lib/libusb/libusb10.c --- a/lib/libusb/libusb10.c +++ b/lib/libusb/libusb10.c @@ -243,6 +243,8 @@ } libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->event, POLLIN); + ctx->log_cb = NULL; + ctx->no_discovery = false; pthread_mutex_lock(&default_context_lock); if (usbi_default_context == NULL) { @@ -1806,7 +1808,71 @@ snprintf(new_fmt, sizeof(new_fmt), "%s: %s\n", log_prefix[level], fmt); vsnprintf(buffer, sizeof(buffer), new_fmt, args); - fputs(buffer, stdout); + if (ctx->log_cb != NULL) + ctx->log_cb(ctx, level, buffer); + else + fputs(buffer, stdout); + + va_end(args); +} + +int +libusb_set_option(libusb_context *ctx, enum libusb_option option, ...) +{ + int err = LIBUSB_SUCCESS; + enum libusb_log_level level; + va_list args; + libusb_log_cb callback; + + ctx = GET_CONTEXT(ctx); + va_start(args, option); + + switch (option) { + case LIBUSB_OPTION_LOG_LEVEL: + level = va_arg(args, enum libusb_log_level); + if (level < LIBUSB_LOG_LEVEL_NONE || + level > LIBUSB_LOG_LEVEL_DEBUG) { + err = LIBUSB_ERROR_INVALID_PARAM; + break; + } + + break; + case LIBUSB_OPTION_LOG_CB: + callback = va_arg(args, libusb_log_cb); + break; + } + + if (option >= LIBUSB_OPTION_MAX) { + err = LIBUSB_ERROR_INVALID_PARAM; + goto end; + } + + switch (option) { + case LIBUSB_OPTION_LOG_LEVEL: + if (ctx->debug_fixed) + break; + ctx->debug = level; + break; + case LIBUSB_OPTION_LOG_CB: + ctx->log_cb = callback; + break; + case LIBUSB_OPTION_NO_DEVICE_DISCOVERY: + ctx->no_discovery = true; + break; + /* + * We don't handle USBDK as it is a windows + * backend specified SDK + */ + case LIBUSB_OPTION_USE_USBDK: + break; + case LIBUSB_OPTION_MAX: + default: + err = LIBUSB_ERROR_INVALID_PARAM; + break; + } + +end: va_end(args); + return (err); } diff --git a/lib/libusb/libusb10_hotplug.c b/lib/libusb/libusb10_hotplug.c --- a/lib/libusb/libusb10_hotplug.c +++ b/lib/libusb/libusb10_hotplug.c @@ -346,7 +346,7 @@ ctx = GET_CONTEXT(ctx); - if (ctx->usb_event_mode == usb_event_none) { + if (ctx->usb_event_mode == usb_event_none && !ctx->no_discovery) { HOTPLUG_LOCK(ctx); if (!netlink_init(ctx) && !devd_init(ctx)) ctx->usb_event_mode = usb_event_scan; @@ -364,8 +364,8 @@ return (LIBUSB_ERROR_NO_MEM); HOTPLUG_LOCK(ctx); - if (ctx->hotplug_handler == NO_THREAD) { - libusb_hotplug_enumerate(ctx, &ctx->hotplug_devs); + if (ctx->hotplug_handler == NO_THREAD && !ctx->no_discovery) { + libusb_hotplug_enumerate(ctx, &ctx->hotplug_devs); if (pthread_create(&ctx->hotplug_handler, NULL, &libusb_hotplug_scan, ctx) != 0)