vbox: cleanup vboxAttachUSB

This cleanup flattens deeply nested code.

Signed-off-by: Ryota Ozaki <ozaki.ryota@gmail.com>
This commit is contained in:
Ryota Ozaki 2013-11-21 23:41:04 +09:00 committed by Laine Stump
parent 55d1285ef4
commit 1868b4574e

View File

@ -4852,94 +4852,99 @@ vboxAttachUSB(virDomainDefPtr def, vboxGlobalData *data, IMachine *machine)
* usual * usual
*/ */
for (i = 0; i < def->nhostdevs; i++) { for (i = 0; i < def->nhostdevs; i++) {
if (def->hostdevs[i]->mode == if (def->hostdevs[i]->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
VIR_DOMAIN_HOSTDEV_MODE_SUBSYS) { continue;
if (def->hostdevs[i]->source.subsys.type ==
VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) { if (def->hostdevs[i]->source.subsys.type !=
if (def->hostdevs[i]->source.subsys.u.usb.vendor || VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB)
def->hostdevs[i]->source.subsys.u.usb.product) { continue;
VIR_DEBUG("USB Device detected, VendorId:0x%x, ProductId:0x%x",
def->hostdevs[i]->source.subsys.u.usb.vendor, if (!def->hostdevs[i]->source.subsys.u.usb.vendor &&
def->hostdevs[i]->source.subsys.u.usb.product); !def->hostdevs[i]->source.subsys.u.usb.product)
isUSB = true; continue;
break;
} VIR_DEBUG("USB Device detected, VendorId:0x%x, ProductId:0x%x",
} def->hostdevs[i]->source.subsys.u.usb.vendor,
} def->hostdevs[i]->source.subsys.u.usb.product);
isUSB = true;
break;
} }
if (isUSB) { if (!isUSB)
/* First Start the USB Controller and then loop return;
* to attach USB Devices to it
*/ /* First Start the USB Controller and then loop
machine->vtbl->GetUSBController(machine, &USBController); * to attach USB Devices to it
if (USBController) { */
USBController->vtbl->SetEnabled(USBController, 1); machine->vtbl->GetUSBController(machine, &USBController);
if (!USBController)
return;
USBController->vtbl->SetEnabled(USBController, 1);
#if VBOX_API_VERSION < 4002 #if VBOX_API_VERSION < 4002
USBController->vtbl->SetEnabledEhci(USBController, 1); USBController->vtbl->SetEnabledEhci(USBController, 1);
#else #else
USBController->vtbl->SetEnabledEHCI(USBController, 1); USBController->vtbl->SetEnabledEHCI(USBController, 1);
#endif #endif
for (i = 0; i < def->nhostdevs; i++) { for (i = 0; i < def->nhostdevs; i++) {
if (def->hostdevs[i]->mode == char *filtername = NULL;
VIR_DOMAIN_HOSTDEV_MODE_SUBSYS) { PRUnichar *filternameUtf16 = NULL;
if (def->hostdevs[i]->source.subsys.type == IUSBDeviceFilter *filter = NULL;
VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) { PRUnichar *vendorIdUtf16 = NULL;
char vendorId[40] = {0};
PRUnichar *productIdUtf16 = NULL;
char productId[40] = {0};
char *filtername = NULL; if (def->hostdevs[i]->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
PRUnichar *filternameUtf16 = NULL; continue;
IUSBDeviceFilter *filter = NULL;
/* Zero pad for nice alignment when fewer than 9999 if (def->hostdevs[i]->source.subsys.type !=
* devices. VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB)
*/ continue;
if (virAsprintf(&filtername, "filter%04zu", i) >= 0) {
VBOX_UTF8_TO_UTF16(filtername, &filternameUtf16);
VIR_FREE(filtername);
USBController->vtbl->CreateDeviceFilter(USBController,
filternameUtf16,
&filter);
}
VBOX_UTF16_FREE(filternameUtf16);
if (filter && /* Zero pad for nice alignment when fewer than 9999
(def->hostdevs[i]->source.subsys.u.usb.vendor || * devices.
def->hostdevs[i]->source.subsys.u.usb.product)) { */
if (virAsprintf(&filtername, "filter%04zu", i) >= 0) {
PRUnichar *vendorIdUtf16 = NULL; VBOX_UTF8_TO_UTF16(filtername, &filternameUtf16);
char vendorId[40] = {0}; VIR_FREE(filtername);
PRUnichar *productIdUtf16 = NULL; USBController->vtbl->CreateDeviceFilter(USBController,
char productId[40] = {0}; filternameUtf16,
&filter);
if (def->hostdevs[i]->source.subsys.u.usb.vendor) {
snprintf(vendorId, sizeof(vendorId), "%x",
def->hostdevs[i]->source.subsys.u.usb.vendor);
VBOX_UTF8_TO_UTF16(vendorId, &vendorIdUtf16);
filter->vtbl->SetVendorId(filter, vendorIdUtf16);
VBOX_UTF16_FREE(vendorIdUtf16);
}
if (def->hostdevs[i]->source.subsys.u.usb.product) {
snprintf(productId, sizeof(productId), "%x",
def->hostdevs[i]->source.subsys.u.usb.product);
VBOX_UTF8_TO_UTF16(productId, &productIdUtf16);
filter->vtbl->SetProductId(filter,
productIdUtf16);
VBOX_UTF16_FREE(productIdUtf16);
}
filter->vtbl->SetActive(filter, 1);
USBController->vtbl->InsertDeviceFilter(USBController,
i,
filter);
VBOX_RELEASE(filter);
}
}
}
}
VBOX_RELEASE(USBController);
} }
VBOX_UTF16_FREE(filternameUtf16);
if (!filter)
continue;
if (!def->hostdevs[i]->source.subsys.u.usb.vendor &&
!def->hostdevs[i]->source.subsys.u.usb.product)
continue;
if (def->hostdevs[i]->source.subsys.u.usb.vendor) {
snprintf(vendorId, sizeof(vendorId), "%x",
def->hostdevs[i]->source.subsys.u.usb.vendor);
VBOX_UTF8_TO_UTF16(vendorId, &vendorIdUtf16);
filter->vtbl->SetVendorId(filter, vendorIdUtf16);
VBOX_UTF16_FREE(vendorIdUtf16);
}
if (def->hostdevs[i]->source.subsys.u.usb.product) {
snprintf(productId, sizeof(productId), "%x",
def->hostdevs[i]->source.subsys.u.usb.product);
VBOX_UTF8_TO_UTF16(productId, &productIdUtf16);
filter->vtbl->SetProductId(filter,
productIdUtf16);
VBOX_UTF16_FREE(productIdUtf16);
}
filter->vtbl->SetActive(filter, 1);
USBController->vtbl->InsertDeviceFilter(USBController,
i,
filter);
VBOX_RELEASE(filter);
} }
VBOX_RELEASE(USBController);
} }
static void static void