qemu/tests/usb-hcd-uhci-test.c
<<
>>
Prefs
   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.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
  18static QOSState *qs;
  19
  20static void test_uhci_init(void)
  21{
  22}
  23
  24static void test_port(int port)
  25{
  26    struct qhc uhci;
  27
  28    g_assert(port > 0);
  29    qusb_pci_init_one(qs->pcibus, &uhci, QPCI_DEVFN(0x1d, 0), 4);
  30    uhci_port_test(&uhci, port - 1, UHCI_PORT_CCS);
  31    uhci_deinit(&uhci);
  32}
  33
  34static void test_port_1(void)
  35{
  36    test_port(1);
  37}
  38
  39static void test_port_2(void)
  40{
  41    test_port(2);
  42}
  43
  44static void test_uhci_hotplug(void)
  45{
  46    usb_test_hotplug("uhci", 2, test_port_2);
  47}
  48
  49static void test_usb_storage_hotplug(void)
  50{
  51    QDict *response;
  52
  53    response = qmp("{'execute': 'device_add',"
  54                   " 'arguments': {"
  55                   "   'driver': 'usb-storage',"
  56                   "   'drive': 'drive0',"
  57                   "   'id': 'usbdev0'"
  58                   "}}");
  59    g_assert(response);
  60    g_assert(!qdict_haskey(response, "error"));
  61    QDECREF(response);
  62
  63    response = qmp("{'execute': 'device_del',"
  64                           " 'arguments': {"
  65                           "   'id': 'usbdev0'"
  66                           "}}");
  67    g_assert(response);
  68    g_assert(!qdict_haskey(response, "error"));
  69    QDECREF(response);
  70
  71    response = qmp("");
  72    g_assert(response);
  73    g_assert(qdict_haskey(response, "event"));
  74    g_assert(!strcmp(qdict_get_str(response, "event"), "DEVICE_DELETED"));
  75    QDECREF(response);
  76}
  77
  78int main(int argc, char **argv)
  79{
  80    const char *arch = qtest_get_arch();
  81    const char *cmd = "-device piix3-usb-uhci,id=uhci,addr=1d.0"
  82                      " -drive id=drive0,if=none,file=null-co://,format=raw"
  83                      " -device usb-tablet,bus=uhci.0,port=1";
  84    int ret;
  85
  86    g_test_init(&argc, &argv, NULL);
  87
  88    qtest_add_func("/uhci/pci/init", test_uhci_init);
  89    qtest_add_func("/uhci/pci/port1", test_port_1);
  90    qtest_add_func("/uhci/pci/hotplug", test_uhci_hotplug);
  91    qtest_add_func("/uhci/pci/hotplug/usb-storage", test_usb_storage_hotplug);
  92
  93    if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
  94        qs = qtest_pc_boot(cmd);
  95    } else if (strcmp(arch, "ppc64") == 0) {
  96        qs = qtest_spapr_boot(cmd);
  97    } else {
  98        g_printerr("usb-hcd-uhci-test tests are only "
  99                   "available on x86 or ppc64\n");
 100        exit(EXIT_FAILURE);
 101    }
 102    ret = g_test_run();
 103    qtest_shutdown(qs);
 104
 105    return ret;
 106}
 107