Merge remote-tracking branch 'remotes/elmarco/tags/chrfe-pull-request' into staging
[qemu/kevin.git] / tests / libqos / usb.c
blob0cdfaecda7f418a6c824696fa9648b523e41351e
1 /*
2 * common code shared by usb tests
4 * Copyright (c) 2014 Red Hat, Inc
6 * Authors:
7 * Gerd Hoffmann <kraxel@redhat.com>
8 * John Snow <jsnow@redhat.com>
9 * Igor Mammedov <imammedo@redhat.com>
11 * This work is licensed under the terms of the GNU GPL, version 2 or later.
12 * See the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include "libqtest.h"
16 #include "hw/usb/uhci-regs.h"
17 #include "libqos/usb.h"
19 void qusb_pci_init_one(QPCIBus *pcibus, struct qhc *hc, uint32_t devfn, int bar)
21 hc->dev = qpci_device_find(pcibus, devfn);
22 g_assert(hc->dev != NULL);
23 qpci_device_enable(hc->dev);
24 hc->bar = qpci_iomap(hc->dev, bar, NULL);
27 void uhci_deinit(struct qhc *hc)
29 g_free(hc->dev);
32 void uhci_port_test(struct qhc *hc, int port, uint16_t expect)
34 uint16_t value = qpci_io_readw(hc->dev, hc->bar, 0x10 + 2 * port);
35 uint16_t mask = ~(UHCI_PORT_WRITE_CLEAR | UHCI_PORT_RSVD1);
37 g_assert((value & mask) == (expect & mask));
40 void usb_test_hotplug(const char *hcd_id, const int port,
41 void (*port_check)(void))
43 QDict *response;
44 char *cmd;
46 cmd = g_strdup_printf("{'execute': 'device_add',"
47 " 'arguments': {"
48 " 'driver': 'usb-tablet',"
49 " 'port': '%d',"
50 " 'bus': '%s.0',"
51 " 'id': 'usbdev%d'"
52 "}}", port, hcd_id, port);
53 response = qmp(cmd);
54 g_free(cmd);
55 g_assert(response);
56 g_assert(!qdict_haskey(response, "error"));
57 QDECREF(response);
59 if (port_check) {
60 port_check();
63 cmd = g_strdup_printf("{'execute': 'device_del',"
64 " 'arguments': {"
65 " 'id': 'usbdev%d'"
66 "}}", port);
67 response = qmp(cmd);
68 g_free(cmd);
69 g_assert(response);
70 g_assert(qdict_haskey(response, "event"));
71 g_assert(!strcmp(qdict_get_str(response, "event"), "DEVICE_DELETED"));
72 QDECREF(response);