Add qemu 2.4.0
[kvmfornfv.git] / qemu / tests / libqos / usb.c
1 /*
2  * common code shared by usb tests
3  *
4  * Copyright (c) 2014 Red Hat, Inc
5  *
6  * Authors:
7  *     Gerd Hoffmann <kraxel@redhat.com>
8  *     John Snow <jsnow@redhat.com>
9  *     Igor Mammedov <imammedo@redhat.com>
10  *
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.
13  */
14 #include <glib.h>
15 #include <string.h>
16 #include "libqtest.h"
17 #include "qemu/osdep.h"
18 #include "hw/usb/uhci-regs.h"
19 #include "libqos/usb.h"
20
21 void qusb_pci_init_one(QPCIBus *pcibus, struct qhc *hc, uint32_t devfn, int bar)
22 {
23     hc->dev = qpci_device_find(pcibus, devfn);
24     g_assert(hc->dev != NULL);
25     qpci_device_enable(hc->dev);
26     hc->base = qpci_iomap(hc->dev, bar, NULL);
27     g_assert(hc->base != NULL);
28 }
29
30 void uhci_port_test(struct qhc *hc, int port, uint16_t expect)
31 {
32     void *addr = hc->base + 0x10 + 2 * port;
33     uint16_t value = qpci_io_readw(hc->dev, addr);
34     uint16_t mask = ~(UHCI_PORT_WRITE_CLEAR | UHCI_PORT_RSVD1);
35
36     g_assert((value & mask) == (expect & mask));
37 }
38
39 void usb_test_hotplug(const char *hcd_id, const int port,
40                       void (*port_check)(void))
41 {
42     QDict *response;
43     char  *cmd;
44
45     cmd = g_strdup_printf("{'execute': 'device_add',"
46                           " 'arguments': {"
47                           "   'driver': 'usb-tablet',"
48                           "   'port': '%d',"
49                           "   'bus': '%s.0',"
50                           "   'id': 'usbdev%d'"
51                           "}}", port, hcd_id, port);
52     response = qmp(cmd);
53     g_free(cmd);
54     g_assert(response);
55     g_assert(!qdict_haskey(response, "error"));
56     QDECREF(response);
57
58     if (port_check) {
59         port_check();
60     }
61
62     cmd = g_strdup_printf("{'execute': 'device_del',"
63                            " 'arguments': {"
64                            "   'id': 'usbdev%d'"
65                            "}}", port);
66     response = qmp(cmd);
67     g_free(cmd);
68     g_assert(response);
69     g_assert(qdict_haskey(response, "event"));
70     g_assert(!strcmp(qdict_get_str(response, "event"), "DEVICE_DELETED"));
71 }