block: Move BlockAcctStats into BlockBackend
[qemu.git] / tests / usb-hcd-ehci-test.c
blob75073bf24e9b913da7f1a97ddff0f6167c2b8fa1
1 /*
2 * QTest testcase for USB EHCI
4 * Copyright (c) 2014 SUSE LINUX Products GmbH
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
8 */
10 #include <glib.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include "libqtest.h"
14 #include "libqos/pci-pc.h"
15 #include "qemu/osdep.h"
16 #include "hw/usb/uhci-regs.h"
17 #include "hw/usb/ehci-regs.h"
18 #include "libqos/usb.h"
20 static QPCIBus *pcibus;
21 static struct qhc uhci1;
22 static struct qhc uhci2;
23 static struct qhc uhci3;
24 static struct qhc ehci1;
26 /* helpers */
28 #if 0
29 static void uhci_port_update(struct qhc *hc, int port,
30 uint16_t set, uint16_t clear)
32 void *addr = hc->base + 0x10 + 2 * port;
33 uint16_t value;
35 value = qpci_io_readw(hc->dev, addr);
36 value |= set;
37 value &= ~clear;
38 qpci_io_writew(hc->dev, addr, value);
40 #endif
42 static void ehci_port_test(struct qhc *hc, int port, uint32_t expect)
44 void *addr = hc->base + 0x64 + 4 * port;
45 uint32_t value = qpci_io_readl(hc->dev, addr);
46 uint16_t mask = ~(PORTSC_CSC | PORTSC_PEDC | PORTSC_OCC);
48 #if 0
49 fprintf(stderr, "%s: %d, have 0x%08x, want 0x%08x\n",
50 __func__, port, value & mask, expect & mask);
51 #endif
52 g_assert((value & mask) == (expect & mask));
55 /* tests */
57 static void pci_init(void)
59 if (pcibus) {
60 return;
62 pcibus = qpci_init_pc();
63 g_assert(pcibus != NULL);
65 qusb_pci_init_one(pcibus, &uhci1, QPCI_DEVFN(0x1d, 0), 4);
66 qusb_pci_init_one(pcibus, &uhci2, QPCI_DEVFN(0x1d, 1), 4);
67 qusb_pci_init_one(pcibus, &uhci3, QPCI_DEVFN(0x1d, 2), 4);
68 qusb_pci_init_one(pcibus, &ehci1, QPCI_DEVFN(0x1d, 7), 0);
71 static void pci_uhci_port_1(void)
73 g_assert(pcibus != NULL);
75 uhci_port_test(&uhci1, 0, UHCI_PORT_CCS); /* usb-tablet */
76 uhci_port_test(&uhci1, 1, UHCI_PORT_CCS); /* usb-storage */
77 uhci_port_test(&uhci2, 0, 0);
78 uhci_port_test(&uhci2, 1, 0);
79 uhci_port_test(&uhci3, 0, 0);
80 uhci_port_test(&uhci3, 1, 0);
83 static void pci_ehci_port_1(void)
85 int i;
87 g_assert(pcibus != NULL);
89 for (i = 0; i < 6; i++) {
90 ehci_port_test(&ehci1, i, PORTSC_POWNER | PORTSC_PPOWER);
94 static void pci_ehci_config(void)
96 /* hands over all ports from companion uhci to ehci */
97 qpci_io_writew(ehci1.dev, ehci1.base + 0x60, 1);
100 static void pci_uhci_port_2(void)
102 g_assert(pcibus != NULL);
104 uhci_port_test(&uhci1, 0, 0); /* usb-tablet, @ehci */
105 uhci_port_test(&uhci1, 1, 0); /* usb-storage, @ehci */
106 uhci_port_test(&uhci2, 0, 0);
107 uhci_port_test(&uhci2, 1, 0);
108 uhci_port_test(&uhci3, 0, 0);
109 uhci_port_test(&uhci3, 1, 0);
112 static void pci_ehci_port_2(void)
114 static uint32_t expect[] = {
115 PORTSC_PPOWER | PORTSC_CONNECT, /* usb-tablet */
116 PORTSC_PPOWER | PORTSC_CONNECT, /* usb-storage */
117 PORTSC_PPOWER,
118 PORTSC_PPOWER,
119 PORTSC_PPOWER,
120 PORTSC_PPOWER,
122 int i;
124 g_assert(pcibus != NULL);
126 for (i = 0; i < 6; i++) {
127 ehci_port_test(&ehci1, i, expect[i]);
131 static void pci_ehci_port_3_hotplug(void)
133 /* check for presence of hotplugged usb-tablet */
134 g_assert(pcibus != NULL);
135 ehci_port_test(&ehci1, 2, PORTSC_PPOWER | PORTSC_CONNECT);
138 static void pci_ehci_port_hotplug(void)
140 usb_test_hotplug("ich9-ehci-1", 3, pci_ehci_port_3_hotplug);
144 int main(int argc, char **argv)
146 int ret;
148 g_test_init(&argc, &argv, NULL);
149 qtest_add_func("/ehci/pci/init", pci_init);
150 qtest_add_func("/ehci/pci/uhci-port-1", pci_uhci_port_1);
151 qtest_add_func("/ehci/pci/ehci-port-1", pci_ehci_port_1);
152 qtest_add_func("/ehci/pci/ehci-config", pci_ehci_config);
153 qtest_add_func("/ehci/pci/uhci-port-2", pci_uhci_port_2);
154 qtest_add_func("/ehci/pci/ehci-port-2", pci_ehci_port_2);
155 qtest_add_func("/ehci/pci/ehci-port-3-hotplug", pci_ehci_port_hotplug);
157 qtest_start("-machine q35 -device ich9-usb-ehci1,bus=pcie.0,addr=1d.7,"
158 "multifunction=on,id=ich9-ehci-1 "
159 "-device ich9-usb-uhci1,bus=pcie.0,addr=1d.0,"
160 "multifunction=on,masterbus=ich9-ehci-1.0,firstport=0 "
161 "-device ich9-usb-uhci2,bus=pcie.0,addr=1d.1,"
162 "multifunction=on,masterbus=ich9-ehci-1.0,firstport=2 "
163 "-device ich9-usb-uhci3,bus=pcie.0,addr=1d.2,"
164 "multifunction=on,masterbus=ich9-ehci-1.0,firstport=4 "
165 "-drive if=none,id=usbcdrom,media=cdrom "
166 "-device usb-tablet,bus=ich9-ehci-1.0,port=1,usb_version=1 "
167 "-device usb-storage,bus=ich9-ehci-1.0,port=2,drive=usbcdrom ");
168 ret = g_test_run();
170 qtest_end();
172 return ret;