qemu/tests/libqos/usb.c
Thomas Huth acd80015fb tests: Introduce generic device hot-plug/hot-unplug functions
A lot of tests provide code for adding and removing a device via the
device_add and device_del QMP commands. Maintaining this code in so many
places is cumbersome and error-prone (some of the code parts check the
responses for device deletion in an incorrect way, for example, we've got
to deal with both, error code and DEVICE_DEL event here). So let's provide
some proper generic functions for adding and removing a device instead.

The code for correctly unplugging a device has been taken from a patch
from Peter Xu.

Reviewed-by: Peter Xu <peterx@redhat.com>
Tested-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Thomas Huth <thuth@redhat.com>
2017-09-15 09:05:18 +02:00

56 lines
1.3 KiB
C

/*
* common code shared by usb tests
*
* Copyright (c) 2014 Red Hat, Inc
*
* Authors:
* Gerd Hoffmann <kraxel@redhat.com>
* John Snow <jsnow@redhat.com>
* Igor Mammedov <imammedo@redhat.com>
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
#include "qemu/osdep.h"
#include "libqtest.h"
#include "hw/usb/uhci-regs.h"
#include "libqos/usb.h"
void qusb_pci_init_one(QPCIBus *pcibus, struct qhc *hc, uint32_t devfn, int bar)
{
hc->dev = qpci_device_find(pcibus, devfn);
g_assert(hc->dev != NULL);
qpci_device_enable(hc->dev);
hc->bar = qpci_iomap(hc->dev, bar, NULL);
}
void uhci_deinit(struct qhc *hc)
{
g_free(hc->dev);
}
void uhci_port_test(struct qhc *hc, int port, uint16_t expect)
{
uint16_t value = qpci_io_readw(hc->dev, hc->bar, 0x10 + 2 * port);
uint16_t mask = ~(UHCI_PORT_WRITE_CLEAR | UHCI_PORT_RSVD1);
g_assert((value & mask) == (expect & mask));
}
void usb_test_hotplug(const char *hcd_id, const int port,
void (*port_check)(void))
{
char *id = g_strdup_printf("usbdev%d", port);
qtest_qmp_device_add("usb-tablet", id, "'port': '%d', 'bus': '%s.0'",
port, hcd_id);
if (port_check) {
port_check();
}
qtest_qmp_device_del(id);
g_free(id);
}