[spice-gtk,v3,3/9] usb-redir: move implementation of device description to USB backend

Submitted by Yuri Benditovich on Aug. 12, 2019, 9:57 a.m.

Details

Message ID 20190812095729.32692-4-yuri.benditovich@daynix.com
State Superseded
Headers show
Series "added feature of sharing CD image" ( rev: 5 ) in Spice

Not browsing as part of any series.

Commit Message

Yuri Benditovich Aug. 12, 2019, 9:57 a.m.
For local USB device the USB backend returns the same device
description as spice-usb-manager did, for emulated devices it
uses the description provided by device's implementation.

Signed-off-by: Yuri Benditovich <yuri.benditovich@daynix.com>
Acked-by: Frediano Ziglio <fziglio@redhat.com>
---
 src/usb-backend.c        | 41 ++++++++++++++++++++++++++++++++++++++++
 src/usb-backend.h        |  1 +
 src/usb-device-manager.c | 29 +---------------------------
 3 files changed, 43 insertions(+), 28 deletions(-)

Patch hide | download patch | download mbox

diff --git a/src/usb-backend.c b/src/usb-backend.c
index 7f9fcc1..aa11c79 100644
--- a/src/usb-backend.c
+++ b/src/usb-backend.c
@@ -42,6 +42,7 @@ 
 #include "usb-emulation.h"
 #include "channel-usbredir-priv.h"
 #include "spice-channel-priv.h"
+#include "usbutil.h"
 
 #define LOUD_DEBUG(x, ...)
 
@@ -844,6 +845,46 @@  spice_usb_backend_channel_get_guest_filter(SpiceUsbBackendChannel *ch,
     }
 }
 
+gchar * spice_usb_backend_device_get_description(SpiceUsbBackendDevice *dev,
+                                                 const gchar *format)
+{
+    guint16 bus, address, vid, pid;
+    gchar *description, *descriptor, *manufacturer = NULL, *product = NULL;
+
+    g_return_val_if_fail(dev != NULL, NULL);
+
+    bus     = dev->device_info.bus;
+    address = dev->device_info.address;
+    vid     = dev->device_info.vid;
+    pid     = dev->device_info.pid;
+
+    if ((vid > 0) && (pid > 0)) {
+        descriptor = g_strdup_printf("[%04x:%04x]", vid, pid);
+    } else {
+        descriptor = g_strdup("");
+    }
+
+    if (dev->libusb_device) {
+        spice_usb_util_get_device_strings(bus, address, vid, pid,
+                                          &manufacturer, &product);
+    } else {
+        product = device_ops(dev->edev)->get_product_description(dev->edev);
+    }
+
+    if (!format) {
+        format = _("%s %s %s at %d-%d");
+    }
+
+    description = g_strdup_printf(format, manufacturer ? manufacturer : "",
+                                  product, descriptor, bus, address);
+
+    g_free(manufacturer);
+    g_free(descriptor);
+    g_free(product);
+
+    return description;
+}
+
 void spice_usb_backend_device_report_change(SpiceUsbBackend *be,
                                             SpiceUsbBackendDevice *dev)
 {
diff --git a/src/usb-backend.h b/src/usb-backend.h
index cd3ff09..c694dbb 100644
--- a/src/usb-backend.h
+++ b/src/usb-backend.h
@@ -90,5 +90,6 @@  void spice_usb_backend_channel_get_guest_filter(SpiceUsbBackendChannel *ch,
                                                 const struct usbredirfilter_rule  **rules,
                                                 int *count);
 void spice_usb_backend_return_write_data(SpiceUsbBackendChannel *ch, void *data);
+gchar *spice_usb_backend_device_get_description(SpiceUsbBackendDevice *dev, const gchar *format);
 
 G_END_DECLS
diff --git a/src/usb-device-manager.c b/src/usb-device-manager.c
index b3db5de..5834e03 100644
--- a/src/usb-device-manager.c
+++ b/src/usb-device-manager.c
@@ -32,7 +32,6 @@ 
 #endif
 
 #include "channel-usbredir-priv.h"
-#include "usbutil.h"
 #endif
 
 #include "spice-session-priv.h"
@@ -1439,35 +1438,9 @@  spice_usb_device_manager_can_redirect_device(SpiceUsbDeviceManager  *self,
 gchar *spice_usb_device_get_description(SpiceUsbDevice *device, const gchar *format)
 {
 #ifdef USE_USBREDIR
-    guint16 bus, address, vid, pid;
-    gchar *description, *descriptor, *manufacturer = NULL, *product = NULL;
-
     g_return_val_if_fail(device != NULL, NULL);
 
-    bus     = spice_usb_device_get_busnum(device);
-    address = spice_usb_device_get_devaddr(device);
-    vid     = spice_usb_device_get_vid(device);
-    pid     = spice_usb_device_get_pid(device);
-
-    if ((vid > 0) && (pid > 0)) {
-        descriptor = g_strdup_printf("[%04x:%04x]", vid, pid);
-    } else {
-        descriptor = g_strdup("");
-    }
-
-    spice_usb_util_get_device_strings(bus, address, vid, pid,
-                                      &manufacturer, &product);
-
-    if (!format)
-        format = _("%s %s %s at %d-%d");
-
-    description = g_strdup_printf(format, manufacturer, product, descriptor, bus, address);
-
-    g_free(manufacturer);
-    g_free(descriptor);
-    g_free(product);
-
-    return description;
+    return spice_usb_backend_device_get_description(device->bdev, format);
 #else
     return NULL;
 #endif