<div dir="ltr"><div>On Mon, Nov 26, 2018 at 7:41 PM Christophe Fergeau <<a href="mailto:cfergeau@redhat.com">cfergeau@redhat.com</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Sun, Sep 30, 2018 at 06:13:04PM +0300, Yuri Benditovich wrote:<br>
> This layer communicates with libusb and libusbredir and<br>
> provides the API for USB redirection procedures.<br>
> All the modules of spice-gtk communicate only with usb<br>
> backend instead of calling libusb and usbredirhost directly.<br>
> This is prerequisite of further implementation of<br>
> cd-sharing via USB redirection.<br>
> <br>
> Signed-off-by: Yuri Benditovich <<a href="mailto:yuri.benditovich@daynix.com" target="_blank">yuri.benditovich@daynix.com</a>><br>
> ---<br>
>  src/Makefile.am               |   2 +<br>
>  src/channel-usbredir-priv.h   |   9 +-<br>
>  src/channel-usbredir.c        | 232 ++++--------<br>
>  src/meson.build               |   1 +<br>
>  src/usb-backend-common.c      | 688 ++++++++++++++++++++++++++++++++++<br>
>  src/usb-backend.h             | 115 ++++++<br>
>  src/usb-device-manager-priv.h |   3 +-<br>
>  src/usb-device-manager.c      | 398 +++++++-------------<br>
>  src/usb-device-manager.h      |   1 +<br>
>  src/usbutil.c                 |  36 --<br>
>  src/usbutil.h                 |   2 -<br>
>  src/win-usb-dev.c             |  59 +--<br>
>  12 files changed, 1044 insertions(+), 502 deletions(-)<br>
>  create mode 100644 src/usb-backend-common.c<br>
>  create mode 100644 src/usb-backend.h<br>
> <br>
> diff --git a/src/Makefile.am b/src/Makefile.am<br>
> index 1bb6f9b..3431459 100644<br>
> --- a/src/Makefile.am<br>
> +++ b/src/Makefile.am<br>
> @@ -253,6 +253,8 @@ libspice_client_glib_2_0_la_SOURCES =                     \<br>
>       spice-uri-priv.h                                \<br>
>       usb-device-manager.c                            \<br>
>       usb-device-manager-priv.h                       \<br>
> +     usb-backend.h                           \<br>
> +     usb-backend-common.c                    \<br>
>       usbutil.c                                       \<br>
>       usbutil.h                                       \<br>
>       $(USB_ACL_HELPER_SRCS)                          \<br>
> diff --git a/src/channel-usbredir-priv.h b/src/channel-usbredir-priv.h<br>
> index 17e9716..4064f36 100644<br>
> --- a/src/channel-usbredir-priv.h<br>
> +++ b/src/channel-usbredir-priv.h<br>
> @@ -21,9 +21,8 @@<br>
>  #ifndef __SPICE_CLIENT_USBREDIR_CHANNEL_PRIV_H__<br>
>  #define __SPICE_CLIENT_USBREDIR_CHANNEL_PRIV_H__<br>
>  <br>
> -#include <libusb.h><br>
> -#include <usbredirfilter.h><br>
>  #include "spice-client.h"<br>
> +#include "usb-backend.h"<br>
>  <br>
>  G_BEGIN_DECLS<br>
>  <br>
> @@ -31,7 +30,7 @@ G_BEGIN_DECLS<br>
>     context should not be destroyed before the last device has been<br>
>     disconnected */<br>
>  void spice_usbredir_channel_set_context(SpiceUsbredirChannel *channel,<br>
> -                                        libusb_context       *context);<br>
> +                                        SpiceUsbBackend      *context);<br>
>  <br>
>  void spice_usbredir_channel_disconnect_device_async(SpiceUsbredirChannel *channel,<br>
>                                                      GCancellable *cancellable,<br>
> @@ -46,7 +45,7 @@ gboolean spice_usbredir_channel_disconnect_device_finish(SpiceUsbredirChannel *c<br>
>     (through spice_channel_connect()), before calling this. */<br>
>  void spice_usbredir_channel_connect_device_async(<br>
>                                          SpiceUsbredirChannel *channel,<br>
> -                                        libusb_device        *device,<br>
> +                                        SpiceUsbBackendDevice  *device,<br>
>                                          SpiceUsbDevice       *spice_device,<br>
>                                          GCancellable         *cancellable,<br>
>                                          GAsyncReadyCallback   callback,<br>
> @@ -58,7 +57,7 @@ gboolean spice_usbredir_channel_connect_device_finish(<br>
>  <br>
>  void spice_usbredir_channel_disconnect_device(SpiceUsbredirChannel *channel);<br>
>  <br>
> -libusb_device *spice_usbredir_channel_get_device(SpiceUsbredirChannel *channel);<br>
> +SpiceUsbBackendDevice *spice_usbredir_channel_get_device(SpiceUsbredirChannel *channel);<br>
>  <br>
>  void spice_usbredir_channel_lock(SpiceUsbredirChannel *channel);<br>
>  <br>
> diff --git a/src/channel-usbredir.c b/src/channel-usbredir.c<br>
> index 182edc4..0eba954 100644<br>
> --- a/src/channel-usbredir.c<br>
> +++ b/src/channel-usbredir.c<br>
> @@ -23,7 +23,6 @@<br>
>  <br>
>  #ifdef USE_USBREDIR<br>
>  #include <glib/gi18n-lib.h><br>
> -#include <usbredirhost.h><br>
>  #ifdef USE_LZ4<br>
>  #include <lz4.h><br>
>  #endif<br>
> @@ -66,15 +65,12 @@ enum SpiceUsbredirChannelState {<br>
>  };<br>
>  <br>
>  struct _SpiceUsbredirChannelPrivate {<br>
> -    libusb_device *device;<br>
> +    SpiceUsbBackendDevice *device;<br>
>      SpiceUsbDevice *spice_device;<br>
> -    libusb_context *context;<br>
> -    struct usbredirhost *host;<br>
> +    SpiceUsbBackend *context;<br>
> +    SpiceUsbBackendChannel *host;<br>
>      /* To catch usbredirhost error messages and report them as a GError */<br>
>      GError **catch_error;<br>
> -    /* Data passed from channel handle msg to the usbredirhost read cb */<br>
> -    const uint8_t *read_buf;<br>
> -    int read_buf_size;<br>
>      enum SpiceUsbredirChannelState state;<br>
>  #ifdef USE_POLKIT<br>
>      GTask *task;<br>
> @@ -90,18 +86,10 @@ static void spice_usbredir_channel_dispose(GObject *obj);<br>
>  static void spice_usbredir_channel_finalize(GObject *obj);<br>
>  static void usbredir_handle_msg(SpiceChannel *channel, SpiceMsgIn *in);<br>
>  <br>
> -static void usbredir_log(void *user_data, int level, const char *msg);<br>
> -static int usbredir_read_callback(void *user_data, uint8_t *data, int count);<br>
> +static void usbredir_error(void *user_data, const char *msg);<br>
>  static int usbredir_write_callback(void *user_data, uint8_t *data, int count);<br>
> -static void usbredir_write_flush_callback(void *user_data);<br>
> -#if USBREDIR_VERSION >= 0x000701<br>
> -static uint64_t usbredir_buffered_output_size_callback(void *user_data);<br>
> -#endif<br>
> -<br>
> -static void *usbredir_alloc_lock(void);<br>
> -static void usbredir_lock_lock(void *user_data);<br>
> -static void usbredir_unlock_lock(void *user_data);<br>
> -static void usbredir_free_lock(void *user_data);<br>
> +static gboolean usbredir_is_channel_ready(void *user_data);<br>
> +static uint64_t usbredir_get_queue_size(void *user_data);<br>
>  <br>
>  #else<br>
>  struct _SpiceUsbredirChannelPrivate {<br>
> @@ -128,7 +116,7 @@ static void _channel_reset_finish(SpiceUsbredirChannel *channel, gboolean migrat<br>
>  <br>
>      spice_usbredir_channel_lock(channel);<br>
>  <br>
> -    usbredirhost_close(priv->host);<br>
> +    spice_usb_backend_channel_finalize(priv->host);<br>
<br>
I'd call this _free rather than _finalize to avoid confusion with<br>
GObject terminology.<br></blockquote><div><br></div><div>This can be changed in V3</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
>      priv->host = NULL;<br>
>  <br>
>      /* Call set_context to re-create the host */<br>
> @@ -238,7 +226,7 @@ static void spice_usbredir_channel_finalize(GObject *obj)<br>
>      SpiceUsbredirChannel *channel = SPICE_USBREDIR_CHANNEL(obj);<br>
>  <br>
>      if (channel->priv->host)<br>
> -        usbredirhost_close(channel->priv->host);<br>
> +        spice_usb_backend_channel_finalize(channel->priv->host);<br>
>  #ifdef USE_USBREDIR<br>
>      g_mutex_clear(&channel->priv->device_connect_mutex);<br>
>  #endif<br>
> @@ -262,33 +250,25 @@ static void channel_set_handlers(SpiceChannelClass *klass)<br>
>  /* private api                                                        */<br>
>  <br>
>  G_GNUC_INTERNAL<br>
> -void spice_usbredir_channel_set_context(SpiceUsbredirChannel *channel,<br>
> -                                        libusb_context       *context)<br>
> +void spice_usbredir_channel_set_context(<br>
> +    SpiceUsbredirChannel *channel,<br>
> +    SpiceUsbBackend      *context)<br>
<br>
You could keep the initial indentation.<br>
<br></blockquote><div><br></div><div>OK</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
>  {<br>
>      SpiceUsbredirChannelPrivate *priv = channel->priv;<br>
> +    SpiceUsbBackendChannelInitData init_data;<br>
> +    init_data.user_data = channel;<br>
> +    init_data.get_queue_size = usbredir_get_queue_size;<br>
> +    init_data.is_channel_ready = usbredir_is_channel_ready;<br>
> +    init_data.on_error = usbredir_error;<br>
> +    init_data.write_callback = usbredir_write_callback;<br>
>  <br>
>      g_return_if_fail(priv->host == NULL);<br>
>  <br>
>      priv->context = context;<br>
> -    priv->host = usbredirhost_open_full(<br>
> -                                   context, NULL,<br>
> -                                   usbredir_log,<br>
> -                                   usbredir_read_callback,<br>
> -                                   usbredir_write_callback,<br>
> -                                   usbredir_write_flush_callback,<br>
> -                                   usbredir_alloc_lock,<br>
> -                                   usbredir_lock_lock,<br>
> -                                   usbredir_unlock_lock,<br>
> -                                   usbredir_free_lock,<br>
> -                                   channel, PACKAGE_STRING,<br>
> -                                   spice_util_get_debug() ? usbredirparser_debug : usbredirparser_warning,<br>
> -                                   usbredirhost_fl_write_cb_owns_buffer);<br>
> +    priv->host = spice_usb_backend_channel_initialize(context, &init_data);<br>
<br>
spice_usb_backend_channel_new() ?<br>
<br></blockquote><div><br></div><div>This can be changed in V3</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
>      if (!priv->host)<br>
> -        g_error("Out of memory allocating usbredirhost");<br>
> +        g_error("Out of memory initializing redirection support");<br>
>  <br>
> -#if USBREDIR_VERSION >= 0x000701<br>
> -    usbredirhost_set_buffered_output_size_cb(priv->host, usbredir_buffered_output_size_callback);<br>
> -#endif<br>
>  #ifdef USE_LZ4<br>
>      spice_channel_set_capability(channel, SPICE_SPICEVMC_CAP_DATA_COMPRESS_LZ4);<br>
>  #endif<br>
> @@ -299,8 +279,6 @@ static gboolean spice_usbredir_channel_open_device(<br>
>  {<br>
>      SpiceUsbredirChannelPrivate *priv = channel->priv;<br>
>      SpiceSession *session;<br>
> -    libusb_device_handle *handle = NULL;<br>
> -    int rc, status;<br>
>      SpiceUsbDeviceManager *manager;<br>
>  <br>
>      g_return_val_if_fail(priv->state == STATE_DISCONNECTED<br>
> @@ -309,21 +287,16 @@ static gboolean spice_usbredir_channel_open_device(<br>
>  #endif<br>
>                           , FALSE);<br>
>  <br>
> -    rc = libusb_open(priv->device, &handle);<br>
> -    if (rc != 0) {<br>
> -        g_set_error(err, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,<br>
> -                    "Could not open usb device: %s [%i]",<br>
> -                    spice_usbutil_libusb_strerror(rc), rc);<br>
> -        return FALSE;<br>
> -    }<br>
> -<br>
>      priv->catch_error = err;<br>
> -    status = usbredirhost_set_device(priv->host, handle);<br>
> -    priv->catch_error = NULL;<br>
> -    if (status != usb_redir_success) {<br>
> -        g_return_val_if_fail(err == NULL || *err != NULL, FALSE);<br>
> +    if (!spice_usb_backend_channel_attach(priv->host, priv->device, err)) {<br>
> +        priv->catch_error = NULL;<br>
> +        if (*err == NULL) {<br>
> +            g_set_error(err, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,<br>
> +                "Error attaching device: (no error information)");<br>
<br>
You could line up the "Error .." with the opening parenthesis.<br>
<br></blockquote><div><br></div><div>This is not described exactly in Spice coding style document and existing code of spice-gtk</div><div>uses different solution for indentation, so this is point of personal preference.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> +        }<br>
>          return FALSE;<br>
>      }<br>
> +    priv->catch_error = NULL;<br>
>  <br>
>      session = spice_channel_get_session(SPICE_CHANNEL(channel));<br>
>      manager = spice_usb_device_manager_get(session, NULL);<br>
> @@ -331,7 +304,7 @@ static gboolean spice_usbredir_channel_open_device(<br>
>  <br>
>      priv->usb_device_manager = g_object_ref(manager);<br>
>      if (!spice_usb_device_manager_start_event_listening(priv->usb_device_manager, err)) {<br>
> -        usbredirhost_set_device(priv->host, NULL);<br>
> +        spice_usb_backend_channel_detach(priv->host);<br>
>          return FALSE;<br>
>      }<br>
>  <br>
> @@ -362,8 +335,7 @@ static void spice_usbredir_channel_open_acl_cb(<br>
>          spice_usbredir_channel_open_device(channel, &err);<br>
>      }<br>
>      if (err) {<br>
> -        libusb_unref_device(priv->device);<br>
> -        priv->device = NULL;<br>
> +        g_clear_pointer(&priv->device, spice_usb_backend_device_release);<br>
<br>
_unref rather than _release?<br></blockquote><div><br></div><div>This can be changed in V3, although I would prefer not to change it.</div><div>Semantics of ref/unref and acquire/release are not different.</div><div>Please let me know whether this change in mandatory.</div><div> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
>          g_boxed_free(spice_usb_device_get_type(), priv->spice_device);<br>
>          priv->spice_device = NULL;<br>
>          priv->state  = STATE_DISCONNECTED;<br>
> @@ -394,8 +366,7 @@ _open_device_async_cb(GTask *task,<br>
>      spice_usbredir_channel_lock(channel);<br>
>  <br>
>      if (!spice_usbredir_channel_open_device(channel, &err)) {<br>
> -        libusb_unref_device(priv->device);<br>
> -        priv->device = NULL;<br>
> +        g_clear_pointer(&priv->device, spice_usb_backend_device_release);<br>
>          g_boxed_free(spice_usb_device_get_type(), priv->spice_device);<br>
>          priv->spice_device = NULL;<br>
>      }<br>
> @@ -413,13 +384,16 @@ _open_device_async_cb(GTask *task,<br>
>  G_GNUC_INTERNAL<br>
>  void spice_usbredir_channel_connect_device_async(<br>
>                                            SpiceUsbredirChannel *channel,<br>
> -                                          libusb_device        *device,<br>
> +                                          SpiceUsbBackendDevice *device,<br>
>                                            SpiceUsbDevice       *spice_device,<br>
>                                            GCancellable         *cancellable,<br>
>                                            GAsyncReadyCallback   callback,<br>
>                                            gpointer              user_data)<br>
>  {<br>
>      SpiceUsbredirChannelPrivate *priv = channel->priv;<br>
> +#ifdef USE_POLKIT<br>
> +    const UsbDeviceInformation *info = spice_usb_backend_device_get_info(device);<br>
> +#endif<br>
>      GTask *task;<br>
>  <br>
>      g_return_if_fail(SPICE_IS_USBREDIR_CHANNEL(channel));<br>
> @@ -446,7 +420,8 @@ void spice_usbredir_channel_connect_device_async(<br>
>          goto done;<br>
>      }<br>
>  <br>
> -    priv->device = libusb_ref_device(device);<br>
> +    spice_usb_backend_device_acquire(device);<br>
> +    priv->device = device;<br>
<br>
You could mimic libusb API actually,<br>
priv->device = spice_usb_backend_device_ref(device);<br></blockquote><div><br></div><div>I do not think I need to mimic libusb or something other.</div><div>Please let me know whether this change is mandatory.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
>      priv->spice_device = g_boxed_copy(spice_usb_device_get_type(),<br>
>                                        spice_device);<br>
>  #ifdef USE_POLKIT<br>
> @@ -456,8 +431,8 @@ void spice_usbredir_channel_connect_device_async(<br>
>      g_object_set(spice_channel_get_session(SPICE_CHANNEL(channel)),<br>
>                   "inhibit-keyboard-grab", TRUE, NULL);<br>
>      spice_usb_acl_helper_open_acl_async(priv->acl_helper,<br>
> -                                        libusb_get_bus_number(device),<br>
> -                                        libusb_get_device_address(device),<br>
> +                                        info->bus,<br>
> +                                        info->address,<br>
>                                          cancellable,<br>
>                                          spice_usbredir_channel_open_acl_cb,<br>
>                                          channel);<br>
> @@ -515,9 +490,8 @@ void spice_usbredir_channel_disconnect_device(SpiceUsbredirChannel *channel)<br>
>          g_clear_object(&priv->usb_device_manager);<br>
>  <br>
>          /* This also closes the libusb handle we passed from open_device */<br>
> -        usbredirhost_set_device(priv->host, NULL);<br>
> -        libusb_unref_device(priv->device);<br>
> -        priv->device = NULL;<br>
> +        spice_usb_backend_channel_detach(priv->host);<br>
> +        g_clear_pointer(&priv->device, spice_usb_backend_device_release);<br>
>          g_boxed_free(spice_usb_device_get_type(), priv->spice_device);<br>
>          priv->spice_device = NULL;<br>
>          priv->state  = STATE_DISCONNECTED;<br>
> @@ -568,7 +542,7 @@ spice_usbredir_channel_get_spice_usb_device(SpiceUsbredirChannel *channel)<br>
>  #endif<br>
>  <br>
>  G_GNUC_INTERNAL<br>
> -libusb_device *spice_usbredir_channel_get_device(SpiceUsbredirChannel *channel)<br>
> +SpiceUsbBackendDevice *spice_usbredir_channel_get_device(SpiceUsbredirChannel *channel)<br>
>  {<br>
>      return channel->priv->device;<br>
>  }<br>
> @@ -583,85 +557,45 @@ void spice_usbredir_channel_get_guest_filter(<br>
>  <br>
>      g_return_if_fail(priv->host != NULL);<br>
>  <br>
> -    usbredirhost_get_guest_filter(priv->host, rules_ret, rules_count_ret);<br>
> +    spice_usb_backend_channel_get_guest_filter(priv->host, rules_ret, rules_count_ret);<br>
>  }<br>
>  <br>
>  /* ------------------------------------------------------------------ */<br>
>  /* callbacks (any context)                                            */<br>
>  <br>
> -#if USBREDIR_VERSION >= 0x000701<br>
> -static uint64_t usbredir_buffered_output_size_callback(void *user_data)<br>
> +static uint64_t usbredir_get_queue_size(void *user_data)<br>
>  {<br>
>      g_return_val_if_fail(SPICE_IS_USBREDIR_CHANNEL(user_data), 0);<br>
>      return spice_channel_get_queue_size(SPICE_CHANNEL(user_data));<br>
>  }<br>
> -#endif<br>
>  <br>
> -/* Note that this function must be re-entrant safe, as it can get called<br>
> -   from both the main thread as well as from the usb event handling thread */<br>
> -static void usbredir_write_flush_callback(void *user_data)<br>
> +static gboolean usbredir_is_channel_ready(void *user_data)<br>
>  {<br>
>      SpiceUsbredirChannel *channel = SPICE_USBREDIR_CHANNEL(user_data);<br>
>      SpiceUsbredirChannelPrivate *priv = channel->priv;<br>
> -<br>
> -    if (spice_channel_get_state(SPICE_CHANNEL(channel)) !=<br>
> -            SPICE_CHANNEL_STATE_READY)<br>
> -        return;<br>
> -<br>
> +    if (spice_channel_get_state(SPICE_CHANNEL(channel)) != SPICE_CHANNEL_STATE_READY)<br>
> +        return FALSE;<br>
>      if (!priv->host)<br>
> -        return;<br>
> -<br>
> -    usbredirhost_write_guest_data(priv->host);<br>
> -}<br>
> -<br>
> -static void usbredir_log(void *user_data, int level, const char *msg)<br>
> -{<br>
> -    SpiceUsbredirChannel *channel = user_data;<br>
> -    SpiceUsbredirChannelPrivate *priv = channel->priv;<br>
> -<br>
> -    if (priv->catch_error && level == usbredirparser_error) {<br>
> -        CHANNEL_DEBUG(channel, "%s", msg);<br>
> -        /* Remove "usbredirhost: " prefix from usbredirhost messages */<br>
> -        if (strncmp(msg, "usbredirhost: ", 14) == 0)<br>
> -            g_set_error_literal(priv->catch_error, SPICE_CLIENT_ERROR,<br>
> -                                SPICE_CLIENT_ERROR_FAILED, msg + 14);<br>
> -        else<br>
> -            g_set_error_literal(priv->catch_error, SPICE_CLIENT_ERROR,<br>
> -                                SPICE_CLIENT_ERROR_FAILED, msg);<br>
> -        return;<br>
> -    }<br>
> +        return FALSE;<br>
>  <br>
> -    switch (level) {<br>
> -        case usbredirparser_error:<br>
> -            g_critical("%s", msg);<br>
> -            break;<br>
> -        case usbredirparser_warning:<br>
> -            g_warning("%s", msg);<br>
> -            break;<br>
> -        default:<br>
> -            CHANNEL_DEBUG(channel, "%s", msg);<br>
> -    }<br>
> +    return TRUE;<br>
>  }<br>
>  <br>
> -static int usbredir_read_callback(void *user_data, uint8_t *data, int count)<br>
> +static void usbredir_error(void *user_data, const char *msg)<br>
>  {<br>
>      SpiceUsbredirChannel *channel = user_data;<br>
>      SpiceUsbredirChannelPrivate *priv = channel->priv;<br>
>  <br>
> -    count = MIN(priv->read_buf_size, count);<br>
> -<br>
> -    if (count != 0) {<br>
> -        memcpy(data, priv->read_buf, count);<br>
> -    }<br>
> -<br>
> -    priv->read_buf_size -= count;<br>
> -    if (priv->read_buf_size) {<br>
> -        priv->read_buf += count;<br>
> -    } else {<br>
> -        priv->read_buf = NULL;<br>
> +    if (priv->catch_error) {<br>
> +        g_set_error_literal(priv->catch_error, SPICE_CLIENT_ERROR,<br>
> +            SPICE_CLIENT_ERROR_FAILED, msg);<br>
> +        /*<br>
> +           if catch_error was set once, it is correct to prevent<br>
> +           further attempts to set it, they will overwrite already<br>
> +           used GError, cause memory leaks and GLib warnings.<br>
> +        */<br>
> +        priv->catch_error = NULL;<br>
>      }<br>
> -<br>
> -    return count;<br>
>  }<br>
>  <br>
>  static void usbredir_free_write_cb_data(uint8_t *data, void *user_data)<br>
> @@ -669,7 +603,7 @@ static void usbredir_free_write_cb_data(uint8_t *data, void *user_data)<br>
>      SpiceUsbredirChannel *channel = user_data;<br>
>      SpiceUsbredirChannelPrivate *priv = channel->priv;<br>
>  <br>
> -    usbredirhost_free_write_buffer(priv->host, data);<br>
> +    spice_usb_backend_return_write_data(priv->host, data);<br>
>  }<br>
>  <br>
>  #ifdef USE_LZ4<br>
> @@ -741,7 +675,7 @@ static int usbredir_write_callback(void *user_data, uint8_t *data, int count)<br>
>  <br>
>  #ifdef USE_LZ4<br>
>      if (try_write_compress_LZ4(channel, data, count)) {<br>
> -        usbredirhost_free_write_buffer(channel->priv->host, data);<br>
> +        spice_usb_backend_return_write_data(channel->priv->host, data);<br>
>          return count;<br>
>      }<br>
>  #endif<br>
> @@ -754,15 +688,6 @@ static int usbredir_write_callback(void *user_data, uint8_t *data, int count)<br>
>      return count;<br>
>  }<br>
>  <br>
> -static void *usbredir_alloc_lock(void) {<br>
> -    GMutex *mutex;<br>
> -<br>
> -    mutex = g_new0(GMutex, 1);<br>
> -    g_mutex_init(mutex);<br>
> -<br>
> -    return mutex;<br>
> -}<br>
> -<br>
>  G_GNUC_INTERNAL<br>
>  void spice_usbredir_channel_lock(SpiceUsbredirChannel *channel)<br>
>  {<br>
> @@ -775,25 +700,6 @@ void spice_usbredir_channel_unlock(SpiceUsbredirChannel *channel)<br>
>      g_mutex_unlock(&channel->priv->device_connect_mutex);<br>
>  }<br>
>  <br>
> -static void usbredir_lock_lock(void *user_data) {<br>
> -    GMutex *mutex = user_data;<br>
> -<br>
> -    g_mutex_lock(mutex);<br>
> -}<br>
> -<br>
> -static void usbredir_unlock_lock(void *user_data) {<br>
> -    GMutex *mutex = user_data;<br>
> -<br>
> -    g_mutex_unlock(mutex);<br>
> -}<br>
> -<br>
> -static void usbredir_free_lock(void *user_data) {<br>
> -    GMutex *mutex = user_data;<br>
> -<br>
> -    g_mutex_clear(mutex);<br>
> -    g_free(mutex);<br>
> -}<br>
> -<br>
>  /* --------------------------------------------------------------------- */<br>
>  <br>
>  typedef struct device_error_data {<br>
> @@ -832,7 +738,7 @@ static void spice_usbredir_channel_up(SpiceChannel *c)<br>
>  <br>
>      g_return_if_fail(priv->host != NULL);<br>
>      /* Flush any pending writes */<br>
> -    usbredirhost_write_guest_data(priv->host);<br>
> +    spice_usb_backend_channel_up(priv->host);<br>
>  }<br>
>  <br>
>  static int try_handle_compressed_msg(SpiceMsgCompressedData *compressed_data_msg,<br>
> @@ -882,26 +788,20 @@ static void usbredir_handle_msg(SpiceChannel *c, SpiceMsgIn *in)<br>
>  <br>
>      g_return_if_fail(priv->host != NULL);<br>
>  <br>
> -    /* No recursion allowed! */<br>
> -    g_return_if_fail(priv->read_buf == NULL);<br>
> -<br>
>      if (spice_msg_in_type(in) == SPICE_MSG_SPICEVMC_COMPRESSED_DATA) {<br>
>          SpiceMsgCompressedData *compressed_data_msg = spice_msg_in_parsed(in);<br>
>          if (try_handle_compressed_msg(compressed_data_msg, &buf, &size)) {<br>
> -            priv->read_buf_size = size;<br>
> -            priv->read_buf = buf;<br>
> +            /* uncompressed ok*/<br>
>          } else {<br>
> -            r = usbredirhost_read_parse_error;<br>
> +            r = USB_REDIR_ERROR_READ_PARSE;<br>
>          }<br>
>      } else { /* Regular SPICE_MSG_SPICEVMC_DATA msg */<br>
>          buf = spice_msg_in_raw(in, &size);<br>
> -        priv->read_buf_size = size;<br>
> -        priv->read_buf = buf;<br>
>      }<br>
>  <br>
>      spice_usbredir_channel_lock(channel);<br>
>      if (r == 0)<br>
> -        r = usbredirhost_read_guest_data(priv->host);<br>
> +        r = spice_usb_backend_provide_read_data(priv->host, buf, size);<br>
>      if (r != 0) {<br>
>          SpiceUsbDevice *spice_device = priv->spice_device;<br>
>          device_error_data err_data;<br>
> @@ -915,16 +815,16 @@ static void usbredir_handle_msg(SpiceChannel *c, SpiceMsgIn *in)<br>
>  <br>
>          desc = spice_usb_device_get_description(spice_device, NULL);<br>
>          switch (r) {<br>
> -        case usbredirhost_read_parse_error:<br>
> +        case USB_REDIR_ERROR_READ_PARSE:<br>
>              err = g_error_new(SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,<br>
>                                _("usbredir protocol parse error for %s"), desc);<br>
>              break;<br>
> -        case usbredirhost_read_device_rejected:<br>
> +        case USB_REDIR_ERROR_DEV_REJECTED:<br>
>              err = g_error_new(SPICE_CLIENT_ERROR,<br>
>                                SPICE_CLIENT_ERROR_USB_DEVICE_REJECTED,<br>
>                                _("%s rejected by host"), desc);<br>
>              break;<br>
> -        case usbredirhost_read_device_lost:<br>
> +        case USB_REDIR_ERROR_DEV_LOST:<br>
>              err = g_error_new(SPICE_CLIENT_ERROR,<br>
>                                SPICE_CLIENT_ERROR_USB_DEVICE_LOST,<br>
>                                _("%s disconnected (fatal IO error)"), desc);<br>
> diff --git a/src/meson.build b/src/meson.build<br>
> index dcf4dcc..62a4c51 100644<br>
> --- a/src/meson.build<br>
> +++ b/src/meson.build<br>
> @@ -80,6 +80,7 @@ spice_client_glib_introspection_sources = [<br>
>    'spice-session.c',<br>
>    'spice-util.c',<br>
>    'usb-device-manager.c',<br>
> +  'usb-backend-common.c',<br>
<br>
This should go in spice_client_glib_sources together with usb-backend.h<br>
imo as suggested here <a href="https://lists.freedesktop.org/archives/spice-devel/2018-September/045818.html" rel="noreferrer" target="_blank">https://lists.freedesktop.org/archives/spice-devel/2018-September/045818.html</a><br>
<br></blockquote><div><br></div><div>This can be done in V3</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
>  ]<br>
>  <br>
>  spice_client_glib_sources = [<br>
> diff --git a/src/usb-backend-common.c b/src/usb-backend-common.c<br>
> new file mode 100644<br>
> index 0000000..6f61193<br>
> --- /dev/null<br>
> +++ b/src/usb-backend-common.c<br>
> @@ -0,0 +1,688 @@<br>
> +/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */<br>
> +/*<br>
> +    Copyright (C) 2012-2018 Red Hat, Inc.<br>
> +<br>
> +    Red Hat Authors:<br>
> +    Yuri Benditovich<<a href="mailto:ybendito@redhat.com" target="_blank">ybendito@redhat.com</a>><br>
> +    Hans de Goede <<a href="mailto:hdegoede@redhat.com" target="_blank">hdegoede@redhat.com</a>><br>
> +<br>
> +    This library is free software; you can redistribute it and/or<br>
> +    modify it under the terms of the GNU Lesser General Public<br>
> +    License as published by the Free Software Foundation; either<br>
> +    version 2.1 of the License, or (at your option) any later version.<br>
> +<br>
> +    This library is distributed in the hope that it will be useful,<br>
> +    but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
> +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU<br>
> +    Lesser General Public License for more details.<br>
> +<br>
> +    You should have received a copy of the GNU Lesser General Public<br>
> +    License along with this library; if not, see <<a href="http://www.gnu.org/licenses/" rel="noreferrer" target="_blank">http://www.gnu.org/licenses/</a>>.<br>
> +*/<br>
> +<br>
> +#include "config.h"<br>
> +<br>
> +#ifdef USE_USBREDIR<br>
> +<br>
> +#include <glib-object.h><br>
> +#include <inttypes.h><br>
> +#include <gio/gio.h><br>
> +#include <errno.h><br>
> +#include <libusb.h><br>
> +#include <string.h><br>
> +#include <fcntl.h><br>
> +#include "usbredirhost.h"<br>
> +#include "usbredirparser.h"<br>
> +#include "spice-util.h"<br>
> +#include "usb-backend.h"<br>
> +#if defined(G_OS_WIN32)<br>
> +#include <windows.h><br>
> +#include "win-usb-dev.h"<br>
> +#else<br>
> +#include <sys/stat.h><br>
> +#include <sys/ioctl.h><br>
> +#include <linux/fs.h><br>
> +#endif<br>
> +<br>
> +//#define LOUD_DEBUG SPICE_DEBUG<br>
> +#define LOUD_DEBUG(x, ...)<br>
> +<br>
> +struct _SpiceUsbBackendDevice<br>
> +{<br>
> +    union<br>
> +    {<br>
> +        libusb_device *libusb_device;<br>
<br>
In _SpiceUsbDeviceManagerPrivate, you replaced<br>
#ifndef G_OS_WIN32<br>
    libusb_device *libdev;<br>
#endif<br>
<br>
with<br>
<br>
#ifndef G_OS_WIN32<br>
    SpiceUsbBackendDevice *bdev;<br>
#endif<br>
<br>
The #ifdef is there because of this comment in<br>
spice_usb_device_manager_device_to_bdev:<br>
<br>
  /*<br>
   * On win32 we need to do this the hard and slow way, by asking libusb to<br>
   * re-enumerate all devices and then finding a matching device.<br>
   * We cannot cache the libusb_device like we do under Linux since the<br>
   * driver swap we do under windows invalidates the cached libdev.<br>
   */<br>
<br>
After your patch, spice_usb_device_manager_device_to_bdev is no longer<br>
at the right level of indirection imo, it looks up the 'bdev' when<br>
needed on Windows in usb-device-manager.c, but my understanding of that<br>
comment is that any libusb call within SpiceUsbBackendDevice should not<br>
use a cached libusb_device?<br>
<br>
<br></blockquote><div><br></div><div>As fas as I understand, there is no change in the level of indirection.</div><div>Previously was:</div><div>On Linux: usb-device-manager receives libusb device on hotplug indication and uses it until the device disappears.</div><div>On Windows: each time the usb-device-manager needs the libusb device it takes fresh list of libusb devices and finds one according to known device properties.</div><div><br></div><div>Now:</div><div><div>On Linux: usb-device-manager receives backend device wrapping libusb device on hotplug indication and uses it until the device disappears</div><div>On Windows: each time the usb-device-manager needs the backend device it takes fresh list of new backend devices and finds one according to known device properties.</div><div>So, the backend device is always fresh one and it always have fresh libusb device.</div><br class="gmail-Apple-interchange-newline"></div><div>This can be simplified with removal of all these lookups in the usb-device-manager (and I already illustrated how) but after this commit is done.</div><div>If from your point of view this is the requirement to existing commit, please let me know (then we will need to reevaluate our plans and our ability to deliver the patches in reasonable time). </div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
> +        void *msc;<br>
> +    } d;<br>
> +    gboolean is_libusb;<br>
> +    gint ref_count;<br>
> +    SpiceUsbBackendChannel *attached_to;<br>
<br>
You don't need 'msc' just yet, nor 'is_libusb', and I'm not sure about<br>
'attached_to'<br></blockquote><div><br></div><div>msc can be changed to reserved if you do not like it so much.</div><div>removal of is_libusb makes further merges harder (and current commit is only prerequisite for further merge).</div><div>attached_to is useful for debugging/support</div><div>Please let me know whether this is a requirement.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
> +    UsbDeviceInformation device_info;<br>
> +};<br>
> +<br>
> +struct _SpiceUsbBackend<br>
> +{<br>
> +    libusb_context *libusb_context;<br>
> +    usb_hot_plug_callback hotplug_callback;<br>
<br>
"usb_hotplug_callback"<br></blockquote><div><br></div><div>This can be changed in V3</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
> +    void *hotplug_user_data;<br>
> +    libusb_hotplug_callback_handle hotplug_handle;<br>
> +};<br>
> +<br>
> +struct _SpiceUsbBackendChannel<br>
> +{<br>
> +    struct usbredirhost *usbredirhost;<br>
> +    uint8_t *read_buf;<br>
> +    int read_buf_size;<br>
> +    struct usbredirfilter_rule *rules;<br>
> +    int rules_count;<br>
> +    SpiceUsbBackendDevice *attached;<br>
<br>
I don't think this is used.<br></blockquote><div><br></div><div>The same as is_libusb.</div><div>In general even now the reference to attached device and attached channel are useful for debugging and parsing logs and dumps.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
> +    SpiceUsbBackendChannelInitData channel_data;<br>
<br>
I don't think 'channel_data' is a much more descriptive than the<br>
previously used 'data'. SpiceUsbBackendChannelInitData is a bunch of<br>
vfuncs/callbacks, so maybe use one of these words in the naming?<br></blockquote><div><br></div><div>It can be renamed in V3, please provide the name you agree with.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
> +};<br>
> +<br>
> +// it's unclear why we use this procedure instead of libusb_error_name,<br>
> +// which by definition supports any error that libusb returns<br>
<br>
Why not add a commit before this one switching libusb_error_name() then?<br></blockquote><div><br></div><div>Because this looks strange: first change it in 10 places then remove all of them in next commit.</div><div>Also because this make the merge very difficult (for me).</div><div>I can remove this note. I can use libusb_error_name instead of this procedure.</div><div>Please let me know what you prefer.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
> +static const char *spice_usbutil_libusb_strerror(enum libusb_error error_code)<br>
> +{<br>
> +    switch (error_code) {<br>
> +    case LIBUSB_SUCCESS:<br>
> +        return "Success";<br>
> +    case LIBUSB_ERROR_IO:<br>
> +        return "Input/output error";<br>
> +    case LIBUSB_ERROR_INVALID_PARAM:<br>
> +        return "Invalid parameter";<br>
> +    case LIBUSB_ERROR_ACCESS:<br>
> +        return "Access denied (insufficient permissions)";<br>
> +    case LIBUSB_ERROR_NO_DEVICE:<br>
> +        return "No such device (it may have been disconnected)";<br>
> +    case LIBUSB_ERROR_NOT_FOUND:<br>
> +        return "Entity not found";<br>
> +    case LIBUSB_ERROR_BUSY:<br>
> +        return "Resource busy";<br>
> +    case LIBUSB_ERROR_TIMEOUT:<br>
> +        return "Operation timed out";<br>
> +    case LIBUSB_ERROR_OVERFLOW:<br>
> +        return "Overflow";<br>
> +    case LIBUSB_ERROR_PIPE:<br>
> +        return "Pipe error";<br>
> +    case LIBUSB_ERROR_INTERRUPTED:<br>
> +        return "System call interrupted (perhaps due to signal)";<br>
> +    case LIBUSB_ERROR_NO_MEM:<br>
> +        return "Insufficient memory";<br>
> +    case LIBUSB_ERROR_NOT_SUPPORTED:<br>
> +        return "Operation not supported or unimplemented on this platform";<br>
> +    case LIBUSB_ERROR_OTHER:<br>
> +        return "Other error";<br>
> +    }<br>
> +    return "Unknown error";<br>
> +}<br>
> +<br>
> +// lock functions for usbredirhost and usbredirparser<br>
> +static void *usbredir_alloc_lock(void) {<br>
> +    GMutex *mutex;<br>
> +<br>
> +    mutex = g_new0(GMutex, 1);<br>
> +    g_mutex_init(mutex);<br>
> +<br>
> +    return mutex;<br>
> +}<br>
> +<br>
> +static void usbredir_free_lock(void *user_data) {<br>
> +    GMutex *mutex = user_data;<br>
> +<br>
> +    g_mutex_clear(mutex);<br>
> +    g_free(mutex);<br>
> +}<br>
> +<br>
> +static void usbredir_lock_lock(void *user_data) {<br>
> +    GMutex *mutex = user_data;<br>
> +<br>
> +    g_mutex_lock(mutex);<br>
> +}<br>
> +<br>
> +static void usbredir_unlock_lock(void *user_data) {<br>
> +    GMutex *mutex = user_data;<br>
> +<br>
> +    g_mutex_unlock(mutex);<br>
> +}<br>
> +<br>
> +static uint8_t is_libusb_isochronous(libusb_device *libdev)<br>
> +{<br>
> +    struct libusb_config_descriptor *conf_desc;<br>
> +    uint8_t isoc_found = FALSE;<br>
> +    gint i, j, k;<br>
> +<br>
> +    g_return_val_if_fail(libdev != NULL, 0);<br>
> +<br>
> +    if (libusb_get_active_config_descriptor(libdev, &conf_desc) != 0) {<br>
> +        g_return_val_if_reached(0);<br>
> +    }<br>
> +<br>
> +    for (i = 0; !isoc_found && i < conf_desc->bNumInterfaces; i++) {<br>
> +        for (j = 0; !isoc_found && j < conf_desc->interface[i].num_altsetting; j++) {<br>
> +            for (k = 0; !isoc_found && k < conf_desc->interface[i].altsetting[j].bNumEndpoints;k++) {<br>
> +                gint attributes = conf_desc->interface[i].altsetting[j].endpoint[k].bmAttributes;<br>
> +                gint type = attributes & LIBUSB_TRANSFER_TYPE_MASK;<br>
> +                if (type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS)<br>
> +                    isoc_found = TRUE;<br>
> +            }<br>
> +        }<br>
> +    }<br>
> +<br>
> +    libusb_free_config_descriptor(conf_desc);<br>
> +    return isoc_found;<br>
> +}<br>
> +<br>
> +static gboolean fill_usb_info(SpiceUsbBackendDevice *bdev)<br>
> +{<br>
> +    UsbDeviceInformation *info = &bdev->device_info;<br>
> +<br>
> +    if (bdev->is_libusb)<br>
> +    {<br>
> +        struct libusb_device_descriptor desc;<br>
> +        libusb_device *libdev = bdev->d.libusb_device;<br>
> +        int res = libusb_get_device_descriptor(libdev, &desc);<br>
> +        info->bus = libusb_get_bus_number(libdev);<br>
> +        info->address = libusb_get_device_address(libdev);<br>
> +        if (res < 0) {<br>
> +            g_warning("cannot get device descriptor for (%p) %d.%d",<br>
> +                libdev, info->bus, info->address);<br>
> +            return FALSE;<br>
> +        }<br>
> +        info->vid = desc.idVendor;<br>
> +        info->pid = desc.idProduct;<br>
> +        info->class = desc.bDeviceClass;<br>
> +        info->subclass = desc.bDeviceSubClass;<br>
> +        info->protocol = desc.bDeviceProtocol;<br>
> +        info->isochronous = is_libusb_isochronous(libdev);<br>
> +    }<br>
> +    return TRUE;<br>
> +}<br>
> +<br>
> +static SpiceUsbBackendDevice *allocate_backend_device(libusb_device *libdev)<br>
> +{<br>
> +    SpiceUsbBackendDevice *dev = g_new0(SpiceUsbBackendDevice, 1);<br>
> +    dev->is_libusb = 1;<br>
> +    dev->ref_count = 1;<br>
> +    dev->d.libusb_device = libdev;<br>
> +    if (!fill_usb_info(dev)) {<br>
> +        g_free(dev);<br>
> +        dev = NULL;<br>
> +    }<br>
> +    return dev;<br>
> +}<br>
> +<br>
> +/* Note that this function must be re-entrant safe, as it can get called<br>
> +from both the main thread as well as from the usb event handling thread */<br>
> +static void usbredir_write_flush_callback(void *user_data)<br>
> +{<br>
> +    SpiceUsbBackendChannel *ch = user_data;<br>
> +    gboolean ok = ch->channel_data.is_channel_ready(ch->channel_data.user_data);<br>
> +    if (ok && ch->usbredirhost) {<br>
> +        SPICE_DEBUG("%s ch %p -> usbredirhost", __FUNCTION__, ch);<br>
> +        usbredirhost_write_guest_data(ch->usbredirhost);<br>
> +    } else {<br>
> +        SPICE_DEBUG("%s ch %p (not ready)", __FUNCTION__, ch);<br>
> +    }<br>
> +}<br>
> +<br>
> +SpiceUsbBackend *spice_usb_backend_initialize(GError **error)<br>
> +{<br>
> +    int rc;<br>
> +    SpiceUsbBackend *be;<br>
> +    SPICE_DEBUG("%s >>", __FUNCTION__);<br>
> +    be = (SpiceUsbBackend *)g_new0(SpiceUsbBackend, 1);<br>
> +    rc = libusb_init(&be->libusb_context);<br>
> +    if (rc < 0) {<br>
> +        const char *desc = spice_usbutil_libusb_strerror(rc);<br>
> +        g_warning("Error initializing LIBUSB support: %s [%i]", desc, rc);<br>
> +        g_set_error(error, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,<br>
> +            "Error initializing LIBUSB support: %s [%i]", desc, rc);<br>
<br>
We usually align with the opening (<br></blockquote><div><br></div><div>I can try in V3</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
> +        g_free(be);<br>
> +        be = NULL;<br>
> +    } else {<br>
> +#ifdef G_OS_WIN32<br>
> +#if LIBUSB_API_VERSION >= 0x01000106<br>
> +    libusb_set_option(be->libusb_context, LIBUSB_OPTION_USE_USBDK);<br>
<br>
This could be indented one more level.<br></blockquote><div><br></div><div>This can be fixed in V3</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
> +#endif<br>
> +#endif<br>
> +    }<br>
> +    SPICE_DEBUG("%s <<", __FUNCTION__);<br>
> +    return be;<br>
> +}<br>
> +<br>
> +gboolean spice_usb_backend_handle_events(SpiceUsbBackend *be)<br>
> +{<br>
> +    SPICE_DEBUG("%s >>", __FUNCTION__);<br>
> +    gboolean ok = FALSE;<br>
> +    if (be->libusb_context) {<br>
> +        SPICE_DEBUG("%s >> libusb", __FUNCTION__);<br>
> +        int res = libusb_handle_events(be->libusb_context);<br>
> +        ok = res == 0;<br>
> +        if (res && res != LIBUSB_ERROR_INTERRUPTED) {<br>
> +            const char *desc = spice_usbutil_libusb_strerror(res);<br>
> +            g_warning("Error handling USB events: %s [%i]", desc, res);<br>
> +            ok = FALSE;<br>
<br>
You don't really need 'ok' in that method, just return FALSE; here...<br></blockquote><div><br></div><div>Please let me know whether this change is mandatory.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
> +        }<br>
> +    }<br>
> +    SPICE_DEBUG("%s << %s %d", __FUNCTION__,<br>
> +        be->libusb_context ? "libusb" : "no libusb", ok);<br>
> +    return ok;<br>
<br>
... and return TRUE; there (and I'm not sure how useful the SPICE_DEBUG<br>
are going to be?)<br>
<br></blockquote><div><br></div><div><div>Please let me know whether this change is mandatory.</div>Reformatting the code will make further merger more difficult.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> +}<br>
> +<br>
> +static int LIBUSB_CALL hotplug_callback(libusb_context *ctx,<br>
> +                                        libusb_device *device,<br>
> +                                        libusb_hotplug_event event,<br>
> +                                        void *user_data)<br>
> +{<br>
> +    SpiceUsbBackend *be = (SpiceUsbBackend *)user_data;<br>
> +    if (be->hotplug_callback) {<br>
> +        SpiceUsbBackendDevice *dev;<br>
> +        gboolean val = event == LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED;<br>
> +        dev = allocate_backend_device(device);<br>
> +        if (dev) {<br>
> +            SPICE_DEBUG("created dev %p, usblib dev %p", dev, device);<br>
> +            libusb_ref_device(device);<br>
> +            be->hotplug_callback(be->hotplug_user_data, dev, val);<br>
> +            spice_usb_backend_device_release(dev);<br>
> +        }<br>
> +    }<br>
> +    return 0;<br>
> +}<br>
> +<br>
> +gboolean spice_usb_backend_handle_hotplug(SpiceUsbBackend *be,<br>
> +                                          void *user_data,<br>
> +                                          usb_hot_plug_callback proc)<br>
<br>
Why not<br>
spice_usb_backend_enable_hotplug/spice_usb_backend_disable_hotplug?<br>
(and now that we have a proper SpiceUsbBackend struct wrapping the<br>
libusb stuff, I wonder if it would not make sense to just turn it into a<br>
gobject, and emit signals when we get new devices).<br>
<br></blockquote><div><br></div><div>Regarding 'why not to split it to two procedures': this is point of personal preference.</div><div>If the splitting is mandatory, please let me know, I'll do that in V3.</div><div>Regarding turning it to GObject - this will discard entire patch and spawn completely</div><div>different activity that requires additional debugging. Is this is mandatory, we will need to</div><div>reevaluate our ability to deliver the patches.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> +{<br>
> +    int rc;<br>
> +    g_return_val_if_fail(be != NULL, FALSE);<br>
> +    if (!proc) {<br>
> +        if (be->hotplug_handle) {<br>
> +            libusb_hotplug_deregister_callback(be->libusb_context, be->hotplug_handle);<br>
> +            be->hotplug_handle = 0;<br>
> +        }<br>
> +        be->hotplug_callback = proc;<br>
> +        return TRUE;<br>
> +    }<br>
> +<br>
> +    be->hotplug_callback = proc;<br>
> +    be->hotplug_user_data = user_data;<br>
> +    rc = libusb_hotplug_register_callback(be->libusb_context,<br>
> +        LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT,<br>
> +        LIBUSB_HOTPLUG_ENUMERATE, LIBUSB_HOTPLUG_MATCH_ANY,<br>
> +        LIBUSB_HOTPLUG_MATCH_ANY, LIBUSB_HOTPLUG_MATCH_ANY,<br>
> +        hotplug_callback, be, &be->hotplug_handle);<br>
> +    if (rc != LIBUSB_SUCCESS) {<br>
> +        const char *desc = spice_usbutil_libusb_strerror(rc);<br>
> +        g_warning("Error initializing USB hotplug support: %s [%i]", desc, rc);<br>
> +        be->hotplug_callback = NULL;<br>
> +        return FALSE;<br>
> +    }<br>
> +    return TRUE;<br>
> +}<br>
> +<br>
> +void spice_usb_backend_finalize(SpiceUsbBackend *be)<br>
> +{<br>
> +    g_return_if_fail(be != NULL);<br>
> +    SPICE_DEBUG("%s >>", __FUNCTION__);<br>
> +    if (be->libusb_context) {<br>
> +        libusb_exit(be->libusb_context);<br>
> +    }<br>
> +    g_free(be);<br>
> +    SPICE_DEBUG("%s <<", __FUNCTION__);<br>
> +}<br>
> +<br>
> +SpiceUsbBackendDevice **spice_usb_backend_get_device_list(SpiceUsbBackend *be)<br>
> +{<br>
> +    LOUD_DEBUG("%s >>", __FUNCTION__);<br>
> +    libusb_device **devlist = NULL, **dev;<br>
> +    SpiceUsbBackendDevice *d, **list;<br>
> +<br>
> +    int n = 0, index;<br>
> +<br>
> +    if (be && be->libusb_context) {<br>
> +        libusb_get_device_list(be->libusb_context, &devlist);<br>
> +    }<br>
> +<br>
> +    // add all the libusb device that not present in our list<br>
> +    for (dev = devlist; dev && *dev; dev++) {<br>
> +        n++;<br>
> +    }<br>
> +<br>
> +    list = g_new0(SpiceUsbBackendDevice*, n + 1);<br>
> +<br>
> +    index = 0;<br>
> +<br>
> +    for (dev = devlist; dev && *dev; dev++) {<br>
> +        d = allocate_backend_device(*dev);<br>
> +        if (!d) {<br>
> +            libusb_unref_device(*dev);<br>
> +        } else {<br>
> +            SPICE_DEBUG("created dev %p, usblib dev %p", d, *dev);<br>
> +            list[index++] = d;<br>
> +        }<br>
> +    }<br>
> +<br>
> +    if (devlist) {<br>
> +        libusb_free_device_list(devlist, 0);<br>
> +    }<br>
> +<br>
> +    LOUD_DEBUG("%s <<", __FUNCTION__);<br>
> +    return list;<br>
> +}<br>
<br>
I still think this could be made slightly simpler with GArray/GPtrArray.<br></blockquote><div><br></div><div>I do not see any advantage at all, just additional change that makes further merges more complicated.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
> +<br>
> +gboolean spice_usb_backend_device_is_hub(SpiceUsbBackendDevice *dev)<br>
> +{<br>
> +    return dev->device_info.class == LIBUSB_CLASS_HUB;<br>
> +}<br>
> +<br>
> +const UsbDeviceInformation* spice_usb_backend_device_get_info(SpiceUsbBackendDevice *dev)<br>
> +{<br>
> +    return &dev->device_info;<br>
> +}<br>
> +<br>
> +gboolean spice_usb_backend_devices_same(SpiceUsbBackendDevice *dev1,<br>
> +                                        SpiceUsbBackendDevice *dev2)<br>
> +{<br>
> +    if (dev1->is_libusb != dev2->is_libusb) {<br>
> +        return FALSE;<br>
> +    }<br>
> +    if (dev1->is_libusb) {<br>
> +        return dev1->d.libusb_device == dev2->d.libusb_device;<br>
> +    }<br>
> +    return dev1 == dev2;<br>
> +}<br>
> +<br>
> +gconstpointer spice_usb_backend_device_get_libdev(SpiceUsbBackendDevice *dev)<br>
> +{<br>
> +    if (dev->is_libusb) {<br>
> +        return dev->d.libusb_device;<br>
> +    }<br>
> +    return NULL;<br>
> +}<br>
> +<br>
> +void spice_usb_backend_free_device_list(SpiceUsbBackendDevice **devlist)<br>
> +{<br>
> +    LOUD_DEBUG("%s >>", __FUNCTION__);<br>
> +    SpiceUsbBackendDevice **dev;<br>
> +    for (dev = devlist; *dev; dev++) {<br>
> +        SpiceUsbBackendDevice *d = *dev;<br>
> +        spice_usb_backend_device_release(d);<br>
> +    }<br>
> +    g_free(devlist);<br>
> +    LOUD_DEBUG("%s <<", __FUNCTION__);<br>
> +}<br>
> +<br>
> +void spice_usb_backend_device_acquire(SpiceUsbBackendDevice *dev)<br>
> +{<br>
> +    LOUD_DEBUG("%s >> %p", __FUNCTION__, dev);<br>
> +    g_atomic_int_inc(&dev->ref_count);<br>
> +}<br>
> +<br>
> +void spice_usb_backend_device_release(SpiceUsbBackendDevice *dev)<br>
> +{<br>
> +    LOUD_DEBUG("%s >> %p(%d)", __FUNCTION__, dev, dev->ref_count);<br>
> +    if (g_atomic_int_dec_and_test(&dev->ref_count)) {<br>
> +        if (dev->is_libusb) {<br>
> +            libusb_unref_device(dev->d.libusb_device);<br>
> +            LOUD_DEBUG("%s freeing %p (libusb %p)", __FUNCTION__, dev, dev->d.libusb_device);<br>
> +            g_free(dev);<br>
> +        }<br>
> +    }<br>
> +}<br>
> +<br>
> +int spice_usb_backend_device_check_filter(<br>
> +    SpiceUsbBackendDevice *dev,<br>
> +    const struct usbredirfilter_rule *rules,<br>
> +    int count)<br>
> +{<br>
> +    if (dev->is_libusb) {<br>
> +        return usbredirhost_check_device_filter(<br>
> +            rules, count, dev->d.libusb_device, 0);<br>
> +    }<br>
> +    return -1;<br>
> +}<br>
> +<br>
> +static int usbredir_read_callback(void *user_data, uint8_t *data, int count)<br>
> +{<br>
> +    SpiceUsbBackendChannel *ch = user_data;<br>
> +<br>
> +    count = MIN(ch->read_buf_size, count);<br>
> +<br>
> +    if (count != 0) {<br>
> +        memcpy(data, ch->read_buf, count);<br>
> +    }<br>
> +<br>
> +    ch->read_buf_size -= count;<br>
> +    if (ch->read_buf_size) {<br>
> +        ch->read_buf += count;<br>
> +    }<br>
> +    else {<br>
> +        ch->read_buf = NULL;<br>
> +    }<br>
> +    SPICE_DEBUG("%s ch %p, %d bytes", __FUNCTION__, ch, count);<br>
> +<br>
> +    return count;<br>
> +}<br>
> +<br>
> +static const char *strip_usbredir_prefix(const char *msg)<br>
> +{<br>
> +    if (strncmp(msg, "usbredirhost: ", 14) == 0) {<br>
> +        msg += 14;<br>
> +    }<br>
> +    return msg;<br>
> +}<br>
> +<br>
> +static void usbredir_log(void *user_data, int level, const char *msg)<br>
> +{<br>
> +    SpiceUsbBackendChannel *ch = (SpiceUsbBackendChannel *)user_data;<br>
> +    const char *stripped_msg = strip_usbredir_prefix(msg);<br>
> +    switch (level) {<br>
> +    case usbredirparser_error:<br>
> +        g_critical("%s", msg);<br>
> +        ch->channel_data.on_error(ch->channel_data.user_data, stripped_msg);<br>
> +        break;<br>
> +    case usbredirparser_warning:<br>
> +        g_warning("%s", msg);<br>
> +        ch->channel_data.on_error(ch->channel_data.user_data, stripped_msg);<br>
> +        break;<br>
> +    default:<br>
> +        break;<br>
> +    }<br>
> +}<br>
> +<br>
> +static int usbredir_write_callback(void *user_data, uint8_t *data, int count)<br>
> +{<br>
> +    SpiceUsbBackendChannel *ch = user_data;<br>
> +    int res;<br>
> +    SPICE_DEBUG("%s ch %p, %d bytes", __FUNCTION__, ch, count);<br>
> +    res = ch->channel_data.write_callback(ch->channel_data.user_data, data, count);<br>
> +    return res;<br>
> +}<br>
> +<br>
> +#if USBREDIR_VERSION >= 0x000701<br>
> +static uint64_t usbredir_buffered_output_size_callback(void *user_data)<br>
> +{<br>
> +    SpiceUsbBackendChannel *ch = user_data;<br>
> +    return ch->channel_data.get_queue_size(ch->channel_data.user_data);<br>
> +}<br>
> +#endif<br>
> +<br>
> +int spice_usb_backend_provide_read_data(SpiceUsbBackendChannel *ch, uint8_t *data, int count)<br>
> +{<br>
> +    int res = 0;<br>
> +<br>
> +    g_return_val_if_fail(ch->read_buf == NULL, USB_REDIR_ERROR_READ_PARSE);<br>
> +<br>
> +    ch->read_buf = data;<br>
> +    ch->read_buf_size = count;<br>
> +    if (ch->usbredirhost) {<br>
> +        res = usbredirhost_read_guest_data(ch->usbredirhost);<br>
> +    } else {<br>
> +        res = USB_REDIR_ERROR_IO;<br>
> +    }<br>
> +    switch (res)<br>
> +    {<br>
> +    case usbredirhost_read_io_error:<br>
> +        res = USB_REDIR_ERROR_IO;<br>
> +        break;<br>
> +    case usbredirhost_read_parse_error:<br>
> +        res = USB_REDIR_ERROR_READ_PARSE;<br>
> +        break;<br>
> +    case usbredirhost_read_device_rejected:<br>
> +        res = USB_REDIR_ERROR_DEV_REJECTED;<br>
> +        break;<br>
> +    case usbredirhost_read_device_lost:<br>
> +        res = USB_REDIR_ERROR_DEV_LOST;<br>
> +        break;<br>
> +    }<br>
> +    SPICE_DEBUG("%s ch %p, %d bytes, res %d", __FUNCTION__, ch, count, res);<br>
> +<br>
> +    return res;<br>
> +}<br>
> +<br>
> +void spice_usb_backend_return_write_data(SpiceUsbBackendChannel *ch, void *data)<br>
> +{<br>
> +    if (ch->usbredirhost) {<br>
> +        SPICE_DEBUG("%s ch %p", __FUNCTION__, ch);<br>
> +        usbredirhost_free_write_buffer(ch->usbredirhost, data);<br>
> +    } else {<br>
> +        SPICE_DEBUG("%s ch %p - NOBODY TO CALL", __FUNCTION__, ch);<br>
> +    }<br>
> +}<br>
> +<br>
> +gboolean spice_usb_backend_channel_attach(SpiceUsbBackendChannel *ch,<br>
> +                                          SpiceUsbBackendDevice *dev,<br>
> +                                          GError **error)<br>
> +{<br>
> +    SPICE_DEBUG("%s >> ch %p, dev %p (was attached %p)", __FUNCTION__, ch, dev, ch->attached);<br>
> +    gboolean ok = FALSE;<br>
> +    if (!dev) {<br>
> +        return ok;<br>
> +    }<br>
> +<br>
> +    if (dev->is_libusb) {<br>
> +        libusb_device_handle *handle = NULL;<br>
> +        int rc = libusb_open(dev->d.libusb_device, &handle);<br>
> +        ok = rc == 0;<br>
> +        if (ok) {<br>
> +            rc = usbredirhost_set_device(ch->usbredirhost, handle);<br>
> +            if (rc) {<br>
> +                SPICE_DEBUG("%s ch %p, dev %p usbredirhost error %d", __FUNCTION__, ch, dev, rc);<br>
> +                ok = FALSE;<br>
> +            } else {<br>
> +                ch->attached = dev;<br>
> +                dev->attached_to = ch;<br>
> +            }<br>
> +        } else {<br>
> +            const char *desc = spice_usbutil_libusb_strerror(rc);<br>
> +            g_set_error(error, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,<br>
> +                "Error libusb_open: %s [%i]", desc, rc);<br>
> +        }<br>
> +    }<br>
> +<br>
> +    return ok;<br>
> +}<br>
> +<br>
> +void spice_usb_backend_channel_detach(SpiceUsbBackendChannel *ch)<br>
> +{<br>
> +    SPICE_DEBUG("%s >> ch %p, was attached %p", __FUNCTION__, ch, ch->attached);<br>
> +    if (!ch->attached) {<br>
> +        SPICE_DEBUG("%s: nothing to detach", __FUNCTION__);<br>
> +        return;<br>
> +    }<br>
> +    if (ch->usbredirhost) {<br>
> +        // it will call libusb_close internally<br>
> +        usbredirhost_set_device(ch->usbredirhost, NULL);<br>
> +    }<br>
> +    SPICE_DEBUG("%s ch %p, detach done", __FUNCTION__, ch);<br>
> +    ch->attached->attached_to = NULL;<br>
> +    ch->attached = NULL;<br>
> +}<br>
> +<br>
> +SpiceUsbBackendChannel *spice_usb_backend_channel_initialize(SpiceUsbBackend *be,<br>
> +                                                             const SpiceUsbBackendChannelInitData *init_data)<br>
> +{<br>
> +    SpiceUsbBackendChannel *ch = g_new0(SpiceUsbBackendChannel, 1);<br>
> +    SPICE_DEBUG("%s >>", __FUNCTION__);<br>
> +    ch->channel_data = *init_data;<br>
> +    if (be->libusb_context) {<br>
<br>
You probably can just have g_return_val_if_fail(be->libusb_context != NULL, NULL) at the very beginning of that method,<br>
and only do the allocation after that.<br></blockquote><div><br></div><div>Please let me know whether this is mandatory.</div><div>I can do that but then later it should be rewritten again.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
> +        ch->usbredirhost = usbredirhost_open_full(<br>
> +            be->libusb_context,<br>
> +            NULL,<br>
> +            usbredir_log,<br>
> +            usbredir_read_callback,<br>
> +            usbredir_write_callback,<br>
> +            usbredir_write_flush_callback,<br>
> +            usbredir_alloc_lock,<br>
> +            usbredir_lock_lock,<br>
> +            usbredir_unlock_lock,<br>
> +            usbredir_free_lock,<br>
> +            ch, PACKAGE_STRING,<br>
> +            spice_util_get_debug() ? usbredirparser_debug : usbredirparser_warning,<br>
> +            usbredirhost_fl_write_cb_owns_buffer);<br>
> +        g_warn_if_fail(ch->usbredirhost != NULL);<br>
> +    }<br>
> +    if (ch->usbredirhost) {<br>
> +#if USBREDIR_VERSION >= 0x000701<br>
> +        usbredirhost_set_buffered_output_size_cb(ch->usbredirhost, usbredir_buffered_output_size_callback);<br>
> +#endif<br>
> +    } else {<br>
> +        g_free(ch);<br>
> +        ch = NULL;<br>
> +    }<br>
> +<br>
> +    SPICE_DEBUG("%s << %p", __FUNCTION__, ch);<br>
> +    return ch;<br>
> +}<br>
> +<br>
> +void spice_usb_backend_channel_up(SpiceUsbBackendChannel *ch)<br>
> +{<br>
> +    SPICE_DEBUG("%s %p, host %p", __FUNCTION__, ch, ch->usbredirhost);<br>
> +    if (ch->usbredirhost) {<br>
> +        usbredirhost_write_guest_data(ch->usbredirhost);<br>
> +    }<br>
> +}<br>
> +<br>
> +void spice_usb_backend_channel_finalize(SpiceUsbBackendChannel *ch)<br>
> +{<br>
> +    SPICE_DEBUG("%s >> %p", __FUNCTION__, ch);<br>
> +    if (ch->usbredirhost) {<br>
> +        usbredirhost_close(ch->usbredirhost);<br>
> +    }<br>
> +<br>
> +    if (ch->rules) {<br>
> +        g_free(ch->rules);<br>
<br>
Should be 'free(ch->rules);' as this was allocated by libusbredir.<br></blockquote><div><br></div><div>This can be changed in V3</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
> +    }<br>
> +<br>
> +    g_free(ch);<br>
> +    SPICE_DEBUG("%s << %p", __FUNCTION__, ch);<br>
> +}<br>
> +<br>
> +void spice_usb_backend_channel_get_guest_filter(<br>
> +    SpiceUsbBackendChannel *ch,<br>
> +    const struct usbredirfilter_rule **r,<br>
> +    int *count)<br>
> +{<br>
> +    int i;<br>
> +    *r = NULL;<br>
> +    *count = 0;<br>
> +    if (ch->usbredirhost) {<br>
> +        usbredirhost_get_guest_filter(ch->usbredirhost, r, count);<br>
> +    }<br>
> +    if (*r == NULL) {<br>
> +        *r = ch->rules;<br>
> +        *count = ch->rules_count;<br>
> +    }<br>
> +<br>
> +    if (*count) {<br>
> +        SPICE_DEBUG("%s ch %p: %d filters", __FUNCTION__, ch, *count);<br>
> +    }<br>
> +    for (i = 0; i < *count; i++) {<br>
> +        const struct usbredirfilter_rule *ra = *r;<br>
> +        SPICE_DEBUG("%s class %d, %X:%X",<br>
> +            ra[i].allow ? "allowed" : "denied", ra[i].device_class,<br>
> +            (uint32_t)ra[i].vendor_id, (uint32_t)ra[i].product_id);<br>
> +    }<br>
> +}<br>
> +<br>
> +#endif // USB_REDIR<br>
> diff --git a/src/usb-backend.h b/src/usb-backend.h<br>
> new file mode 100644<br>
> index 0000000..31c31eb<br>
> --- /dev/null<br>
> +++ b/src/usb-backend.h<br>
> @@ -0,0 +1,115 @@<br>
> +/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */<br>
> +/*<br>
> +    Copyright (C) 2018 Red Hat, Inc.<br>
> +<br>
> +    Red Hat Authors:<br>
> +    Yuri Benditovich<<a href="mailto:ybendito@redhat.com" target="_blank">ybendito@redhat.com</a>><br>
> +<br>
> +    This library is free software; you can redistribute it and/or<br>
> +    modify it under the terms of the GNU Lesser General Public<br>
> +    License as published by the Free Software Foundation; either<br>
> +    version 2.1 of the License, or (at your option) any later version.<br>
> +<br>
> +    This library is distributed in the hope that it will be useful,<br>
> +    but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
> +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU<br>
> +    Lesser General Public License for more details.<br>
> +<br>
> +    You should have received a copy of the GNU Lesser General Public<br>
> +    License along with this library; if not, see <<a href="http://www.gnu.org/licenses/" rel="noreferrer" target="_blank">http://www.gnu.org/licenses/</a>>.<br>
> +*/<br>
> +<br>
> +#ifndef __SPICE_USB_BACKEND_H__<br>
> +#define __SPICE_USB_BACKEND_H__<br>
> +<br>
> +#include <usbredirfilter.h><br>
> +#include "usb-device-manager.h"<br>
> +<br>
> +G_BEGIN_DECLS<br>
> +<br>
> +typedef struct _SpiceUsbBackend SpiceUsbBackend;<br>
> +typedef struct _SpiceUsbBackendDevice SpiceUsbBackendDevice;<br>
> +typedef struct _SpiceUsbBackendChannel SpiceUsbBackendChannel;<br>
> +<br>
> +typedef struct UsbDeviceInformation<br>
> +{<br>
> +    uint16_t bus;<br>
> +    uint16_t address;<br>
> +    uint16_t vid;<br>
> +    uint16_t pid;<br>
> +    uint8_t class;<br>
> +    uint8_t subclass;<br>
> +    uint8_t protocol;<br>
> +    uint8_t isochronous;<br>
> +} UsbDeviceInformation;<br>
<br>
As already mentioned in the past, please let's not duplicate very similar<br>
information with different names/different types in 2 structures with<br>
very close names (UsbDeviceInformation and SpiceUsbDeviceInfo)<br>
<br></blockquote><div><br></div><div>I do not see any possibility to do that in context of current commit.</div><div>This can be done later when SpiceUsbDevice will have persistent link</div><div>to backend device with it's information. For now this information is not</div><div>duplicated, it is cached in SpiceUsbDeviceInfo and can be returned</div><div>without looking for backend device.</div><div>If this is mandatory requirement, please let me know - this discards</div><div>current commit and we will need to reevaluate our plans.</div><div> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
> +<br>
> +typedef void (*usb_channel_error_callback)(void *user_data, const char *msg);<br>
> +typedef int (*usb_channel_write_callback)(void *user_data, uint8_t *data, int count);<br>
> +typedef gboolean (*usb_channel_is_ready_callback)(void *user_data);<br>
> +typedef uint64_t (*usb_channel_get_queue_size)(void *user_data);<br>
> +<br>
> +typedef struct SpiceUsbBackendChannelInitData<br>
> +{<br>
> +    void *user_data;<br>
> +    usb_channel_error_callback on_error;<br>
> +    usb_channel_write_callback write_callback;<br>
> +    usb_channel_is_ready_callback is_channel_ready;<br>
> +    usb_channel_get_queue_size get_queue_size;<br>
<br>
I would not add typedef for these vfuncs, I think the types are only<br>
used here, so I'd directly use:<br>
    void *user_data;<br>
    void (*on_error)(void *user_data, const char *msg);<br>
    ...<br>
<br></blockquote><div><br></div><div>Please let me know whether this is mandatory.</div><div> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> +} SpiceUsbBackendChannelInitData;<br>
> +<br>
> +typedef void(*usb_hot_plug_callback)(void *user_data, SpiceUsbBackendDevice *dev, gboolean added);<br>
<br>
usb_hotplug_callback.<br></blockquote><div><br></div><div>OK</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
> +<br>
> +enum {<br>
> +    USB_REDIR_ERROR_IO = -1,<br>
> +    USB_REDIR_ERROR_READ_PARSE = -2,<br>
> +    USB_REDIR_ERROR_DEV_REJECTED = -3,<br>
> +    USB_REDIR_ERROR_DEV_LOST = -4,<br>
> +};<br>
> +<br>
> +/* Spice USB backend API */<br>
> +/* sets error on failure */<br>
> +SpiceUsbBackend *spice_usb_backend_initialize(GError **error);<br>
> +void spice_usb_backend_finalize(SpiceUsbBackend *context);<br>
> +<br>
> +/*<br>
> +returns newly-allocated null-terminated list of<br>
<br>
s/list/array/<br>
<br></blockquote><div>OK</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> +SpiceUsbBackendDevice pointers.<br>
> +The caller must call spice_usb_backend_free_device_list<br>
> +after it finishes list processing<br>
<br>
"processing the list"<br></blockquote><div>OK</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
> +*/<br>
> +SpiceUsbBackendDevice **spice_usb_backend_get_device_list(SpiceUsbBackend *backend);<br>
> +void spice_usb_backend_free_device_list(SpiceUsbBackendDevice **devlist);<br>
> +gboolean spice_usb_backend_handle_events(SpiceUsbBackend *be);<br>
> +gboolean spice_usb_backend_handle_hotplug(SpiceUsbBackend *be,<br>
> +                                          void *user_data, usb_hot_plug_callback proc);<br>
> +<br>
> +/* Spice USB backend device API */<br>
> +gboolean spice_usb_backend_device_is_hub(SpiceUsbBackendDevice *dev);<br>
> +void spice_usb_backend_device_acquire(SpiceUsbBackendDevice *dev);<br>
> +void spice_usb_backend_device_release(SpiceUsbBackendDevice *dev);<br>
> +gboolean spice_usb_backend_devices_same(SpiceUsbBackendDevice *dev1, SpiceUsbBackendDevice *dev2);<br>
> +gconstpointer spice_usb_backend_device_get_libdev(SpiceUsbBackendDevice *dev);<br>
> +const UsbDeviceInformation* spice_usb_backend_device_get_info(SpiceUsbBackendDevice *dev);<br>
> +/* returns 0 if the device passes the filter */<br>
> +int spice_usb_backend_device_check_filter(SpiceUsbBackendDevice *dev,<br>
> +                                          const struct usbredirfilter_rule *rules, int count);<br>
> +<br>
> +/* Spice USB backend channel API */<br>
> +SpiceUsbBackendChannel *spice_usb_backend_channel_initialize(SpiceUsbBackend *context,<br>
> +                                                             const SpiceUsbBackendChannelInitData *init_data);<br>
> +void spice_usb_backend_channel_finalize(SpiceUsbBackendChannel *ch);<br>
> +/* returns 0 for success or error code */<br>
> +int spice_usb_backend_provide_read_data(SpiceUsbBackendChannel *ch, uint8_t *data, int count);<br>
> +gboolean spice_usb_backend_channel_attach(SpiceUsbBackendChannel *ch,<br>
> +                                          SpiceUsbBackendDevice *dev,<br>
> +                                          GError **error);<br>
> +void spice_usb_backend_channel_detach(SpiceUsbBackendChannel *ch);<br>
> +void spice_usb_backend_channel_up(SpiceUsbBackendChannel *ch);<br>
> +void spice_usb_backend_channel_get_guest_filter(SpiceUsbBackendChannel *ch,<br>
> +                                                const struct usbredirfilter_rule  **rules,<br>
> +                                                int *count);<br>
> +void spice_usb_backend_return_write_data(SpiceUsbBackendChannel *ch, void *data);<br>
> +<br>
> +G_END_DECLS<br>
> +<br>
> +#endif<br>
> diff --git a/src/usb-device-manager-priv.h b/src/usb-device-manager-priv.h<br>
> index 83884d7..4a1b592 100644<br>
> --- a/src/usb-device-manager-priv.h<br>
> +++ b/src/usb-device-manager-priv.h<br>
> @@ -22,6 +22,7 @@<br>
>  #define __SPICE_USB_DEVICE_MANAGER_PRIV_H__<br>
>  <br>
>  #include "usb-device-manager.h"<br>
> +#include "usb-backend.h"<br>
>  <br>
>  G_BEGIN_DECLS<br>
>  <br>
> @@ -32,7 +33,7 @@ void spice_usb_device_manager_stop_event_listening(<br>
>      SpiceUsbDeviceManager *manager);<br>
>  <br>
>  #ifdef USE_USBREDIR<br>
> -#include <libusb.h><br>
> +<br>
>  void spice_usb_device_manager_device_error(<br>
>      SpiceUsbDeviceManager *manager, SpiceUsbDevice *device, GError *err);<br>
>  <br>
> diff --git a/src/usb-device-manager.c b/src/usb-device-manager.c<br>
> index 354038a..e7c363a 100644<br>
> --- a/src/usb-device-manager.c<br>
> +++ b/src/usb-device-manager.c<br>
> @@ -24,10 +24,11 @@<br>
>  #include <glib-object.h><br>
>  <br>
>  #ifdef USE_USBREDIR<br>
> +<br>
>  #include <errno.h><br>
> -#include <libusb.h><br>
>  <br>
>  #ifdef G_OS_WIN32<br>
> +#include <windows.h><br>
>  #include "usbdk_api.h"<br>
>  #endif<br>
>  <br>
> @@ -41,8 +42,8 @@<br>
>  #endif<br>
>  <br>
>  #include "channel-usbredir-priv.h"<br>
> -#include "usbredirhost.h"<br>
>  #include "usbutil.h"<br>
> +<br>
>  #endif<br>
>  <br>
>  #include "spice-session-priv.h"<br>
> @@ -102,7 +103,7 @@ struct _SpiceUsbDeviceManagerPrivate {<br>
>      gchar *auto_connect_filter;<br>
>      gchar *redirect_on_connect;<br>
>  #ifdef USE_USBREDIR<br>
> -    libusb_context *context;<br>
> +    SpiceUsbBackend *context;<br>
>      int event_listeners;<br>
>      GThread *event_thread;<br>
>      gint event_thread_run;<br>
> @@ -112,10 +113,9 @@ struct _SpiceUsbDeviceManagerPrivate {<br>
>      int redirect_on_connect_rules_count;<br>
>  #ifdef USE_GUDEV<br>
>      GUdevClient *udev;<br>
> -    libusb_device **coldplug_list; /* Avoid needless reprobing during init */<br>
> +    SpiceUsbBackendDevice **coldplug_list; /* Avoid needless reprobing during init */<br>
>  #else<br>
>      gboolean redirecting; /* Handled by GUdevClient in the gudev case */<br>
> -    libusb_hotplug_callback_handle hp_handle;<br>
>  #endif<br>
>  #ifdef G_OS_WIN32<br>
>      usbdk_api_wrapper     *usbdk_api;<br>
> @@ -148,7 +148,7 @@ typedef struct _SpiceUsbDeviceInfo {<br>
>  #ifdef G_OS_WIN32<br>
>      guint8  state;<br>
>  #else<br>
> -    libusb_device *libdev;<br>
> +    SpiceUsbBackendDevice *bdev;<br>
>  #endif<br>
>      gint    ref;<br>
>  } SpiceUsbDeviceInfo;<br>
> @@ -168,15 +168,14 @@ static void spice_usb_device_manager_uevent_cb(GUdevClient     *client,<br>
>  static void spice_usb_device_manager_add_udev(SpiceUsbDeviceManager  *self,<br>
>                                                GUdevDevice            *udev);<br>
>  #else<br>
> -static int spice_usb_device_manager_hotplug_cb(libusb_context       *ctx,<br>
> -                                               libusb_device        *device,<br>
> -                                               libusb_hotplug_event  event,<br>
> -                                               void                 *data);<br>
> +static void spice_usb_device_manager_hotplug_cb(void                  *data,<br>
> +                                                SpiceUsbBackendDevice *bdev,<br>
> +                                                gboolean              added);<br>
>  #endif<br>
>  static void spice_usb_device_manager_check_redir_on_connect(<br>
>      SpiceUsbDeviceManager *self, SpiceChannel *channel);<br>
>  <br>
> -static SpiceUsbDeviceInfo *spice_usb_device_new(libusb_device *libdev);<br>
> +static SpiceUsbDeviceInfo *spice_usb_device_new(SpiceUsbBackendDevice *bdev);<br>
>  static SpiceUsbDevice *spice_usb_device_ref(SpiceUsbDevice *device);<br>
>  static void spice_usb_device_unref(SpiceUsbDevice *device);<br>
>  <br>
> @@ -185,12 +184,12 @@ static void _usbdk_hider_update(SpiceUsbDeviceManager *manager);<br>
>  static void _usbdk_hider_clear(SpiceUsbDeviceManager *manager);<br>
>  #endif<br>
>  <br>
> -static gboolean spice_usb_manager_device_equal_libdev(SpiceUsbDeviceManager *manager,<br>
> -                                                      SpiceUsbDevice *device,<br>
> -                                                      libusb_device *libdev);<br>
> -static libusb_device *<br>
> -spice_usb_device_manager_device_to_libdev(SpiceUsbDeviceManager *self,<br>
> -                                          SpiceUsbDevice *device);<br>
> +static gboolean spice_usb_manager_device_equal_bdev(SpiceUsbDeviceManager *manager,<br>
> +                                                    SpiceUsbDevice *device,<br>
> +                                                    SpiceUsbBackendDevice *bdev);<br>
> +static SpiceUsbBackendDevice*<br>
> +spice_usb_device_manager_device_to_bdev(SpiceUsbDeviceManager *self,<br>
> +                                        SpiceUsbDevice        *device);<br>
>  <br>
>  static void<br>
>  _spice_usb_device_manager_connect_device_async(SpiceUsbDeviceManager *self,<br>
> @@ -290,27 +289,15 @@ static gboolean spice_usb_device_manager_initable_init(GInitable  *initable,<br>
>      SpiceUsbDeviceManagerPrivate *priv = self->priv;<br>
>      GList *list;<br>
>      GList *it;<br>
> -    int rc;<br>
>  #ifdef USE_GUDEV<br>
>      const gchar *const subsystems[] = {"usb", NULL};<br>
>  #endif<br>
>  <br>
> -    /* Initialize libusb */<br>
> -    rc = libusb_init(&priv->context);<br>
> -    if (rc < 0) {<br>
> -        const char *desc = spice_usbutil_libusb_strerror(rc);<br>
> -        g_warning("Error initializing USB support: %s [%i]", desc, rc);<br>
> -        g_set_error(err, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,<br>
> -                    "Error initializing USB support: %s [%i]", desc, rc);<br>
> +    /* Initialize spice backend */<br>
> +    priv->context = spice_usb_backend_initialize(err);<br>
> +    if (!priv->context) {<br>
>          return FALSE;<br>
>      }<br>
> -<br>
> -#ifdef G_OS_WIN32<br>
> -#if LIBUSB_API_VERSION >= 0x01000106<br>
> -    libusb_set_option(priv->context, LIBUSB_OPTION_USE_USBDK);<br>
> -#endif<br>
> -#endif<br>
> -<br>
>      /* Start listening for usb devices plug / unplug */<br>
>  #ifdef USE_GUDEV<br>
>      priv->udev = g_udev_client_new(subsystems);<br>
> @@ -321,26 +308,20 @@ static gboolean spice_usb_device_manager_initable_init(GInitable  *initable,<br>
>      g_signal_connect(G_OBJECT(priv->udev), "uevent",<br>
>                       G_CALLBACK(spice_usb_device_manager_uevent_cb), self);<br>
>      /* Do coldplug (detection of already connected devices) */<br>
> -    libusb_get_device_list(priv->context, &priv->coldplug_list);<br>
> +    priv->coldplug_list = spice_usb_backend_get_device_list(priv->context);<br>
>      list = g_udev_client_query_by_subsystem(priv->udev, "usb");<br>
>      for (it = g_list_first(list); it; it = g_list_next(it)) {<br>
>          spice_usb_device_manager_add_udev(self, it->data);<br>
>          g_object_unref(it->data);<br>
>      }<br>
>      g_list_free(list);<br>
> -    libusb_free_device_list(priv->coldplug_list, 1);<br>
> +    spice_usb_backend_free_device_list(priv->coldplug_list);<br>
>      priv->coldplug_list = NULL;<br>
>  #else<br>
> -    rc = libusb_hotplug_register_callback(priv->context,<br>
> -        LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT,<br>
> -        LIBUSB_HOTPLUG_ENUMERATE, LIBUSB_HOTPLUG_MATCH_ANY,<br>
> -        LIBUSB_HOTPLUG_MATCH_ANY, LIBUSB_HOTPLUG_MATCH_ANY,<br>
> -        spice_usb_device_manager_hotplug_cb, self, &priv->hp_handle);<br>
> -    if (rc < 0) {<br>
> -        const char *desc = spice_usbutil_libusb_strerror(rc);<br>
> -        g_warning("Error initializing USB hotplug support: %s [%i]", desc, rc);<br>
> +    if (!spice_usb_backend_handle_hotplug(priv->context,<br>
> +        self, spice_usb_device_manager_hotplug_cb)) {<br>
>          g_set_error(err, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,<br>
> -                  "Error initializing USB hotplug support: %s [%i]", desc, rc);<br>
> +            "Error initializing USB hotplug support");<br>
<br>
You don't need to change the indentation here<br></blockquote><div><br></div><div>OK </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
>          return FALSE;<br>
>      }<br>
>      spice_usb_device_manager_start_event_listening(self, NULL);<br>
> @@ -371,21 +352,19 @@ static void spice_usb_device_manager_dispose(GObject *gobject)<br>
>      SpiceUsbDeviceManagerPrivate *priv = self->priv;<br>
>  <br>
>  #ifdef USE_LIBUSB_HOTPLUG<br>
> -    if (priv->hp_handle) {<br>
> -        spice_usb_device_manager_stop_event_listening(self);<br>
> -        if (g_atomic_int_get(&priv->event_thread_run)) {<br>
> -            /* Force termination of the event thread even if there were some<br>
> -             * mismatched spice_usb_device_manager_{start,stop}_event_listening<br>
> -             * calls. Otherwise, the usb event thread will be leaked, and will<br>
> -             * try to use the libusb context we destroy in finalize(), which would<br>
> -             * cause a crash */<br>
> -             g_warn_if_reached();<br>
> -             g_atomic_int_set(&priv->event_thread_run, FALSE);<br>
> -        }<br>
> -        /* This also wakes up the libusb_handle_events() in the event_thread */<br>
> -        libusb_hotplug_deregister_callback(priv->context, priv->hp_handle);<br>
> -        priv->hp_handle = 0;<br>
> +    spice_usb_device_manager_stop_event_listening(self);<br>
> +    if (g_atomic_int_get(&priv->event_thread_run)) {<br>
> +        /* Force termination of the event thread even if there were some<br>
> +            * mismatched spice_usb_device_manager_{start,stop}_event_listening<br>
> +            * calls. Otherwise, the usb event thread will be leaked, and will<br>
> +            * try to use the libusb context we destroy in finalize(), which would<br>
> +            * cause a crash */<br>
<br>
Indentation<br>
<br></blockquote><div>OK</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> +            g_warn_if_reached();<br>
> +            g_atomic_int_set(&priv->event_thread_run, FALSE);<br>
> +<br>
>      }<br>
> +    /* This also wakes up the libusb_handle_events() in the event_thread */<br>
> +    spice_usb_backend_handle_hotplug(priv->context, NULL, NULL);<br>
>  #endif<br>
>      if (priv->event_thread) {<br>
>          g_warn_if_fail(g_atomic_int_get(&priv->event_thread_run) == FALSE);<br>
> @@ -413,8 +392,9 @@ static void spice_usb_device_manager_finalize(GObject *gobject)<br>
>      g_clear_object(&priv->udev);<br>
>  #endif<br>
>      g_return_if_fail(priv->event_thread == NULL);<br>
> -    if (priv->context)<br>
> -        libusb_exit(priv->context);<br>
> +    if (priv->context) {<br>
> +        spice_usb_backend_finalize(priv->context);<br>
> +    }<br>
>      free(priv->auto_conn_filter_rules);<br>
>      free(priv->redirect_on_connect_rules);<br>
>  #ifdef G_OS_WIN32<br>
> @@ -739,7 +719,7 @@ static void spice_usb_device_manager_class_init(SpiceUsbDeviceManagerClass *klas<br>
>  #ifdef USE_USBREDIR<br>
>  <br>
>  /* ------------------------------------------------------------------ */<br>
> -/* gudev / libusb Helper functions                                    */<br>
> +/* gudev / backend Helper functions                                    */<br>
>  <br>
>  #ifdef USE_GUDEV<br>
>  static gboolean spice_usb_device_manager_get_udev_bus_n_address(<br>
> @@ -763,40 +743,16 @@ static gboolean spice_usb_device_manager_get_udev_bus_n_address(<br>
>  }<br>
>  #endif<br>
>  <br>
> -static gboolean spice_usb_device_manager_get_device_descriptor(<br>
> -    libusb_device *libdev,<br>
> -    struct libusb_device_descriptor *desc)<br>
> -{<br>
> -    int errcode;<br>
> -    const gchar *errstr;<br>
> -<br>
> -    g_return_val_if_fail(libdev != NULL, FALSE);<br>
> -    g_return_val_if_fail(desc   != NULL, FALSE);<br>
> -<br>
> -    errcode = libusb_get_device_descriptor(libdev, desc);<br>
> -    if (errcode < 0) {<br>
> -        int bus, addr;<br>
> -<br>
> -        bus = libusb_get_bus_number(libdev);<br>
> -        addr = libusb_get_device_address(libdev);<br>
> -        errstr = spice_usbutil_libusb_strerror(errcode);<br>
> -        g_warning("cannot get device descriptor for (%p) %d.%d -- %s(%d)",<br>
> -                  libdev, bus, addr, errstr, errcode);<br>
> -        return FALSE;<br>
> -    }<br>
> -    return TRUE;<br>
> -}<br>
> -<br>
>  #endif // USE_USBREDIR<br>
>  <br>
>  /**<br>
>   * spice_usb_device_get_libusb_device:<br>
> - * @device: #SpiceUsbDevice to get the descriptor information of<br>
> + * @device: #SpiceUsbDevice to get the libusb device of (if exists)<br>
>   *<br>
>   * Finds the %libusb_device associated with the @device.<br>
>   *<br>
> - * Returns: (transfer none): the %libusb_device associated to %SpiceUsbDevice.<br>
> - *<br>
> + * Returns: (transfer none): the %libusb_device associated to %SpiceUsbDevice<br>
> + *    or NULL (if the device does not have associated libusb device)<br>
>   * Since: 0.27<br>
>   **/<br>
>  gconstpointer<br>
> @@ -808,34 +764,13 @@ spice_usb_device_get_libusb_device(const SpiceUsbDevice *device G_GNUC_UNUSED)<br>
>  <br>
>      g_return_val_if_fail(info != NULL, FALSE);<br>
>  <br>
> -    return info->libdev;<br>
> +    return spice_usb_backend_device_get_libdev(info->bdev);<br>
>  #endif<br>
>  #endif<br>
>      return NULL;<br>
>  }<br>
>  <br>
>  #ifdef USE_USBREDIR<br>
> -static gboolean spice_usb_device_manager_get_libdev_vid_pid(<br>
> -    libusb_device *libdev, int *vid, int *pid)<br>
> -{<br>
> -    struct libusb_device_descriptor desc;<br>
> -<br>
> -    g_return_val_if_fail(libdev != NULL, FALSE);<br>
> -    g_return_val_if_fail(vid != NULL, FALSE);<br>
> -    g_return_val_if_fail(pid != NULL, FALSE);<br>
> -<br>
> -    *vid = *pid = 0;<br>
> -<br>
> -    if (!spice_usb_device_manager_get_device_descriptor(libdev, &desc)) {<br>
> -        return FALSE;<br>
> -    }<br>
> -    *vid = desc.idVendor;<br>
> -    *pid = desc.idProduct;<br>
> -<br>
> -    return TRUE;<br>
> -}<br>
> -<br>
> -/* ------------------------------------------------------------------ */<br>
>  /* callbacks                                                          */<br>
>  <br>
>  static void channel_new(SpiceSession *session, SpiceChannel *channel,<br>
> @@ -854,9 +789,8 @@ static void channel_new(SpiceSession *session, SpiceChannel *channel,<br>
>      g_signal_connect(channel, "channel-event", G_CALLBACK(channel_event), self);<br>
>  <br>
>      spice_usb_device_manager_check_redir_on_connect(self, channel);<br>
> -<br>
>      /*<br>
> -     * add a reference to ourself, to make sure the libusb context is<br>
> +     * add a reference to ourself, to make sure the backend device context is<br>
>       * alive as long as the channel is.<br>
>       * TODO: moving to gusb could help here too.<br>
>       */<br>
> @@ -933,12 +867,12 @@ spice_usb_device_manager_device_match(SpiceUsbDeviceManager *self, SpiceUsbDevic<br>
>  <br>
>  #ifdef USE_GUDEV<br>
>  static gboolean<br>
> -spice_usb_device_manager_libdev_match(SpiceUsbDeviceManager *self, libusb_device *libdev,<br>
> +spice_usb_device_manager_bdev_match(SpiceUsbDeviceManager *self, SpiceUsbBackendDevice *dev,<br>
>                                        const int bus, const int address)<br>
<br>
Indentation<br></blockquote><div>OK </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
>  {<br>
> +    const UsbDeviceInformation* info = spice_usb_backend_device_get_info(dev);<br>
>      /* match functions for Linux/UsbDk -- match by bus.addr */<br>
> -    return (libusb_get_bus_number(libdev) == bus &&<br>
> -            libusb_get_device_address(libdev) == address);<br>
> +    return (info->bus == bus && info->address == address);<br>
>  }<br>
>  #endif<br>
>  <br>
> @@ -960,21 +894,17 @@ spice_usb_device_manager_find_device(SpiceUsbDeviceManager *self,<br>
>      return device;<br>
>  }<br>
>  <br>
> -static void spice_usb_device_manager_add_dev(SpiceUsbDeviceManager  *self,<br>
> -                                             libusb_device          *libdev)<br>
> +static void spice_usb_device_manager_add_dev(SpiceUsbDeviceManager          *self,<br>
> +                                             SpiceUsbBackendDevice          *bdev)<br>
<br>
Here I don't think you need that many spaces before *self and *bdev<br>
<br></blockquote><div>OK </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
>  {<br>
>      SpiceUsbDeviceManagerPrivate *priv = self->priv;<br>
> -    struct libusb_device_descriptor desc;<br>
>      SpiceUsbDevice *device;<br>
>  <br>
> -    if (!spice_usb_device_manager_get_device_descriptor(libdev, &desc))<br>
> -        return;<br>
> -<br>
>      /* Skip hubs */<br>
> -    if (desc.bDeviceClass == LIBUSB_CLASS_HUB)<br>
> +    if (spice_usb_backend_device_is_hub(bdev))<br>
>          return;<br>
>  <br>
> -    device = (SpiceUsbDevice*)spice_usb_device_new(libdev);<br>
> +    device = (SpiceUsbDevice*)spice_usb_device_new(bdev);<br>
>      if (!device)<br>
>          return;<br>
>  <br>
> @@ -986,10 +916,10 @@ static void spice_usb_device_manager_add_dev(SpiceUsbDeviceManager  *self,<br>
>          can_redirect = spice_usb_device_manager_can_redirect_device(<br>
>                                          self, device, NULL);<br>
>  <br>
> -        auto_ok = usbredirhost_check_device_filter(<br>
> -                            priv->auto_conn_filter_rules,<br>
> -                            priv->auto_conn_filter_rules_count,<br>
> -                            libdev, 0) == 0;<br>
> +        auto_ok = spice_usb_backend_device_check_filter(<br>
> +            bdev,<br>
> +            priv->auto_conn_filter_rules,<br>
> +            priv->auto_conn_filter_rules_count) == 0;<br>
>  <br>
>          if (can_redirect && auto_ok)<br>
>              spice_usb_device_manager_connect_device_async(self,<br>
> @@ -1036,7 +966,7 @@ static void spice_usb_device_manager_add_udev(SpiceUsbDeviceManager  *self,<br>
>                                                GUdevDevice            *udev)<br>
>  {<br>
>      SpiceUsbDeviceManagerPrivate *priv = self->priv;<br>
> -    libusb_device *libdev = NULL, **dev_list = NULL;<br>
> +    SpiceUsbBackendDevice *bdev = NULL, **dev_list = NULL;<br>
>      SpiceUsbDevice *device;<br>
>      const gchar *devtype;<br>
>      int i, bus, address;<br>
> @@ -1064,23 +994,23 @@ static void spice_usb_device_manager_add_udev(SpiceUsbDeviceManager  *self,<br>
>      if (priv->coldplug_list)<br>
>          dev_list = priv->coldplug_list;<br>
>      else<br>
> -        libusb_get_device_list(priv->context, &dev_list);<br>
> +        dev_list = spice_usb_backend_get_device_list(priv->context);<br>
>  <br>
>      for (i = 0; dev_list && dev_list[i]; i++) {<br>
> -        if (spice_usb_device_manager_libdev_match(self, dev_list[i], bus, address)) {<br>
> -            libdev = dev_list[i];<br>
> +        if (spice_usb_device_manager_bdev_match(self, dev_list[i], bus, address)) {<br>
> +            bdev = dev_list[i];<br>
>              break;<br>
>          }<br>
>      }<br>
>  <br>
> -    if (libdev)<br>
> -        spice_usb_device_manager_add_dev(self, libdev);<br>
> +    if (bdev)<br>
> +        spice_usb_device_manager_add_dev(self, bdev);<br>
>      else<br>
>          g_warning("Could not find USB device to add " DEV_ID_FMT,<br>
>                    (guint) bus, (guint) address);<br>
>  <br>
>      if (!priv->coldplug_list)<br>
> -        libusb_free_device_list(dev_list, 1);<br>
> +        spice_usb_backend_free_device_list(dev_list);<br>
>  }<br>
>  <br>
>  static void spice_usb_device_manager_remove_udev(SpiceUsbDeviceManager  *self,<br>
> @@ -1109,8 +1039,8 @@ static void spice_usb_device_manager_uevent_cb(GUdevClient     *client,<br>
>  #else<br>
>  struct hotplug_idle_cb_args {<br>
>      SpiceUsbDeviceManager *self;<br>
> -    libusb_device *device;<br>
> -    libusb_hotplug_event event;<br>
> +    SpiceUsbBackendDevice *device;<br>
> +    gboolean device_added;<br>
>  };<br>
>  <br>
>  static gboolean spice_usb_device_manager_hotplug_idle_cb(gpointer user_data)<br>
> @@ -1118,36 +1048,31 @@ static gboolean spice_usb_device_manager_hotplug_idle_cb(gpointer user_data)<br>
>      struct hotplug_idle_cb_args *args = user_data;<br>
>      SpiceUsbDeviceManager *self = SPICE_USB_DEVICE_MANAGER(args->self);<br>
>  <br>
> -    switch (args->event) {<br>
> -    case LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED:<br>
> +    if (args->device_added) {<br>
>          spice_usb_device_manager_add_dev(self, args->device);<br>
> -        break;<br>
> -    case LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT:<br>
> -        spice_usb_device_manager_remove_dev(self,<br>
> -                                    libusb_get_bus_number(args->device),<br>
> -                                    libusb_get_device_address(args->device));<br>
> -        break;<br>
> +    } else {<br>
> +        const UsbDeviceInformation *info = spice_usb_backend_device_get_info(args->device);<br>
> +        spice_usb_device_manager_remove_dev(self, info->bus, info->address);<br>
>      }<br>
> -    libusb_unref_device(args->device);<br>
> +    spice_usb_backend_device_release(args->device);<br>
>      g_object_unref(self);<br>
>      g_free(args);<br>
>      return FALSE;<br>
>  }<br>
>  <br>
>  /* Can be called from both the main-thread as well as the event_thread */<br>
> -static int spice_usb_device_manager_hotplug_cb(libusb_context       *ctx,<br>
> -                                               libusb_device        *device,<br>
> -                                               libusb_hotplug_event  event,<br>
> -                                               void                 *user_data)<br>
> +static void spice_usb_device_manager_hotplug_cb(void                  *user_data,<br>
> +                                                SpiceUsbBackendDevice *bdev,<br>
> +                                                gboolean              added)<br>
>  {<br>
>      SpiceUsbDeviceManager *self = SPICE_USB_DEVICE_MANAGER(user_data);<br>
>      struct hotplug_idle_cb_args *args = g_malloc0(sizeof(*args));<br>
>  <br>
>      args->self = g_object_ref(self);<br>
> -    args->device = libusb_ref_device(device);<br>
> -    args->event = event;<br>
> +    spice_usb_backend_device_acquire(bdev);<br>
> +    args->device_added = added;<br>
> +    args->device = bdev;<br>
>      g_idle_add(spice_usb_device_manager_hotplug_idle_cb, args);<br>
> -    return 0;<br>
>  }<br>
>  #endif // USE_USBREDIR<br>
>  <br>
> @@ -1174,13 +1099,9 @@ static gpointer spice_usb_device_manager_usb_ev_thread(gpointer user_data)<br>
>  {<br>
>      SpiceUsbDeviceManager *self = SPICE_USB_DEVICE_MANAGER(user_data);<br>
>      SpiceUsbDeviceManagerPrivate *priv = self->priv;<br>
> -    int rc;<br>
>  <br>
>      while (g_atomic_int_get(&priv->event_thread_run)) {<br>
> -        rc = libusb_handle_events(priv->context);<br>
> -        if (rc && rc != LIBUSB_ERROR_INTERRUPTED) {<br>
> -            const char *desc = spice_usbutil_libusb_strerror(rc);<br>
> -            g_warning("Error handling USB events: %s [%i]", desc, rc);<br>
> +        if (!spice_usb_backend_handle_events(priv->context)) {<br>
>              break;<br>
>          }<br>
>      }<br>
> @@ -1231,7 +1152,7 @@ static void spice_usb_device_manager_check_redir_on_connect(<br>
>      SpiceUsbDeviceManagerPrivate *priv = self->priv;<br>
>      GTask *task;<br>
>      SpiceUsbDevice *device;<br>
> -    libusb_device *libdev;<br>
> +    SpiceUsbBackendDevice *dev;<br>
>      guint i;<br>
>  <br>
>      if (priv->redirect_on_connect == NULL)<br>
> @@ -1243,15 +1164,15 @@ static void spice_usb_device_manager_check_redir_on_connect(<br>
>          if (spice_usb_device_manager_is_device_connected(self, device))<br>
>              continue;<br>
>  <br>
> -        libdev = spice_usb_device_manager_device_to_libdev(self, device);<br>
> +        dev = spice_usb_device_manager_device_to_bdev(self, device);<br>
>  #ifdef G_OS_WIN32<br>
> -        if (libdev == NULL)<br>
> +        if (dev == NULL)<br>
>              continue;<br>
>  #endif<br>
> -        if (usbredirhost_check_device_filter(<br>
> -                            priv->redirect_on_connect_rules,<br>
> -                            priv->redirect_on_connect_rules_count,<br>
> -                            libdev, 0) == 0) {<br>
> +        if (spice_usb_backend_device_check_filter(<br>
> +            dev,<br>
> +            priv->redirect_on_connect_rules,<br>
> +            priv->redirect_on_connect_rules_count) == 0) {<br>
>              /* Note: re-uses spice_usb_device_manager_connect_device_async's<br>
>                 completion handling code! */<br>
>              task = g_task_new(self,<br>
> @@ -1261,14 +1182,14 @@ static void spice_usb_device_manager_check_redir_on_connect(<br>
>  <br>
>              spice_usbredir_channel_connect_device_async(<br>
>                                 SPICE_USBREDIR_CHANNEL(channel),<br>
> -                               libdev, device, NULL,<br>
> +                               dev, device, NULL,<br>
>                                 spice_usb_device_manager_channel_connect_cb,<br>
>                                 task);<br>
> -            libusb_unref_device(libdev);<br>
> +            spice_usb_backend_device_release(dev);<br>
>              return; /* We've taken the channel! */<br>
>          }<br>
>  <br>
> -        libusb_unref_device(libdev);<br>
> +        spice_usb_backend_device_release(dev);<br>
>      }<br>
>  }<br>
>  <br>
> @@ -1292,8 +1213,8 @@ static SpiceUsbredirChannel *spice_usb_device_manager_get_channel_for_dev(<br>
>      for (i = 0; i < priv->channels->len; i++) {<br>
>          SpiceUsbredirChannel *channel = g_ptr_array_index(priv->channels, i);<br>
>          spice_usbredir_channel_lock(channel);<br>
> -        libusb_device *libdev = spice_usbredir_channel_get_device(channel);<br>
> -        if (spice_usb_manager_device_equal_libdev(manager, device, libdev)) {<br>
> +        SpiceUsbBackendDevice *dev = spice_usbredir_channel_get_device(channel);<br>
> +        if (spice_usb_manager_device_equal_bdev(manager, device, dev)) {<br>
>              spice_usbredir_channel_unlock(channel);<br>
>              return channel;<br>
>          }<br>
> @@ -1350,13 +1271,13 @@ GPtrArray* spice_usb_device_manager_get_devices_with_filter(<br>
>          SpiceUsbDevice *device = g_ptr_array_index(priv->devices, i);<br>
>  <br>
>          if (rules) {<br>
> -            libusb_device *libdev =<br>
> -                spice_usb_device_manager_device_to_libdev(self, device);<br>
> +            SpiceUsbBackendDevice *bdev =<br>
> +                spice_usb_device_manager_device_to_bdev(self, device);<br>
>  #ifdef G_OS_WIN32<br>
> -            if (libdev == NULL)<br>
> +            if (bdev == NULL)<br>
>                  continue;<br>
>  #endif<br>
> -            if (usbredirhost_check_device_filter(rules, count, libdev, 0) != 0)<br>
> +            if (spice_usb_backend_device_check_filter(bdev, rules, count) != 0)<br>
>                  continue;<br>
>          }<br>
>          g_ptr_array_add(devices_copy, spice_usb_device_ref(device));<br>
> @@ -1430,7 +1351,7 @@ _spice_usb_device_manager_connect_device_async(SpiceUsbDeviceManager *self,<br>
>      task = g_task_new(self, cancellable, callback, user_data);<br>
>  <br>
>      SpiceUsbDeviceManagerPrivate *priv = self->priv;<br>
> -    libusb_device *libdev;<br>
> +    SpiceUsbBackendDevice *bdev;<br>
>      guint i;<br>
>  <br>
>      if (spice_usb_device_manager_is_device_connected(self, device)) {<br>
> @@ -1446,9 +1367,9 @@ _spice_usb_device_manager_connect_device_async(SpiceUsbDeviceManager *self,<br>
>          if (spice_usbredir_channel_get_device(channel))<br>
>              continue; /* Skip already used channels */<br>
>  <br>
> -        libdev = spice_usb_device_manager_device_to_libdev(self, device);<br>
> +        bdev = spice_usb_device_manager_device_to_bdev(self, device);<br>
>  #ifdef G_OS_WIN32<br>
> -        if (libdev == NULL) {<br>
> +        if (bdev == NULL) {<br>
>              /* Most likely, the device was plugged out at driver installation<br>
>               * time, and its remove-device event was ignored.<br>
>               * So remove the device now<br>
> @@ -1466,12 +1387,12 @@ _spice_usb_device_manager_connect_device_async(SpiceUsbDeviceManager *self,<br>
>          }<br>
>  #endif<br>
>          spice_usbredir_channel_connect_device_async(channel,<br>
> -                                 libdev,<br>
> +                                 bdev,<br>
>                                   device,<br>
>                                   cancellable,<br>
>                                   spice_usb_device_manager_channel_connect_cb,<br>
>                                   task);<br>
> -        libusb_unref_device(libdev);<br>
> +        spice_usb_backend_device_release(bdev);<br>
>          return;<br>
>      }<br>
>  <br>
> @@ -1733,20 +1654,20 @@ spice_usb_device_manager_can_redirect_device(SpiceUsbDeviceManager  *self,<br>
>  <br>
>      if (guest_filter_rules) {<br>
>          gboolean filter_ok;<br>
> -        libusb_device *libdev;<br>
> +        SpiceUsbBackendDevice *bdev;<br>
>  <br>
> -        libdev = spice_usb_device_manager_device_to_libdev(self, device);<br>
> +        bdev = spice_usb_device_manager_device_to_bdev(self, device);<br>
>  #ifdef G_OS_WIN32<br>
> -        if (libdev == NULL) {<br>
> +        if (bdev == NULL) {<br>
>              g_set_error_literal(err, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,<br>
>                                  _("Some USB devices were not found"));<br>
>              return FALSE;<br>
>          }<br>
>  #endif<br>
> -        filter_ok = (usbredirhost_check_device_filter(<br>
> -                            guest_filter_rules, guest_filter_rules_count,<br>
> -                            libdev, 0) == 0);<br>
> -        libusb_unref_device(libdev);<br>
> +        filter_ok = (spice_usb_backend_device_check_filter(<br>
> +                            bdev,<br>
> +                            guest_filter_rules, guest_filter_rules_count) == 0);<br>
> +        spice_usb_backend_device_release(bdev);<br>
>          if (!filter_ok) {<br>
>              g_set_error_literal(err, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,<br>
>                                  _("Some USB devices are blocked by host policy"));<br>
> @@ -1837,64 +1758,30 @@ gchar *spice_usb_device_get_description(SpiceUsbDevice *device, const gchar *for<br>
>  #endif<br>
>  }<br>
>  <br>
> -<br>
> -<br>
>  #ifdef USE_USBREDIR<br>
> -static gboolean probe_isochronous_endpoint(libusb_device *libdev)<br>
> -{<br>
> -    struct libusb_config_descriptor *conf_desc;<br>
> -    gboolean isoc_found = FALSE;<br>
> -    gint i, j, k;<br>
> -<br>
> -    g_return_val_if_fail(libdev != NULL, FALSE);<br>
> -<br>
> -    if (libusb_get_active_config_descriptor(libdev, &conf_desc) != 0) {<br>
> -        g_return_val_if_reached(FALSE);<br>
> -    }<br>
> -<br>
> -    for (i = 0; !isoc_found && i < conf_desc->bNumInterfaces; i++) {<br>
> -        for (j = 0; !isoc_found && j < conf_desc->interface[i].num_altsetting; j++) {<br>
> -            for (k = 0; !isoc_found && k < conf_desc->interface[i].altsetting[j].bNumEndpoints;k++) {<br>
> -                gint attributes = conf_desc->interface[i].altsetting[j].endpoint[k].bmAttributes;<br>
> -                gint type = attributes & LIBUSB_TRANSFER_TYPE_MASK;<br>
> -                if (type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS)<br>
> -                    isoc_found = TRUE;<br>
> -            }<br>
> -        }<br>
> -    }<br>
> -<br>
> -    libusb_free_config_descriptor(conf_desc);<br>
> -    return isoc_found;<br>
> -}<br>
>  <br>
>  /*<br>
>   * SpiceUsbDeviceInfo<br>
>   */<br>
> -static SpiceUsbDeviceInfo *spice_usb_device_new(libusb_device *libdev)<br>
> +static SpiceUsbDeviceInfo *spice_usb_device_new(SpiceUsbBackendDevice *bdev)<br>
>  {<br>
>      SpiceUsbDeviceInfo *info;<br>
> -    int vid, pid;<br>
> -    guint8 bus, addr;<br>
> +    const UsbDeviceInformation *devinfo;<br>
>  <br>
> -    g_return_val_if_fail(libdev != NULL, NULL);<br>
> -<br>
> -    bus = libusb_get_bus_number(libdev);<br>
> -    addr = libusb_get_device_address(libdev);<br>
> -<br>
> -    if (!spice_usb_device_manager_get_libdev_vid_pid(libdev, &vid, &pid)) {<br>
> -        return NULL;<br>
> -    }<br>
> +    g_return_val_if_fail(bdev != NULL, NULL);<br>
> +    devinfo = spice_usb_backend_device_get_info(bdev);<br>
>  <br>
>      info = g_new0(SpiceUsbDeviceInfo, 1);<br>
>  <br>
> -    info->busnum  = bus;<br>
> -    info->devaddr = addr;<br>
> -    info->vid = vid;<br>
> -    info->pid = pid;<br>
> +    info->busnum  = devinfo->bus;<br>
> +    info->devaddr = devinfo->address;<br>
> +    info->vid = devinfo->vid;<br>
> +    info->pid = devinfo->pid;<br>
>      info->ref = 1;<br>
> -    info->isochronous = probe_isochronous_endpoint(libdev);<br>
> +    info->isochronous = devinfo->isochronous;<br>
>  #ifndef G_OS_WIN32<br>
> -    info->libdev = libusb_ref_device(libdev);<br>
> +    info->bdev = bdev;<br>
> +    spice_usb_backend_device_acquire(bdev);<br>
>  #endif<br>
>  <br>
>      return info;<br>
> @@ -2032,50 +1919,54 @@ static void spice_usb_device_unref(SpiceUsbDevice *device)<br>
>      ref_count_is_0 = g_atomic_int_dec_and_test(&info->ref);<br>
>      if (ref_count_is_0) {<br>
>  #ifndef G_OS_WIN32<br>
> -        libusb_unref_device(info->libdev);<br>
> +        if (info->bdev) {<br>
> +            spice_usb_backend_device_release(info->bdev);<br>
> +        }<br>
>  #endif<br>
> +        info->vid = info->pid = 0;<br>
> +        SPICE_DEBUG("%s: deleting %p", __FUNCTION__, info);<br>
>          g_free(info);<br>
>      }<br>
>  }<br>
>  <br>
>  #ifndef G_OS_WIN32 /* Linux -- directly compare libdev */<br>
>  static gboolean<br>
> -spice_usb_manager_device_equal_libdev(SpiceUsbDeviceManager *manager,<br>
> -                                      SpiceUsbDevice *device,<br>
> -                                      libusb_device  *libdev)<br>
> +spice_usb_manager_device_equal_bdev(SpiceUsbDeviceManager *manager,<br>
> +                                    SpiceUsbDevice        *device,<br>
> +                                    SpiceUsbBackendDevice *bdev)<br>
>  {<br>
>      SpiceUsbDeviceInfo *info = (SpiceUsbDeviceInfo *)device;<br>
>  <br>
> -    if ((device == NULL) || (libdev == NULL))<br>
> +    if ((device == NULL) || (bdev == NULL))<br>
>          return FALSE;<br>
>  <br>
> -    return info->libdev == libdev;<br>
> +    return spice_usb_backend_devices_same(info->bdev, bdev);<br>
>  }<br>
>  #else /* Windows -- compare vid:pid of device and libdev */<br>
>  static gboolean<br>
> -spice_usb_manager_device_equal_libdev(SpiceUsbDeviceManager *manager,<br>
> -                                      SpiceUsbDevice *device,<br>
> -                                      libusb_device  *libdev)<br>
> +spice_usb_manager_device_equal_bdev(SpiceUsbDeviceManager *manager,<br>
> +                                    SpiceUsbDevice *device,<br>
> +                                    SpiceUsbBackendDevice  *bdev)<br>
>  {<br>
>      int busnum, devaddr;<br>
>  <br>
> -    if ((device == NULL) || (libdev == NULL))<br>
> +    if ((device == NULL) || (bdev == NULL))<br>
>          return FALSE;<br>
>  <br>
>      busnum = spice_usb_device_get_busnum(device);<br>
>      devaddr = spice_usb_device_get_devaddr(device);<br>
> -    return spice_usb_device_manager_libdev_match(manager, libdev,<br>
> -                                                 busnum, devaddr);<br>
> +    return spice_usb_device_manager_bdev_match(manager, bdev,<br>
> +                                               busnum, devaddr);<br>
>  }<br>
>  #endif<br>
>  <br>
>  /*<br>
> - * Caller must libusb_unref_device the libusb_device returned by this function.<br>
> - * Returns a libusb_device, or NULL upon failure<br>
> + * Caller must spice_usb_backend_device_release the SpiceUsbBackendDevice returned by this function.<br>
> + * Returns a SpiceUsbBackendDevice, or NULL upon failure<br>
>   */<br>
> -static libusb_device *<br>
> -spice_usb_device_manager_device_to_libdev(SpiceUsbDeviceManager *self,<br>
> -                                          SpiceUsbDevice *device)<br>
> +static SpiceUsbBackendDevice *<br>
> +spice_usb_device_manager_device_to_bdev(SpiceUsbDeviceManager *self,<br>
> +                                        SpiceUsbDevice        *device)<br>
>  {<br>
>  #ifdef G_OS_WIN32<br>
>      /*<br>
> @@ -2085,7 +1976,7 @@ spice_usb_device_manager_device_to_libdev(SpiceUsbDeviceManager *self,<br>
>       * driver swap we do under windows invalidates the cached libdev.<br>
>       */<br>
>  <br>
> -    libusb_device *d, **devlist;<br>
> +    SpiceUsbBackendDevice *d, **devlist;<br>
>      int i;<br>
>  <br>
>      g_return_val_if_fail(SPICE_IS_USB_DEVICE_MANAGER(self), NULL);<br>
> @@ -2093,26 +1984,27 @@ spice_usb_device_manager_device_to_libdev(SpiceUsbDeviceManager *self,<br>
>      g_return_val_if_fail(self->priv != NULL, NULL);<br>
>      g_return_val_if_fail(self->priv->context != NULL, NULL);<br>
>  <br>
> -    libusb_get_device_list(self->priv->context, &devlist);<br>
> +    devlist = spice_usb_backend_get_device_list(self->priv->context);<br>
>      if (!devlist)<br>
>          return NULL;<br>
>  <br>
>      for (i = 0; (d = devlist[i]) != NULL; i++) {<br>
> -        if (spice_usb_manager_device_equal_libdev(self, device, d)) {<br>
> -            libusb_ref_device(d);<br>
> +        if (spice_usb_manager_device_equal_bdev(self, device, d)) {<br>
> +            spice_usb_backend_device_acquire(d);<br>
>              break;<br>
>          }<br>
>      }<br>
>  <br>
> -    libusb_free_device_list(devlist, 1);<br>
> +    spice_usb_backend_free_device_list(devlist);<br>
>  <br>
>      return d;<br>
>  <br>
>  #else<br>
>      /* Simply return a ref to the cached libdev */<br>
>      SpiceUsbDeviceInfo *info = (SpiceUsbDeviceInfo *)device;<br>
> -<br>
> -    return libusb_ref_device(info->libdev);<br>
> +    spice_usb_backend_device_acquire(info->bdev);<br>
> +    return info->bdev;<br>
>  #endif<br>
>  }<br>
> +<br>
>  #endif /* USE_USBREDIR */<br>
> diff --git a/src/usb-device-manager.h b/src/usb-device-manager.h<br>
> index 773208f..4ba7b2f 100644<br>
> --- a/src/usb-device-manager.h<br>
> +++ b/src/usb-device-manager.h<br>
> @@ -87,6 +87,7 @@ struct _SpiceUsbDeviceManagerClass<br>
>                                   SpiceUsbDevice *device, GError *error);<br>
>      void (*device_error) (SpiceUsbDeviceManager *manager,<br>
>                            SpiceUsbDevice *device, GError *error);<br>
> +<br>
<br>
Spurious whitespace change.<br></blockquote><div>OK </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
>      /*< private >*/<br>
>      /*<br>
>       * If adding fields to this struct, remove corresponding<br>
> diff --git a/src/usbutil.c b/src/usbutil.c<br>
> index e96ab11..5052ef3 100644<br>
> --- a/src/usbutil.c<br>
> +++ b/src/usbutil.c<br>
> @@ -58,42 +58,6 @@ static GMutex usbids_load_mutex;<br>
>  static int usbids_vendor_count = 0; /* < 0: failed, 0: empty, > 0: loaded */<br>
>  static usb_vendor_info *usbids_vendor_info = NULL;<br>
>  <br>
> -G_GNUC_INTERNAL<br>
> -const char *spice_usbutil_libusb_strerror(enum libusb_error error_code)<br>
> -{<br>
> -    switch (error_code) {<br>
> -    case LIBUSB_SUCCESS:<br>
> -        return "Success";<br>
> -    case LIBUSB_ERROR_IO:<br>
> -        return "Input/output error";<br>
> -    case LIBUSB_ERROR_INVALID_PARAM:<br>
> -        return "Invalid parameter";<br>
> -    case LIBUSB_ERROR_ACCESS:<br>
> -        return "Access denied (insufficient permissions)";<br>
> -    case LIBUSB_ERROR_NO_DEVICE:<br>
> -        return "No such device (it may have been disconnected)";<br>
> -    case LIBUSB_ERROR_NOT_FOUND:<br>
> -        return "Entity not found";<br>
> -    case LIBUSB_ERROR_BUSY:<br>
> -        return "Resource busy";<br>
> -    case LIBUSB_ERROR_TIMEOUT:<br>
> -        return "Operation timed out";<br>
> -    case LIBUSB_ERROR_OVERFLOW:<br>
> -        return "Overflow";<br>
> -    case LIBUSB_ERROR_PIPE:<br>
> -        return "Pipe error";<br>
> -    case LIBUSB_ERROR_INTERRUPTED:<br>
> -        return "System call interrupted (perhaps due to signal)";<br>
> -    case LIBUSB_ERROR_NO_MEM:<br>
> -        return "Insufficient memory";<br>
> -    case LIBUSB_ERROR_NOT_SUPPORTED:<br>
> -        return "Operation not supported or unimplemented on this platform";<br>
> -    case LIBUSB_ERROR_OTHER:<br>
> -        return "Other error";<br>
> -    }<br>
> -    return "Unknown error";<br>
> -}<br>
> -<br>
>  #ifdef __linux__<br>
>  /* <Sigh> libusb does not allow getting the manufacturer and product strings<br>
>     without opening the device, so grab them directly from sysfs */<br>
> diff --git a/src/usbutil.h b/src/usbutil.h<br>
> index de5e92a..d18d688 100644<br>
> --- a/src/usbutil.h<br>
> +++ b/src/usbutil.h<br>
> @@ -24,11 +24,9 @@<br>
>  #include <glib.h><br>
>  <br>
>  #ifdef USE_USBREDIR<br>
> -#include <libusb.h><br>
>  <br>
>  G_BEGIN_DECLS<br>
>  <br>
> -const char *spice_usbutil_libusb_strerror(enum libusb_error error_code);<br>
>  void spice_usb_util_get_device_strings(int bus, int address,<br>
>                                         int vendor_id, int product_id,<br>
>                                         gchar **manufacturer, gchar **product);<br>
> diff --git a/src/win-usb-dev.c b/src/win-usb-dev.c<br>
> index 9a130a3..e5b6d62 100644<br>
> --- a/src/win-usb-dev.c<br>
> +++ b/src/win-usb-dev.c<br>
> @@ -23,11 +23,13 @@<br>
>  #include "config.h"<br>
>  <br>
>  #include <windows.h><br>
> -#include <libusb.h><br>
>  #include "win-usb-dev.h"<br>
>  #include "spice-marshal.h"<br>
>  #include "spice-util.h"<br>
>  #include "usbutil.h"<br>
> +#include "usb-backend.h"<br>
> +<br>
> +#define USB_CLASS_HUB   9<br>
>  <br>
>  enum {<br>
>      PROP_0,<br>
> @@ -35,7 +37,7 @@ enum {<br>
>  };<br>
>  <br>
>  struct _GUdevClientPrivate {<br>
> -    libusb_context *ctx;<br>
> +    SpiceUsbBackend *ctx;<br>
>      GList *udev_list;<br>
>      HWND hwnd;<br>
>      gboolean redirecting;<br>
> @@ -85,7 +87,7 @@ static GUdevClient *singleton = NULL;<br>
>  <br>
>  static GUdevDevice *g_udev_device_new(GUdevDeviceInfo *udevinfo);<br>
>  static LRESULT CALLBACK wnd_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);<br>
> -static gboolean get_usb_dev_info(libusb_device *dev, GUdevDeviceInfo *udevinfo);<br>
> +static gboolean get_usb_dev_info(SpiceUsbBackendDevice *dev, GUdevDeviceInfo *udevinfo);<br>
>  <br>
>  //uncomment to debug gudev device lists.<br>
>  //#define DEBUG_GUDEV_DEVICE_LISTS<br>
> @@ -122,8 +124,7 @@ static ssize_t<br>
>  g_udev_client_list_devices(GUdevClient *self, GList **devs,<br>
>                             GError **err, const gchar *name)<br>
>  {<br>
> -    gssize rc;<br>
> -    libusb_device **lusb_list, **dev;<br>
> +    SpiceUsbBackendDevice **lusb_list, **dev;<br>
>      GUdevClientPrivate *priv;<br>
>      GUdevDeviceInfo *udevinfo;<br>
>      GUdevDevice *udevice;<br>
> @@ -136,13 +137,8 @@ g_udev_client_list_devices(GUdevClient *self, GList **devs,<br>
>  <br>
>      g_return_val_if_fail(self->priv->ctx != NULL, -3);<br>
>  <br>
> -    rc = libusb_get_device_list(priv->ctx, &lusb_list);<br>
> -    if (rc < 0) {<br>
> -        const char *errstr = spice_usbutil_libusb_strerror(rc);<br>
> -        g_warning("%s: libusb_get_device_list failed - %s", name, errstr);<br>
> -        g_set_error(err, G_UDEV_CLIENT_ERROR, G_UDEV_CLIENT_LIBUSB_FAILED,<br>
> -                    "%s: Error getting device list from libusb: %s [%"G_GSSIZE_FORMAT"]",<br>
> -                    name, errstr, rc);<br>
> +    lusb_list = spice_usb_backend_get_device_list(priv->ctx);<br>
> +    if (!lusb_list) {<br>
>          return -4;<br>
>      }<br>
<br>
Why did you remove the g_set_error?<br>
<br></blockquote><div><br></div><div>Possible, just because this never happens, empty list it will receive anyway.</div><div>I can return it back, if you like it.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
>  <br>
> @@ -158,7 +154,7 @@ g_udev_client_list_devices(GUdevClient *self, GList **devs,<br>
>          *devs = g_list_prepend(*devs, udevice);<br>
>          n++;<br>
>      }<br>
> -    libusb_free_device_list(lusb_list, 1);<br>
> +    spice_usb_backend_free_device_list(lusb_list);<br>
>  <br>
>      return n;<br>
>  }<br>
> @@ -180,7 +176,6 @@ g_udev_client_initable_init(GInitable *initable, GCancellable *cancellable,<br>
>      GUdevClient *self;<br>
>      GUdevClientPrivate *priv;<br>
>      WNDCLASS wcls;<br>
> -    int rc;<br>
>  <br>
>      g_return_val_if_fail(G_UDEV_IS_CLIENT(initable), FALSE);<br>
>      g_return_val_if_fail(cancellable == NULL, FALSE);<br>
> @@ -188,19 +183,10 @@ g_udev_client_initable_init(GInitable *initable, GCancellable *cancellable,<br>
>      self = G_UDEV_CLIENT(initable);<br>
>      priv = self->priv;<br>
>  <br>
> -    rc = libusb_init(&priv->ctx);<br>
> -    if (rc < 0) {<br>
> -        const char *errstr = spice_usbutil_libusb_strerror(rc);<br>
> -        g_warning("Error initializing USB support: %s [%i]", errstr, rc);<br>
> -        g_set_error(err, G_UDEV_CLIENT_ERROR, G_UDEV_CLIENT_LIBUSB_FAILED,<br>
> -                    "Error initializing USB support: %s [%i]", errstr, rc);<br>
> +    priv->ctx = spice_usb_backend_initialize(err);<br>
> +    if (!priv->ctx) {<br>
>          return FALSE;<br>
>      }<br>
> -#ifdef G_OS_WIN32<br>
> -#if LIBUSB_API_VERSION >= 0x01000106<br>
> -    libusb_set_option(priv->ctx, LIBUSB_OPTION_USE_USBDK);<br>
> -#endif<br>
> -#endif<br>
>  <br>
>      /* get initial device list */<br>
>      if (g_udev_client_list_devices(self, &priv->udev_list, err, __FUNCTION__) < 0) {<br>
> @@ -267,7 +253,7 @@ static void g_udev_client_finalize(GObject *gobject)<br>
>  <br>
>      /* free libusb context initializing by libusb_init() */<br>
>      g_warn_if_fail(priv->ctx != NULL);<br>
> -    libusb_exit(priv->ctx);<br>
> +    spice_usb_backend_finalize(priv->ctx);<br>
>  <br>
>      /* Chain up to the parent class */<br>
>      if (G_OBJECT_CLASS(g_udev_client_parent_class)->finalize)<br>
> @@ -356,23 +342,18 @@ static void g_udev_client_class_init(GUdevClientClass *klass)<br>
>      g_object_class_install_property(gobject_class, PROP_REDIRECTING, pspec);<br>
>  }<br>
>  <br>
> -static gboolean get_usb_dev_info(libusb_device *dev, GUdevDeviceInfo *udevinfo)<br>
> +static gboolean get_usb_dev_info(SpiceUsbBackendDevice *dev, GUdevDeviceInfo *udevinfo)<br>
>  {<br>
> -    struct libusb_device_descriptor desc;<br>
> +    const UsbDeviceInformation* info = spice_usb_backend_device_get_info(dev);<br>
>  <br>
>      g_return_val_if_fail(dev, FALSE);<br>
>      g_return_val_if_fail(udevinfo, FALSE);<br>
>  <br>
> -    if (libusb_get_device_descriptor(dev, &desc) < 0) {<br>
> -        g_warning("cannot get device descriptor %p", dev);<br>
> -        return FALSE;<br>
> -    }<br>
> -<br>
> -    udevinfo->bus   = libusb_get_bus_number(dev);<br>
> -    udevinfo->addr  = libusb_get_device_address(dev);<br>
> -    udevinfo->class = desc.bDeviceClass;<br>
> -    udevinfo->vid   = desc.idVendor;<br>
> -    udevinfo->pid   = desc.idProduct;<br>
> +    udevinfo->bus = info->bus;<br>
> +    udevinfo->addr = info->address;<br>
> +    udevinfo->class = info->class;<br>
> +    udevinfo->vid   = info->vid;<br>
> +    udevinfo->pid   = info->pid;<br>
>      snprintf(udevinfo->sclass, sizeof(udevinfo->sclass), "%d", udevinfo->class);<br>
>      snprintf(udevinfo->sbus,   sizeof(udevinfo->sbus),   "%d", udevinfo->bus);<br>
>      snprintf(udevinfo->saddr,  sizeof(udevinfo->saddr),  "%d", udevinfo->addr);<br>
> @@ -573,7 +554,7 @@ static gboolean g_udev_skip_search(GUdevDevice *udev)<br>
>  #if defined(LIBUSBX_API_VERSION) && (LIBUSBX_API_VERSION >= 0x010000FF)<br>
>              (udevinfo->addr == 1) || /* root hub addr for libusbx >= 1.0.13 */<br>
>  #endif<br>
> -            (udevinfo->class == LIBUSB_CLASS_HUB) || /* hub*/<br>
> +            (udevinfo->class == USB_CLASS_HUB) || /* hub*/<br>
>              (udevinfo->addr == 0)); /* bad address */<br>
>      return skip;<br>
>  }<br>
<br></blockquote><div><br></div><div>Please respond regarding 2 serious questions:</div><div>- whether there is change in indirection level or wrong use of obtained libdev</div><div>- whether we discard this commit to avoid caching of device properties</div><div>If we do not discard it, please let me know whether specific mentioned changes are mandatory.</div><div>My plan is to merge it with CD redirection code, otherwise all this commit does not make any sense.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Christophe<br>
</blockquote></div></div>