mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-02-22 11:22:23 +00:00
qemu: Introduce qemuFindHostdevUSBDevice
The code which looks up a USB device specified by hostdev is duplicated in two places. This patch creates a dedicated function that can be called in both places.
This commit is contained in:
parent
e658daeb58
commit
7bcc7278bf
@ -641,6 +641,44 @@ error:
|
||||
return -1;
|
||||
}
|
||||
|
||||
usbDevice *
|
||||
qemuFindHostdevUSBDevice(virDomainHostdevDefPtr hostdev)
|
||||
{
|
||||
usbDevice *usb = NULL;
|
||||
unsigned vendor = hostdev->source.subsys.u.usb.vendor;
|
||||
unsigned product = hostdev->source.subsys.u.usb.product;
|
||||
unsigned bus = hostdev->source.subsys.u.usb.bus;
|
||||
unsigned device = hostdev->source.subsys.u.usb.device;
|
||||
|
||||
if (vendor && bus) {
|
||||
usb = usbFindDevice(vendor, product, bus, device);
|
||||
|
||||
} else if (vendor && !bus) {
|
||||
usbDeviceList *devs = usbFindDeviceByVendor(vendor, product);
|
||||
if (!devs)
|
||||
return NULL;
|
||||
|
||||
if (usbDeviceListCount(devs) > 1) {
|
||||
virReportError(VIR_ERR_OPERATION_FAILED,
|
||||
_("multiple USB devices for %x:%x, "
|
||||
"use <address> to specify one"), vendor, product);
|
||||
usbDeviceListFree(devs);
|
||||
return NULL;
|
||||
}
|
||||
usb = usbDeviceListGet(devs, 0);
|
||||
usbDeviceListSteal(devs, usb);
|
||||
usbDeviceListFree(devs);
|
||||
|
||||
hostdev->source.subsys.u.usb.bus = usbDeviceGetBus(usb);
|
||||
hostdev->source.subsys.u.usb.device = usbDeviceGetDevno(usb);
|
||||
|
||||
} else if (!vendor && bus) {
|
||||
usb = usbFindDeviceByBus(bus, device);
|
||||
}
|
||||
|
||||
return usb;
|
||||
}
|
||||
|
||||
static int
|
||||
qemuPrepareHostUSBDevices(struct qemud_driver *driver,
|
||||
virDomainDefPtr def)
|
||||
@ -663,45 +701,14 @@ qemuPrepareHostUSBDevices(struct qemud_driver *driver,
|
||||
*/
|
||||
for (i = 0 ; i < nhostdevs ; i++) {
|
||||
virDomainHostdevDefPtr hostdev = hostdevs[i];
|
||||
usbDevice *usb = NULL;
|
||||
usbDevice *usb;
|
||||
|
||||
if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
|
||||
continue;
|
||||
if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB)
|
||||
continue;
|
||||
|
||||
unsigned vendor = hostdev->source.subsys.u.usb.vendor;
|
||||
unsigned product = hostdev->source.subsys.u.usb.product;
|
||||
unsigned bus = hostdev->source.subsys.u.usb.bus;
|
||||
unsigned device = hostdev->source.subsys.u.usb.device;
|
||||
|
||||
if (vendor && bus) {
|
||||
usb = usbFindDevice(vendor, product, bus, device);
|
||||
|
||||
} else if (vendor && !bus) {
|
||||
usbDeviceList *devs = usbFindDeviceByVendor(vendor, product);
|
||||
if (!devs)
|
||||
goto cleanup;
|
||||
|
||||
if (usbDeviceListCount(devs) > 1) {
|
||||
virReportError(VIR_ERR_OPERATION_FAILED,
|
||||
_("multiple USB devices for %x:%x, "
|
||||
"use <address> to specify one"), vendor, product);
|
||||
usbDeviceListFree(devs);
|
||||
goto cleanup;
|
||||
}
|
||||
usb = usbDeviceListGet(devs, 0);
|
||||
usbDeviceListSteal(devs, usb);
|
||||
usbDeviceListFree(devs);
|
||||
|
||||
hostdev->source.subsys.u.usb.bus = usbDeviceGetBus(usb);
|
||||
hostdev->source.subsys.u.usb.device = usbDeviceGetDevno(usb);
|
||||
|
||||
} else if (!vendor && bus) {
|
||||
usb = usbFindDeviceByBus(bus, device);
|
||||
}
|
||||
|
||||
if (!usb)
|
||||
if (!(usb = qemuFindHostdevUSBDevice(hostdev)))
|
||||
goto cleanup;
|
||||
|
||||
if (usbDeviceListAdd(list, usb) < 0) {
|
||||
|
@ -36,6 +36,7 @@ int qemuPrepareHostdevPCIDevices(struct qemud_driver *driver,
|
||||
const unsigned char *uuid,
|
||||
virDomainHostdevDefPtr *hostdevs,
|
||||
int nhostdevs);
|
||||
usbDevice *qemuFindHostdevUSBDevice(virDomainHostdevDefPtr hostdev);
|
||||
int qemuPrepareHostdevUSBDevices(struct qemud_driver *driver,
|
||||
const char *name,
|
||||
usbDeviceList *list);
|
||||
|
@ -1153,38 +1153,7 @@ int qemuDomainAttachHostDevice(struct qemud_driver *driver,
|
||||
goto cleanup;
|
||||
|
||||
if (hostdev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) {
|
||||
unsigned vendor = hostdev->source.subsys.u.usb.vendor;
|
||||
unsigned product = hostdev->source.subsys.u.usb.product;
|
||||
unsigned bus = hostdev->source.subsys.u.usb.bus;
|
||||
unsigned device = hostdev->source.subsys.u.usb.device;
|
||||
|
||||
if (vendor && bus) {
|
||||
usb = usbFindDevice(vendor, product, bus, device);
|
||||
|
||||
} else if (vendor && !bus) {
|
||||
usbDeviceList *devs = usbFindDeviceByVendor(vendor, product);
|
||||
if (!devs)
|
||||
goto cleanup;
|
||||
|
||||
if (usbDeviceListCount(devs) > 1) {
|
||||
virReportError(VIR_ERR_OPERATION_FAILED,
|
||||
_("multiple USB devices for %x:%x, "
|
||||
"use <address> to specify one"), vendor, product);
|
||||
usbDeviceListFree(devs);
|
||||
goto cleanup;
|
||||
}
|
||||
usb = usbDeviceListGet(devs, 0);
|
||||
usbDeviceListSteal(devs, usb);
|
||||
usbDeviceListFree(devs);
|
||||
|
||||
hostdev->source.subsys.u.usb.bus = usbDeviceGetBus(usb);
|
||||
hostdev->source.subsys.u.usb.device = usbDeviceGetDevno(usb);
|
||||
|
||||
} else if (!vendor && bus) {
|
||||
usb = usbFindDeviceByBus(bus, device);
|
||||
}
|
||||
|
||||
if (!usb)
|
||||
if (!(usb = qemuFindHostdevUSBDevice(hostdev)))
|
||||
goto cleanup;
|
||||
|
||||
if (usbDeviceListAdd(list, usb) < 0) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user