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 <glib.h>
  12#include "libqtest.h"
  13#include "libqos/usb.h"
  14#include "hw/usb/uhci-regs.h"
  15
  16
  17static void test_uhci_init(void)
  18{
  19}
  20
  21static void test_port(int port)
  22{
  23    QPCIBus *pcibus;
  24    struct qhc uhci;
  25
  26    g_assert(port > 0);
  27    pcibus = qpci_init_pc();
  28    g_assert(pcibus != NULL);
  29    qusb_pci_init_one(pcibus, &uhci, QPCI_DEVFN(0x1d, 0), 4);
  30    uhci_port_test(&uhci, port - 1, UHCI_PORT_CCS);
  31}
  32
  33static void test_port_1(void)
  34{
  35    test_port(1);
  36}
  37
  38static void test_port_2(void)
  39{
  40    test_port(2);
  41}
  42
  43static void test_uhci_hotplug(void)
  44{
  45    usb_test_hotplug("uhci", 2, test_port_2);
  46}
  47
  48static void test_usb_storage_hotplug(void)
  49{
  50    QDict *response;
  51
  52    response = qmp("{'execute': 'device_add',"
  53                   " 'arguments': {"
  54                   "   'driver': 'usb-storage',"
  55                   "   'drive': 'drive0',"
  56                   "   'id': 'usbdev0'"
  57                   "}}");
  58    g_assert(response);
  59    g_assert(!qdict_haskey(response, "error"));
  60    QDECREF(response);
  61
  62    response = qmp("{'execute': 'device_del',"
  63                           " 'arguments': {"
  64                           "   'id': 'usbdev0'"
  65                           "}}");
  66    g_assert(response);
  67    g_assert(!qdict_haskey(response, "error"));
  68    QDECREF(response);
  69
  70    response = qmp("");
  71    g_assert(response);
  72    g_assert(qdict_haskey(response, "event"));
  73    g_assert(!strcmp(qdict_get_str(response, "event"), "DEVICE_DELETED"));
  74    QDECREF(response);
  75}
  76
  77int main(int argc, char **argv)
  78{
  79    int ret;
  80
  81    g_test_init(&argc, &argv, NULL);
  82
  83    qtest_add_func("/uhci/pci/init", test_uhci_init);
  84    qtest_add_func("/uhci/pci/port1", test_port_1);
  85    qtest_add_func("/uhci/pci/hotplug", test_uhci_hotplug);
  86    qtest_add_func("/uhci/pci/hotplug/usb-storage", test_usb_storage_hotplug);
  87
  88    qtest_start("-device piix3-usb-uhci,id=uhci,addr=1d.0"
  89                " -drive id=drive0,if=none,file=/dev/null,format=raw"
  90                " -device usb-tablet,bus=uhci.0,port=1");
  91    ret = g_test_run();
  92    qtest_end();
  93
  94    return ret;
  95}
  96