These changes are the raw update to qemu-2.6.
[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 "qemu/osdep.h"
15 #include <glib.h>
16 #include "libqtest.h"
17 #include "hw/usb/uhci-regs.h"
18 #include "libqos/usb.h"
19
20 void qusb_pci_init_one(QPCIBus *pcibus, struct qhc *hc, uint32_t devfn, int bar)
21 {
22     hc->dev = qpci_device_find(pcibus, devfn);
23     g_assert(hc->dev != NULL);
24     qpci_device_enable(hc->dev);
25     hc->base = qpci_iomap(hc->dev, bar, NULL);
26     g_assert(hc->base != NULL);
27 }
28
29 void uhci_port_test(struct qhc *hc, int port, uint16_t expect)
30 {
31     void *addr = hc->base + 0x10 + 2 * port;
32     uint16_t value = qpci_io_readw(hc->dev, addr);
33     uint16_t mask = ~(UHCI_PORT_WRITE_CLEAR | UHCI_PORT_RSVD1);
34
35     g_assert((value & mask) == (expect & mask));
36 }
37
38 void usb_test_hotplug(const char *hcd_id, const int port,
39                       void (*port_check)(void))
40 {
41     QDict *response;
42     char  *cmd;
43
44     cmd = g_strdup_printf("{'execute': 'device_add',"
45                           " 'arguments': {"
46                           "   'driver': 'usb-tablet',"
47                           "   'port': '%d',"
48                           "   'bus': '%s.0',"
49                           "   'id': 'usbdev%d'"
50                           "}}", port, hcd_id, port);
51     response = qmp(cmd);
52     g_free(cmd);
53     g_assert(response);
54     g_assert(!qdict_haskey(response, "error"));
55     QDECREF(response);
56
57     if (port_check) {
58         port_check();
59     }
60
61     cmd = g_strdup_printf("{'execute': 'device_del',"
62                            " 'arguments': {"
63                            "   'id': 'usbdev%d'"
64                            "}}", port);
65     response = qmp(cmd);
66     g_free(cmd);
67     g_assert(response);
68     g_assert(qdict_haskey(response, "event"));
69     g_assert(!strcmp(qdict_get_str(response, "event"), "DEVICE_DELETED"));
70 }