Andreas Färber | cc900d3 | 2014-03-30 20:25:38 +0200 | [diff] [blame] | 1 | /* |
| 2 | * QTest testcase for USB EHCI |
| 3 | * |
| 4 | * Copyright (c) 2014 SUSE LINUX Products GmbH |
| 5 | * |
| 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 | */ |
| 9 | |
Peter Maydell | e532b2e | 2016-01-26 18:17:12 +0000 | [diff] [blame] | 10 | #include "qemu/osdep.h" |
Andreas Färber | cc900d3 | 2014-03-30 20:25:38 +0200 | [diff] [blame] | 11 | #include "libqtest.h" |
Gerd Hoffmann | d81d410 | 2014-05-07 16:39:11 +0200 | [diff] [blame] | 12 | #include "libqos/pci-pc.h" |
Gerd Hoffmann | d81d410 | 2014-05-07 16:39:11 +0200 | [diff] [blame] | 13 | #include "hw/usb/uhci-regs.h" |
| 14 | #include "hw/usb/ehci-regs.h" |
Igor Mammedov | b0354ec | 2014-09-26 09:28:12 +0000 | [diff] [blame] | 15 | #include "libqos/usb.h" |
Gerd Hoffmann | d81d410 | 2014-05-07 16:39:11 +0200 | [diff] [blame] | 16 | |
| 17 | static QPCIBus *pcibus; |
| 18 | static struct qhc uhci1; |
| 19 | static struct qhc uhci2; |
| 20 | static struct qhc uhci3; |
| 21 | static struct qhc ehci1; |
| 22 | |
| 23 | /* helpers */ |
| 24 | |
Gerd Hoffmann | d81d410 | 2014-05-07 16:39:11 +0200 | [diff] [blame] | 25 | #if 0 |
| 26 | static void uhci_port_update(struct qhc *hc, int port, |
| 27 | uint16_t set, uint16_t clear) |
| 28 | { |
| 29 | void *addr = hc->base + 0x10 + 2 * port; |
| 30 | uint16_t value; |
| 31 | |
| 32 | value = qpci_io_readw(hc->dev, addr); |
| 33 | value |= set; |
| 34 | value &= ~clear; |
| 35 | qpci_io_writew(hc->dev, addr, value); |
| 36 | } |
| 37 | #endif |
| 38 | |
Gerd Hoffmann | d81d410 | 2014-05-07 16:39:11 +0200 | [diff] [blame] | 39 | static void ehci_port_test(struct qhc *hc, int port, uint32_t expect) |
| 40 | { |
David Gibson | b4ba67d | 2016-10-24 15:52:06 +1100 | [diff] [blame] | 41 | uint32_t value = qpci_io_readl(hc->dev, hc->bar, 0x64 + 4 * port); |
Gerd Hoffmann | d81d410 | 2014-05-07 16:39:11 +0200 | [diff] [blame] | 42 | uint16_t mask = ~(PORTSC_CSC | PORTSC_PEDC | PORTSC_OCC); |
| 43 | |
| 44 | #if 0 |
| 45 | fprintf(stderr, "%s: %d, have 0x%08x, want 0x%08x\n", |
| 46 | __func__, port, value & mask, expect & mask); |
| 47 | #endif |
| 48 | g_assert((value & mask) == (expect & mask)); |
| 49 | } |
| 50 | |
| 51 | /* tests */ |
| 52 | |
| 53 | static void pci_init(void) |
| 54 | { |
| 55 | if (pcibus) { |
| 56 | return; |
| 57 | } |
Laurent Vivier | 2ecd7e2 | 2016-09-29 12:32:45 +0200 | [diff] [blame] | 58 | pcibus = qpci_init_pc(NULL); |
Gerd Hoffmann | d81d410 | 2014-05-07 16:39:11 +0200 | [diff] [blame] | 59 | g_assert(pcibus != NULL); |
| 60 | |
Igor Mammedov | b0354ec | 2014-09-26 09:28:12 +0000 | [diff] [blame] | 61 | qusb_pci_init_one(pcibus, &uhci1, QPCI_DEVFN(0x1d, 0), 4); |
| 62 | qusb_pci_init_one(pcibus, &uhci2, QPCI_DEVFN(0x1d, 1), 4); |
| 63 | qusb_pci_init_one(pcibus, &uhci3, QPCI_DEVFN(0x1d, 2), 4); |
| 64 | qusb_pci_init_one(pcibus, &ehci1, QPCI_DEVFN(0x1d, 7), 0); |
Gerd Hoffmann | d81d410 | 2014-05-07 16:39:11 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static void pci_uhci_port_1(void) |
| 68 | { |
| 69 | g_assert(pcibus != NULL); |
| 70 | |
| 71 | uhci_port_test(&uhci1, 0, UHCI_PORT_CCS); /* usb-tablet */ |
| 72 | uhci_port_test(&uhci1, 1, UHCI_PORT_CCS); /* usb-storage */ |
| 73 | uhci_port_test(&uhci2, 0, 0); |
| 74 | uhci_port_test(&uhci2, 1, 0); |
| 75 | uhci_port_test(&uhci3, 0, 0); |
| 76 | uhci_port_test(&uhci3, 1, 0); |
| 77 | } |
| 78 | |
| 79 | static void pci_ehci_port_1(void) |
| 80 | { |
| 81 | int i; |
| 82 | |
| 83 | g_assert(pcibus != NULL); |
| 84 | |
| 85 | for (i = 0; i < 6; i++) { |
| 86 | ehci_port_test(&ehci1, i, PORTSC_POWNER | PORTSC_PPOWER); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | static void pci_ehci_config(void) |
| 91 | { |
| 92 | /* hands over all ports from companion uhci to ehci */ |
David Gibson | b4ba67d | 2016-10-24 15:52:06 +1100 | [diff] [blame] | 93 | qpci_io_writew(ehci1.dev, ehci1.bar, 0x60, 1); |
Gerd Hoffmann | d81d410 | 2014-05-07 16:39:11 +0200 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static void pci_uhci_port_2(void) |
| 97 | { |
| 98 | g_assert(pcibus != NULL); |
| 99 | |
| 100 | uhci_port_test(&uhci1, 0, 0); /* usb-tablet, @ehci */ |
| 101 | uhci_port_test(&uhci1, 1, 0); /* usb-storage, @ehci */ |
| 102 | uhci_port_test(&uhci2, 0, 0); |
| 103 | uhci_port_test(&uhci2, 1, 0); |
| 104 | uhci_port_test(&uhci3, 0, 0); |
| 105 | uhci_port_test(&uhci3, 1, 0); |
| 106 | } |
| 107 | |
| 108 | static void pci_ehci_port_2(void) |
| 109 | { |
| 110 | static uint32_t expect[] = { |
| 111 | PORTSC_PPOWER | PORTSC_CONNECT, /* usb-tablet */ |
| 112 | PORTSC_PPOWER | PORTSC_CONNECT, /* usb-storage */ |
| 113 | PORTSC_PPOWER, |
| 114 | PORTSC_PPOWER, |
| 115 | PORTSC_PPOWER, |
| 116 | PORTSC_PPOWER, |
| 117 | }; |
| 118 | int i; |
| 119 | |
| 120 | g_assert(pcibus != NULL); |
| 121 | |
| 122 | for (i = 0; i < 6; i++) { |
| 123 | ehci_port_test(&ehci1, i, expect[i]); |
| 124 | } |
Andreas Färber | cc900d3 | 2014-03-30 20:25:38 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Igor Mammedov | b393768 | 2014-09-26 09:28:14 +0000 | [diff] [blame] | 127 | static void pci_ehci_port_3_hotplug(void) |
| 128 | { |
| 129 | /* check for presence of hotplugged usb-tablet */ |
| 130 | g_assert(pcibus != NULL); |
| 131 | ehci_port_test(&ehci1, 2, PORTSC_PPOWER | PORTSC_CONNECT); |
| 132 | } |
| 133 | |
| 134 | static void pci_ehci_port_hotplug(void) |
| 135 | { |
| 136 | usb_test_hotplug("ich9-ehci-1", 3, pci_ehci_port_3_hotplug); |
| 137 | } |
| 138 | |
| 139 | |
Andreas Färber | cc900d3 | 2014-03-30 20:25:38 +0200 | [diff] [blame] | 140 | int main(int argc, char **argv) |
| 141 | { |
| 142 | int ret; |
| 143 | |
| 144 | g_test_init(&argc, &argv, NULL); |
Gerd Hoffmann | d81d410 | 2014-05-07 16:39:11 +0200 | [diff] [blame] | 145 | qtest_add_func("/ehci/pci/init", pci_init); |
| 146 | qtest_add_func("/ehci/pci/uhci-port-1", pci_uhci_port_1); |
| 147 | qtest_add_func("/ehci/pci/ehci-port-1", pci_ehci_port_1); |
| 148 | qtest_add_func("/ehci/pci/ehci-config", pci_ehci_config); |
| 149 | qtest_add_func("/ehci/pci/uhci-port-2", pci_uhci_port_2); |
| 150 | qtest_add_func("/ehci/pci/ehci-port-2", pci_ehci_port_2); |
Igor Mammedov | b393768 | 2014-09-26 09:28:14 +0000 | [diff] [blame] | 151 | qtest_add_func("/ehci/pci/ehci-port-3-hotplug", pci_ehci_port_hotplug); |
Andreas Färber | cc900d3 | 2014-03-30 20:25:38 +0200 | [diff] [blame] | 152 | |
| 153 | qtest_start("-machine q35 -device ich9-usb-ehci1,bus=pcie.0,addr=1d.7," |
| 154 | "multifunction=on,id=ich9-ehci-1 " |
| 155 | "-device ich9-usb-uhci1,bus=pcie.0,addr=1d.0," |
| 156 | "multifunction=on,masterbus=ich9-ehci-1.0,firstport=0 " |
| 157 | "-device ich9-usb-uhci2,bus=pcie.0,addr=1d.1," |
| 158 | "multifunction=on,masterbus=ich9-ehci-1.0,firstport=2 " |
| 159 | "-device ich9-usb-uhci3,bus=pcie.0,addr=1d.2," |
Gerd Hoffmann | d81d410 | 2014-05-07 16:39:11 +0200 | [diff] [blame] | 160 | "multifunction=on,masterbus=ich9-ehci-1.0,firstport=4 " |
| 161 | "-drive if=none,id=usbcdrom,media=cdrom " |
| 162 | "-device usb-tablet,bus=ich9-ehci-1.0,port=1,usb_version=1 " |
| 163 | "-device usb-storage,bus=ich9-ehci-1.0,port=2,drive=usbcdrom "); |
Andreas Färber | cc900d3 | 2014-03-30 20:25:38 +0200 | [diff] [blame] | 164 | ret = g_test_run(); |
| 165 | |
| 166 | qtest_end(); |
| 167 | |
| 168 | return ret; |
| 169 | } |