qemu/tests/libqos/usb.c
<<
>>
Prefs
   1/*
   2 * common code shared by usb tests
   3 *
   4 * Copyright (c) 2014 Red Hat, Inc
   5 *
   6 * Authors:
   7 *     Gerd Hoffmann <kraxel@redhat.com>
   8 *     John Snow <jsnow@redhat.com>
   9 *     Igor Mammedov <imammedo@redhat.com>
  10 *
  11 * This work is licensed under the terms of the GNU GPL, version 2 or later.
  12 * See the COPYING file in the top-level directory.
  13 */
  14#include "qemu/osdep.h"
  15#include "libqtest.h"
  16#include "hw/usb/uhci-regs.h"
  17#include "libqos/usb.h"
  18
  19void qusb_pci_init_one(QPCIBus *pcibus, struct qhc *hc, uint32_t devfn, int bar)
  20{
  21    hc->dev = qpci_device_find(pcibus, devfn);
  22    g_assert(hc->dev != NULL);
  23    qpci_device_enable(hc->dev);
  24    hc->bar = qpci_iomap(hc->dev, bar, NULL);
  25}
  26
  27void uhci_port_test(struct qhc *hc, int port, uint16_t expect)
  28{
  29    uint16_t value = qpci_io_readw(hc->dev, hc->bar, 0x10 + 2 * port);
  30    uint16_t mask = ~(UHCI_PORT_WRITE_CLEAR | UHCI_PORT_RSVD1);
  31
  32    g_assert((value & mask) == (expect & mask));
  33}
  34
  35void usb_test_hotplug(const char *hcd_id, const int port,
  36                      void (*port_check)(void))
  37{
  38    QDict *response;
  39    char  *cmd;
  40
  41    cmd = g_strdup_printf("{'execute': 'device_add',"
  42                          " 'arguments': {"
  43                          "   'driver': 'usb-tablet',"
  44                          "   'port': '%d',"
  45                          "   'bus': '%s.0',"
  46                          "   'id': 'usbdev%d'"
  47                          "}}", port, hcd_id, port);
  48    response = qmp(cmd);
  49    g_free(cmd);
  50    g_assert(response);
  51    g_assert(!qdict_haskey(response, "error"));
  52    QDECREF(response);
  53
  54    if (port_check) {
  55        port_check();
  56    }
  57
  58    cmd = g_strdup_printf("{'execute': 'device_del',"
  59                           " 'arguments': {"
  60                           "   'id': 'usbdev%d'"
  61                           "}}", port);
  62    response = qmp(cmd);
  63    g_free(cmd);
  64    g_assert(response);
  65    g_assert(qdict_haskey(response, "event"));
  66    g_assert(!strcmp(qdict_get_str(response, "event"), "DEVICE_DELETED"));
  67}
  68