[spice-gtk,09/44] fixup! usb-redir: define interfaces to support emulated devices

Submitted by Frediano Ziglio on July 30, 2019, 12:02 p.m.

Details

Message ID 20190730120331.17967-10-fziglio@redhat.com
State Superseded
Headers show
Series "Follow ups for CD emulation patch series" ( rev: 1 ) in Spice

Not browsing as part of any series.

Commit Message

Frediano Ziglio July 30, 2019, 12:02 p.m.
Remove circular dependency in usb emulation interface
Allows to add or remove usb emulations without changing the
backend
---
 src/usb-backend.c   | 49 ++++++++++++++++-----------------------------
 src/usb-backend.h   | 28 --------------------------
 src/usb-emulation.h | 21 ++++++++++---------
 3 files changed, 29 insertions(+), 69 deletions(-)

Patch hide | download patch | download mbox

diff --git a/src/usb-backend.c b/src/usb-backend.c
index 67fd63c3..75ce12ba 100644
--- a/src/usb-backend.c
+++ b/src/usb-backend.c
@@ -72,7 +72,6 @@  struct _SpiceUsbBackend
     gint redirecting;
 #endif
 
-    SpiceUsbEmulatedDeviceCreate dev_init[USB_DEV_TYPE_MAX];
     /* Mask of allocated device, a specific bit set to 1 to indicate that the device at
      * that address is allocated */
     uint32_t own_devices_mask;
@@ -885,16 +884,6 @@  gchar * spice_usb_backend_device_get_description(SpiceUsbBackendDevice *dev,
     return description;
 }
 
-void spice_usb_backend_register_device_type(SpiceUsbBackend *be,
-                                            UsbEmulatedDeviceType dev_type,
-                                            SpiceUsbEmulatedDeviceCreate init_proc)
-{
-    if (dev_type >= USB_DEV_TYPE_MAX) {
-        g_return_if_reached();
-    }
-    be->dev_init[dev_type] = init_proc;
-}
-
 void spice_usb_backend_device_report_change(SpiceUsbBackend *be,
                                             SpiceUsbBackendDevice *dev)
 {
@@ -916,26 +905,22 @@  void spice_usb_backend_device_eject(SpiceUsbBackend *be, SpiceUsbBackendDevice *
     spice_usb_backend_device_unref(dev);
 }
 
-gboolean spice_usb_backend_create_device(SpiceUsbBackend *be,
-                                         UsbEmulatedDeviceType dev_type,
-                                         UsbCreateDeviceParameters *param)
+SpiceUsbBackendDevice*
+spice_usb_backend_create_device(SpiceUsbBackend *be,
+                                SpiceUsbEmulatedDeviceCreate create_proc,
+                                void *create_params,
+                                GError **err)
 {
-    SpiceUsbEmulatedDevice *edev = NULL;
-    SpiceUsbBackendDevice  *dev;
+    SpiceUsbEmulatedDevice *edev;
+    SpiceUsbBackendDevice *dev;
     struct libusb_device_descriptor *desc;
     uint16_t device_desc_size;
     uint8_t address = 0;
 
-    if (dev_type >= USB_DEV_TYPE_MAX || !be->dev_init[dev_type]) {
-        param->error = g_error_new(SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,
-                                   _("can't create device - not supported"));
-        return FALSE;
-    }
-
     if (be->own_devices_mask == 0xffffffff) {
-        param->error = g_error_new(SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,
-                                   _("can't create device - limit reached"));
-        return FALSE;
+        g_set_error(err, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,
+                    _("can't create device - limit reached"));
+        return NULL;
     }
     for (address = 0; address < 32; ++address) {
         if (~be->own_devices_mask & (1 << address)) {
@@ -948,20 +933,20 @@  gboolean spice_usb_backend_create_device(SpiceUsbBackend *be,
     dev->device_info.address = address;
     dev->ref_count = 1;
 
-    if (be->dev_init[dev_type](be, dev, param, &edev)) {
+    dev->edev = edev = create_proc(be, dev, create_params, err);
+    if (edev == NULL) {
         spice_usb_backend_device_unref(dev);
-        return FALSE;
+        return NULL;
     }
-    dev->edev = edev;
 
     if (!device_ops(edev)->get_descriptor(edev, LIBUSB_DT_DEVICE, 0,
                                           (void **)&desc, &device_desc_size)
         || device_desc_size != sizeof(*desc)) {
 
         spice_usb_backend_device_unref(dev);
-        param->error = g_error_new(SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,
-                                   _("can't create device - internal error"));
-        return FALSE;
+        g_set_error(err, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,
+                    _("can't create device - internal error"));
+        return NULL;
     }
 
     be->own_devices_mask |= 1 << address;
@@ -977,7 +962,7 @@  gboolean spice_usb_backend_create_device(SpiceUsbBackend *be,
         be->hotplug_callback(be->hotplug_user_data, dev, TRUE);
     }
 
-    return TRUE;
+    return dev;
 }
 
 #endif /* USB_REDIR */
diff --git a/src/usb-backend.h b/src/usb-backend.h
index df24fa56..51aca3b1 100644
--- a/src/usb-backend.h
+++ b/src/usb-backend.h
@@ -31,13 +31,6 @@  typedef struct _SpiceUsbBackend SpiceUsbBackend;
 typedef struct _SpiceUsbBackendDevice SpiceUsbBackendDevice;
 typedef struct _SpiceUsbBackendChannel SpiceUsbBackendChannel;
 
-typedef enum
-{
-    USB_DEV_TYPE_NONE,
-    USB_DEV_TYPE_CD,
-    USB_DEV_TYPE_MAX
-} UsbEmulatedDeviceType;
-
 #define BUS_NUMBER_FOR_EMULATED_USB G_MAXUINT16
 
 typedef struct UsbDeviceInformation
@@ -99,27 +92,6 @@  void spice_usb_backend_channel_get_guest_filter(SpiceUsbBackendChannel *ch,
 void spice_usb_backend_return_write_data(SpiceUsbBackendChannel *ch, void *data);
 gchar *spice_usb_backend_device_get_description(SpiceUsbBackendDevice *dev, const gchar *format);
 
-typedef struct UsbCreateDeviceParameters
-{
-    GError *error;
-    union
-    {
-        struct
-        {
-            const char *filename;
-            uint32_t   delete_on_eject  : 1;
-        } create_cd;
-    } device_param;
-
-} UsbCreateDeviceParameters;
-
-/* API related to emulated devices */
-gboolean spice_usb_backend_create_device(SpiceUsbBackend *be,
-                                         UsbEmulatedDeviceType dev_type,
-                                         UsbCreateDeviceParameters *param);
-void spice_usb_backend_device_eject(SpiceUsbBackend *be, SpiceUsbBackendDevice *device);
-void spice_usb_backend_device_report_change(SpiceUsbBackend *be, SpiceUsbBackendDevice *device);
-
 G_END_DECLS
 
 #endif
diff --git a/src/usb-emulation.h b/src/usb-emulation.h
index b3654bf7..9ea819a4 100644
--- a/src/usb-emulation.h
+++ b/src/usb-emulation.h
@@ -26,10 +26,11 @@ 
 #include "usb-backend.h"
 
 typedef struct SpiceUsbEmulatedDevice SpiceUsbEmulatedDevice;
-typedef int (*SpiceUsbEmulatedDeviceCreate)(SpiceUsbBackend *be,
-                                            SpiceUsbBackendDevice *parent,
-                                            UsbCreateDeviceParameters *param,
-                                            SpiceUsbEmulatedDevice **device);
+typedef SpiceUsbEmulatedDevice*
+(*SpiceUsbEmulatedDeviceCreate)(SpiceUsbBackend *be,
+                                SpiceUsbBackendDevice *parent,
+                                void *create_params,
+                                GError **err);
 
 /*
     function table for emulated USB device
@@ -79,10 +80,12 @@  static inline const UsbDeviceOps *device_ops(SpiceUsbEmulatedDevice *dev)
     return (const UsbDeviceOps *)dev;
 }
 
-void spice_usb_backend_register_device_type(SpiceUsbBackend *be,
-                                            UsbEmulatedDeviceType dev_type,
-                                            SpiceUsbEmulatedDeviceCreate init_proc);
-/* supported device types */
-void spice_usb_device_register_cd(SpiceUsbBackend *be);
+SpiceUsbBackendDevice*
+spice_usb_backend_create_device(SpiceUsbBackend *be,
+                                SpiceUsbEmulatedDeviceCreate create_proc,
+                                void *create_params,
+                                GError **err);
+void spice_usb_backend_device_eject(SpiceUsbBackend *be, SpiceUsbBackendDevice *device);
+void spice_usb_backend_device_report_change(SpiceUsbBackend *be, SpiceUsbBackendDevice *device);
 
 #endif