File 770bf992-libxl-usb-controller-creation.patch of Package libvirt

From 770bf992c6287ee98dc444ddb0f5bef048f92be8 Mon Sep 17 00:00:00 2001
From: Chunyan Liu <cyliu@suse.com>
Date: Wed, 15 Jun 2016 14:00:09 +0800
Subject: [PATCH 2/6] libxl: support USB controllers in creation time

To support USB Controller in xen guest domains, just add
USB controller in domain config xml as following:
<controller type='usb' model='qusb2' ports='4'/>

Signed-off-by: Chunyan Liu <cyliu@suse.com>
---
 src/libxl/libxl_conf.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/libxl/libxl_conf.h |  4 +++
 2 files changed, 88 insertions(+)

Index: libvirt-2.0.0/src/libxl/libxl_conf.c
===================================================================
--- libvirt-2.0.0.orig/src/libxl/libxl_conf.c
+++ libvirt-2.0.0/src/libxl/libxl_conf.c
@@ -1516,6 +1516,87 @@ int libxlDriverConfigLoadFile(libxlDrive
 
 #ifdef LIBXL_HAVE_PVUSB
 int
+libxlMakeUSBController(virDomainControllerDefPtr controller,
+                       libxl_device_usbctrl *usbctrl)
+{
+    usbctrl->devid = controller->idx;
+
+    if (controller->type != VIR_DOMAIN_CONTROLLER_TYPE_USB)
+        return -1;
+
+    if (controller->model == -1) {
+        usbctrl->version = 2;
+        usbctrl->type = LIBXL_USBCTRL_TYPE_QUSB;
+    } else {
+        switch (controller->model) {
+        case VIR_DOMAIN_CONTROLLER_MODEL_USB_QUSB1:
+            usbctrl->version = 1;
+            usbctrl->type = LIBXL_USBCTRL_TYPE_QUSB;
+            break;
+
+        case VIR_DOMAIN_CONTROLLER_MODEL_USB_QUSB2:
+            usbctrl->version = 2;
+            usbctrl->type = LIBXL_USBCTRL_TYPE_QUSB;
+            break;
+
+        default:
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                           _("unsupported usb model"));
+            return -1;
+        }
+    }
+
+    if (controller->opts.usbopts.ports == -1)
+        usbctrl->ports = 8;
+    else
+        usbctrl->ports = controller->opts.usbopts.ports;
+
+    return 0;
+}
+
+static int
+libxlMakeUSBControllerList(virDomainDefPtr def, libxl_domain_config *d_config)
+{
+    virDomainControllerDefPtr *l_controllers = def->controllers;
+    size_t ncontrollers = def->ncontrollers;
+    size_t nusbctrls = 0;
+    libxl_device_usbctrl *x_usbctrls;
+    size_t i;
+
+    if (ncontrollers == 0)
+        return 0;
+
+    if (VIR_ALLOC_N(x_usbctrls, ncontrollers) < 0)
+        return -1;
+
+    for (i = 0; i < ncontrollers; i++) {
+        if (l_controllers[i]->type != VIR_DOMAIN_CONTROLLER_TYPE_USB)
+            continue;
+
+        libxl_device_usbctrl_init(&x_usbctrls[nusbctrls]);
+
+        if (libxlMakeUSBController(l_controllers[i],
+                                   &x_usbctrls[nusbctrls]) < 0)
+            goto error;
+
+        nusbctrls++;
+    }
+
+    VIR_SHRINK_N(x_usbctrls, ncontrollers, ncontrollers - nusbctrls);
+    d_config->usbctrls = x_usbctrls;
+    d_config->num_usbctrls = nusbctrls;
+
+    return 0;
+
+ error:
+    for (i = 0; i < nusbctrls; i++)
+        libxl_device_usbctrl_dispose(&x_usbctrls[i]);
+
+    VIR_FREE(x_usbctrls);
+    return -1;
+}
+
+int
 libxlMakeUSB(virDomainHostdevDefPtr hostdev, libxl_device_usbdev *usbdev)
 {
     virDomainHostdevSubsysUSBPtr usbsrc = &hostdev->source.subsys.u.usb;
@@ -1800,6 +1881,9 @@ libxlBuildDomainConfig(virPortAllocatorP
         return -1;
 
 #ifdef LIBXL_HAVE_PVUSB
+    if (libxlMakeUSBControllerList(def, d_config) < 0)
+        return -1;
+
     if (libxlMakeUSBList(def, d_config) < 0)
         return -1;
 #endif
Index: libvirt-2.0.0/src/libxl/libxl_conf.h
===================================================================
--- libvirt-2.0.0.orig/src/libxl/libxl_conf.h
+++ libvirt-2.0.0/src/libxl/libxl_conf.h
@@ -186,6 +186,10 @@ libxlMakePCI(virDomainHostdevDefPtr host
 
 # ifdef LIBXL_HAVE_PVUSB
 int
+libxlMakeUSBController(virDomainControllerDefPtr controller,
+                       libxl_device_usbctrl *usbctrl);
+
+int
 libxlMakeUSB(virDomainHostdevDefPtr hostdev, libxl_device_usbdev *usbdev);
 # endif