1 /*
2  * QTest testcase for USB UHCI controller
3  *
4  * Copyright (c) 2014 HUAWEI TECHNOLOGIES CO., LTD.
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 
10 #include "qemu/osdep.h"
11 #include "libqtest-single.h"
12 #include "libqos/libqos.h"
13 #include "libqos/usb.h"
14 #include "libqos/libqos-pc.h"
15 #include "libqos/libqos-spapr.h"
16 #include "hw/usb/uhci-regs.h"
17 
18 static QOSState *qs;
19 
test_port(int port)20 static void test_port(int port)
21 {
22     struct qhc uhci;
23 
24     g_assert(port > 0);
25     qusb_pci_init_one(qs->pcibus, &uhci, QPCI_DEVFN(0x1d, 0), 4);
26     uhci_port_test(&uhci, port - 1, UHCI_PORT_CCS);
27     uhci_deinit(&uhci);
28 }
29 
test_port_1(void)30 static void test_port_1(void)
31 {
32     test_port(1);
33 }
34 
test_port_2(void)35 static void test_port_2(void)
36 {
37     test_port(2);
38 }
39 
test_uhci_hotplug(void)40 static void test_uhci_hotplug(void)
41 {
42     usb_test_hotplug(global_qtest, "uhci", "2", test_port_2);
43 }
44 
test_usb_storage_hotplug(void)45 static void test_usb_storage_hotplug(void)
46 {
47     QTestState *qts = global_qtest;
48 
49     qtest_qmp_device_add(qts, "usb-storage", "usbdev0", "{'drive': 'drive0'}");
50 
51     qtest_qmp_device_del(qts, "usbdev0");
52 }
53 
main(int argc,char ** argv)54 int main(int argc, char **argv)
55 {
56     const char *arch = qtest_get_arch();
57     const char *cmd = "-device piix3-usb-uhci,id=uhci,addr=1d.0"
58                       " -drive id=drive0,if=none,file=null-co://,"
59                       "file.read-zeroes=on,format=raw"
60                       " -device usb-tablet,bus=uhci.0,port=1";
61     int ret;
62 
63     g_test_init(&argc, &argv, NULL);
64 
65     if (!qtest_has_device("piix3-usb-uhci")) {
66         g_debug("piix3-usb-uhci not available");
67         return 0;
68     }
69 
70     qtest_add_func("/uhci/pci/port1", test_port_1);
71     qtest_add_func("/uhci/pci/hotplug", test_uhci_hotplug);
72     if (qtest_has_device("usb-storage")) {
73         qtest_add_func("/uhci/pci/hotplug/usb-storage", test_usb_storage_hotplug);
74     }
75 
76     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
77         qs = qtest_pc_boot("%s", cmd);
78     } else if (strcmp(arch, "ppc64") == 0) {
79         qs = qtest_spapr_boot("%s", cmd);
80     } else {
81         g_printerr("usb-hcd-uhci-test tests are only "
82                    "available on x86 or ppc64\n");
83         exit(EXIT_FAILURE);
84     }
85     global_qtest = qs->qts;
86     ret = g_test_run();
87     qtest_shutdown(qs);
88 
89     return ret;
90 }
91