thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone
218e99
From 328574a81c4879246b3f222447654eced05ab570 Mon Sep 17 00:00:00 2001
218e99
From: Gerd Hoffmann <kraxel@redhat.com>
218e99
Date: Tue, 5 Nov 2013 10:54:20 +0100
218e99
Subject: [PATCH 14/29] usb: remove old usb-host code
218e99
218e99
RH-Author: Gerd Hoffmann <kraxel@redhat.com>
218e99
Message-id: <1383648860-6526-2-git-send-email-kraxel@redhat.com>
218e99
Patchwork-id: 55374
218e99
O-Subject: [RHEL-7 qemu-kvm PATCH 1/1] usb: remove old usb-host code
218e99
Bugzilla: 1001144
218e99
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
218e99
RH-Acked-by: Bandan Das <bsd@redhat.com>
218e99
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
218e99
218e99
The usb-host code has been rewritten for qemu 1.5 to use libusb,
218e99
the old code has been left in as temporary fallback.  Now we are
218e99
two releases further out, targeting the 1.7 release.  No major
218e99
issues with the new code poped up until now.  Time to remove it
218e99
from tre tree.  Should we ever need it again for some reason --
218e99
git has a copy for us in the history.
218e99
218e99
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
218e99
(cherry picked from commit b5613fdcb0e03d47852582c252942512f050b5b6)
218e99
218e99
Conflicts:
218e99
	configure
218e99
	hw/usb/host-linux.c
218e99
---
218e99
 configure           |   24 +-
218e99
 hw/usb/host-bsd.c   |  639 -----------------
218e99
 hw/usb/host-linux.c | 1910 ---------------------------------------------------
218e99
 3 files changed, 4 insertions(+), 2569 deletions(-)
218e99
 delete mode 100644 hw/usb/host-bsd.c
218e99
 delete mode 100644 hw/usb/host-linux.c
218e99
218e99
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
218e99
---
218e99
 configure           |   24 +-
218e99
 hw/usb/host-bsd.c   |  639 -----------------
218e99
 hw/usb/host-linux.c | 1910 ---------------------------------------------------
218e99
 3 files changed, 4 insertions(+), 2569 deletions(-)
218e99
 delete mode 100644 hw/usb/host-bsd.c
218e99
 delete mode 100644 hw/usb/host-linux.c
218e99
218e99
diff --git a/configure b/configure
218e99
index df587d7..4830f7e 100755
218e99
--- a/configure
218e99
+++ b/configure
218e99
@@ -538,7 +538,6 @@ Haiku)
218e99
   audio_possible_drivers="oss alsa sdl esd pa"
218e99
   linux="yes"
218e99
   linux_user="yes"
218e99
-  usb="linux"
218e99
   kvm="yes"
218e99
   vhost_net="yes"
218e99
   vhost_scsi="yes"
218e99
@@ -551,7 +550,6 @@ esac
218e99
 
218e99
 if [ "$bsd" = "yes" ] ; then
218e99
   if [ "$darwin" != "yes" ] ; then
218e99
-    usb="bsd"
218e99
     bsd_user="yes"
218e99
   fi
218e99
 fi
218e99
@@ -3036,7 +3034,6 @@ fi
218e99
 if test "$libusb" != "no" ; then
218e99
     if $pkg_config --atleast-version=1.0.13 libusb-1.0 >/dev/null 2>&1 ; then
218e99
         libusb="yes"
218e99
-	usb="libusb"
218e99
         libusb_cflags=$($pkg_config --cflags libusb-1.0 2>/dev/null)
218e99
         libusb_libs=$($pkg_config --libs libusb-1.0 2>/dev/null)
218e99
         QEMU_CFLAGS="$QEMU_CFLAGS $libusb_cflags"
218e99
@@ -3944,24 +3941,11 @@ if test "$virtio_blk_data_plane" = "yes" ; then
218e99
 fi
218e99
 
218e99
 # USB host support
218e99
-case "$usb" in
218e99
-linux)
218e99
-  echo "HOST_USB=linux legacy" >> $config_host_mak
218e99
-;;
218e99
-bsd)
218e99
-  echo "HOST_USB=bsd" >> $config_host_mak
218e99
-;;
218e99
-libusb)
218e99
-  if test "$linux" = "yes"; then
218e99
-    echo "HOST_USB=libusb linux legacy" >> $config_host_mak
218e99
-  else
218e99
-    echo "HOST_USB=libusb legacy" >> $config_host_mak
218e99
-  fi
218e99
-;;
218e99
-*)
218e99
+if test "$libusb" = "yes"; then
218e99
+  echo "HOST_USB=libusb legacy" >> $config_host_mak
218e99
+else
218e99
   echo "HOST_USB=stub" >> $config_host_mak
218e99
-;;
218e99
-esac
218e99
+fi
218e99
 
218e99
 # TPM passthrough support?
218e99
 if test "$tpm" = "yes"; then
218e99
diff --git a/hw/usb/host-bsd.c b/hw/usb/host-bsd.c
218e99
deleted file mode 100644
218e99
index 39f2281..0000000
218e99
--- a/hw/usb/host-bsd.c
218e99
+++ /dev/null
218e99
@@ -1,639 +0,0 @@
218e99
-/*
218e99
- * BSD host USB redirector
218e99
- *
218e99
- * Copyright (c) 2006 Lonnie Mendez
218e99
- * Portions of code and concepts borrowed from
218e99
- * usb-linux.c and libusb's bsd.c and are copyright their respective owners.
218e99
- *
218e99
- * Permission is hereby granted, free of charge, to any person obtaining a copy
218e99
- * of this software and associated documentation files (the "Software"), to deal
218e99
- * in the Software without restriction, including without limitation the rights
218e99
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
218e99
- * copies of the Software, and to permit persons to whom the Software is
218e99
- * furnished to do so, subject to the following conditions:
218e99
- *
218e99
- * The above copyright notice and this permission notice shall be included in
218e99
- * all copies or substantial portions of the Software.
218e99
- *
218e99
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
218e99
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
218e99
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
218e99
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
218e99
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
218e99
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
218e99
- * THE SOFTWARE.
218e99
- */
218e99
-
218e99
-#include "qemu-common.h"
218e99
-#include "monitor/monitor.h"
218e99
-#include "hw/usb.h"
218e99
-
218e99
-/* usb.h declares these */
218e99
-#undef USB_SPEED_HIGH
218e99
-#undef USB_SPEED_FULL
218e99
-#undef USB_SPEED_LOW
218e99
-
218e99
-#include <sys/ioctl.h>
218e99
-#ifndef __DragonFly__
218e99
-#include <dev/usb/usb.h>
218e99
-#else
218e99
-#include <bus/usb/usb.h>
218e99
-#endif
218e99
-
218e99
-/* This value has maximum potential at 16.
218e99
- * You should also set hw.usb.debug to gain
218e99
- * more detailed view.
218e99
- */
218e99
-//#define DEBUG
218e99
-#define UGEN_DEBUG_LEVEL 0
218e99
-
218e99
-
218e99
-typedef int USBScanFunc(void *opaque, int bus_num, int addr, int class_id,
218e99
-                        int vendor_id, int product_id,
218e99
-                        const char *product_name, int speed);
218e99
-static int usb_host_find_device(int *pbus_num, int *paddr,
218e99
-                                const char *devname);
218e99
-
218e99
-typedef struct USBHostDevice {
218e99
-    USBDevice dev;
218e99
-    int ep_fd[USB_MAX_ENDPOINTS];
218e99
-    int devfd;
218e99
-    char devpath[32];
218e99
-} USBHostDevice;
218e99
-
218e99
-
218e99
-static int ensure_ep_open(USBHostDevice *dev, int ep, int mode)
218e99
-{
218e99
-    char buf[32];
218e99
-    int fd;
218e99
-
218e99
-    /* Get the address for this endpoint */
218e99
-    ep = UE_GET_ADDR(ep);
218e99
-
218e99
-    if (dev->ep_fd[ep] < 0) {
218e99
-#if defined(__FreeBSD__) || defined(__DragonFly__)
218e99
-        snprintf(buf, sizeof(buf) - 1, "%s.%d", dev->devpath, ep);
218e99
-#else
218e99
-        snprintf(buf, sizeof(buf) - 1, "%s.%02d", dev->devpath, ep);
218e99
-#endif
218e99
-        /* Try to open it O_RDWR first for those devices which have in and out
218e99
-         * endpoints with the same address (eg 0x02 and 0x82)
218e99
-         */
218e99
-        fd = open(buf, O_RDWR);
218e99
-        if (fd < 0 && errno == ENXIO)
218e99
-            fd = open(buf, mode);
218e99
-        if (fd < 0) {
218e99
-#ifdef DEBUG
218e99
-            printf("ensure_ep_open: failed to open device endpoint %s: %s\n",
218e99
-                   buf, strerror(errno));
218e99
-#endif
218e99
-        }
218e99
-        dev->ep_fd[ep] = fd;
218e99
-    }
218e99
-
218e99
-    return dev->ep_fd[ep];
218e99
-}
218e99
-
218e99
-static void ensure_eps_closed(USBHostDevice *dev)
218e99
-{
218e99
-    int epnum = 1;
218e99
-
218e99
-    if (!dev)
218e99
-        return;
218e99
-
218e99
-    while (epnum < USB_MAX_ENDPOINTS) {
218e99
-        if (dev->ep_fd[epnum] >= 0) {
218e99
-            close(dev->ep_fd[epnum]);
218e99
-            dev->ep_fd[epnum] = -1;
218e99
-        }
218e99
-        epnum++;
218e99
-    }
218e99
-}
218e99
-
218e99
-static void usb_host_handle_reset(USBDevice *dev)
218e99
-{
218e99
-#if 0
218e99
-    USBHostDevice *s = (USBHostDevice *)dev;
218e99
-#endif
218e99
-}
218e99
-
218e99
-/* XXX:
218e99
- * -check device states against transfer requests
218e99
- *  and return appropriate response
218e99
- */
218e99
-static void usb_host_handle_control(USBDevice *dev,
218e99
-                                   USBPacket *p,
218e99
-                                   int request,
218e99
-                                   int value,
218e99
-                                   int index,
218e99
-                                   int length,
218e99
-                                   uint8_t *data)
218e99
-{
218e99
-    USBHostDevice *s = (USBHostDevice *)dev;
218e99
-    struct usb_ctl_request req;
218e99
-    struct usb_alt_interface aiface;
218e99
-    int ret, timeout = 50;
218e99
-
218e99
-    if ((request >> 8) == UT_WRITE_DEVICE &&
218e99
-        (request & 0xff) == UR_SET_ADDRESS) {
218e99
-
218e99
-        /* specific SET_ADDRESS support */
218e99
-        dev->addr = value;
218e99
-    } else if ((request >> 8) == UT_WRITE_DEVICE &&
218e99
-               (request & 0xff) == UR_SET_CONFIG) {
218e99
-
218e99
-        ensure_eps_closed(s); /* can't do this without all eps closed */
218e99
-
218e99
-        ret = ioctl(s->devfd, USB_SET_CONFIG, &value);
218e99
-        if (ret < 0) {
218e99
-#ifdef DEBUG
218e99
-            printf("handle_control: failed to set configuration - %s\n",
218e99
-                   strerror(errno));
218e99
-#endif
218e99
-            p->status = USB_RET_STALL;
218e99
-        }
218e99
-    } else if ((request >> 8) == UT_WRITE_INTERFACE &&
218e99
-               (request & 0xff) == UR_SET_INTERFACE) {
218e99
-
218e99
-        aiface.uai_interface_index = index;
218e99
-        aiface.uai_alt_no = value;
218e99
-
218e99
-        ensure_eps_closed(s); /* can't do this without all eps closed */
218e99
-        ret = ioctl(s->devfd, USB_SET_ALTINTERFACE, &aiface);
218e99
-        if (ret < 0) {
218e99
-#ifdef DEBUG
218e99
-            printf("handle_control: failed to set alternate interface - %s\n",
218e99
-                   strerror(errno));
218e99
-#endif
218e99
-            p->status = USB_RET_STALL;
218e99
-        }
218e99
-    } else {
218e99
-        req.ucr_request.bmRequestType = request >> 8;
218e99
-        req.ucr_request.bRequest = request & 0xff;
218e99
-        USETW(req.ucr_request.wValue, value);
218e99
-        USETW(req.ucr_request.wIndex, index);
218e99
-        USETW(req.ucr_request.wLength, length);
218e99
-        req.ucr_data = data;
218e99
-        req.ucr_flags = USBD_SHORT_XFER_OK;
218e99
-
218e99
-        ret = ioctl(s->devfd, USB_SET_TIMEOUT, &timeout);
218e99
-#if defined(__NetBSD__) || defined(__OpenBSD__)
218e99
-        if (ret < 0 && errno != EINVAL) {
218e99
-#else
218e99
-        if (ret < 0) {
218e99
-#endif
218e99
-#ifdef DEBUG
218e99
-            printf("handle_control: setting timeout failed - %s\n",
218e99
-                   strerror(errno));
218e99
-#endif
218e99
-        }
218e99
-
218e99
-        ret = ioctl(s->devfd, USB_DO_REQUEST, &req;;
218e99
-        /* ugen returns EIO for usbd_do_request_ no matter what
218e99
-         * happens with the transfer */
218e99
-        if (ret < 0) {
218e99
-#ifdef DEBUG
218e99
-            printf("handle_control: error after request - %s\n",
218e99
-                   strerror(errno));
218e99
-#endif
218e99
-            p->status = USB_RET_NAK; /* STALL */
218e99
-        } else {
218e99
-            p->actual_length = req.ucr_actlen;
218e99
-        }
218e99
-    }
218e99
-}
218e99
-
218e99
-static void usb_host_handle_data(USBDevice *dev, USBPacket *p)
218e99
-{
218e99
-    USBHostDevice *s = (USBHostDevice *)dev;
218e99
-    int ret, fd, mode;
218e99
-    int one = 1, shortpacket = 0, timeout = 50;
218e99
-    sigset_t new_mask, old_mask;
218e99
-    uint8_t devep = p->ep->nr;
218e99
-
218e99
-    /* protect data transfers from SIGALRM signal */
218e99
-    sigemptyset(&new_mask);
218e99
-    sigaddset(&new_mask, SIGALRM);
218e99
-    sigprocmask(SIG_BLOCK, &new_mask, &old_mask);
218e99
-
218e99
-    if (p->pid == USB_TOKEN_IN) {
218e99
-        devep |= 0x80;
218e99
-        mode = O_RDONLY;
218e99
-        shortpacket = 1;
218e99
-    } else {
218e99
-        mode = O_WRONLY;
218e99
-    }
218e99
-
218e99
-    fd = ensure_ep_open(s, devep, mode);
218e99
-    if (fd < 0) {
218e99
-        sigprocmask(SIG_SETMASK, &old_mask, NULL);
218e99
-        p->status = USB_RET_NODEV;
218e99
-        return;
218e99
-    }
218e99
-
218e99
-    if (ioctl(fd, USB_SET_TIMEOUT, &timeout) < 0) {
218e99
-#ifdef DEBUG
218e99
-        printf("handle_data: failed to set timeout - %s\n",
218e99
-               strerror(errno));
218e99
-#endif
218e99
-    }
218e99
-
218e99
-    if (shortpacket) {
218e99
-        if (ioctl(fd, USB_SET_SHORT_XFER, &one) < 0) {
218e99
-#ifdef DEBUG
218e99
-            printf("handle_data: failed to set short xfer mode - %s\n",
218e99
-                   strerror(errno));
218e99
-#endif
218e99
-            sigprocmask(SIG_SETMASK, &old_mask, NULL);
218e99
-        }
218e99
-    }
218e99
-
218e99
-    if (p->pid == USB_TOKEN_IN)
218e99
-        ret = readv(fd, p->iov.iov, p->iov.niov);
218e99
-    else
218e99
-        ret = writev(fd, p->iov.iov, p->iov.niov);
218e99
-
218e99
-    sigprocmask(SIG_SETMASK, &old_mask, NULL);
218e99
-
218e99
-    if (ret < 0) {
218e99
-#ifdef DEBUG
218e99
-        printf("handle_data: error after %s data - %s\n",
218e99
-               pid == USB_TOKEN_IN ? "reading" : "writing", strerror(errno));
218e99
-#endif
218e99
-        switch(errno) {
218e99
-        case ETIMEDOUT:
218e99
-        case EINTR:
218e99
-            p->status = USB_RET_NAK;
218e99
-            break;
218e99
-        default:
218e99
-            p->status = USB_RET_STALL;
218e99
-        }
218e99
-    } else {
218e99
-        p->actual_length = ret;
218e99
-    }
218e99
-}
218e99
-
218e99
-static void usb_host_handle_destroy(USBDevice *opaque)
218e99
-{
218e99
-    USBHostDevice *s = (USBHostDevice *)opaque;
218e99
-    int i;
218e99
-
218e99
-    for (i = 0; i < USB_MAX_ENDPOINTS; i++)
218e99
-        if (s->ep_fd[i] >= 0)
218e99
-            close(s->ep_fd[i]);
218e99
-
218e99
-    if (s->devfd < 0)
218e99
-        return;
218e99
-
218e99
-    close(s->devfd);
218e99
-
218e99
-    g_free(s);
218e99
-}
218e99
-
218e99
-static int usb_host_initfn(USBDevice *dev)
218e99
-{
218e99
-    dev->flags |= (1 << USB_DEV_FLAG_IS_HOST);
218e99
-    return 0;
218e99
-}
218e99
-
218e99
-USBDevice *usb_host_device_open(USBBus *guest_bus, const char *devname)
218e99
-{
218e99
-    struct usb_device_info bus_info, dev_info;
218e99
-    USBDevice *d = NULL, *ret = NULL;
218e99
-    USBHostDevice *dev;
218e99
-    char ctlpath[PATH_MAX + 1];
218e99
-    char buspath[PATH_MAX + 1];
218e99
-    int bfd, dfd, bus, address, i;
218e99
-    int ugendebug = UGEN_DEBUG_LEVEL;
218e99
-
218e99
-    if (usb_host_find_device(&bus, &address, devname) < 0) {
218e99
-        goto fail;
218e99
-    }
218e99
-
218e99
-    snprintf(buspath, PATH_MAX, "/dev/usb%d", bus);
218e99
-
218e99
-    bfd = open(buspath, O_RDWR);
218e99
-    if (bfd < 0) {
218e99
-#ifdef DEBUG
218e99
-        printf("usb_host_device_open: failed to open usb bus - %s\n",
218e99
-               strerror(errno));
218e99
-#endif
218e99
-        goto fail;
218e99
-    }
218e99
-
218e99
-    bus_info.udi_addr = address;
218e99
-    if (ioctl(bfd, USB_DEVICEINFO, &bus_info) < 0) {
218e99
-#ifdef DEBUG
218e99
-        printf("usb_host_device_open: failed to grab bus information - %s\n",
218e99
-               strerror(errno));
218e99
-#endif
218e99
-        goto fail_bfd;
218e99
-    }
218e99
-
218e99
-#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
218e99
-    snprintf(ctlpath, PATH_MAX, "/dev/%s", bus_info.udi_devnames[0]);
218e99
-#else
218e99
-    snprintf(ctlpath, PATH_MAX, "/dev/%s.00", bus_info.udi_devnames[0]);
218e99
-#endif
218e99
-
218e99
-    dfd  = open(ctlpath, O_RDWR);
218e99
-    if (dfd < 0) {
218e99
-        dfd = open(ctlpath, O_RDONLY);
218e99
-        if (dfd < 0) {
218e99
-#ifdef DEBUG
218e99
-            printf("usb_host_device_open: failed to open usb device %s - %s\n",
218e99
-                   ctlpath, strerror(errno));
218e99
-#endif
218e99
-        }
218e99
-        goto fail_dfd;
218e99
-    }
218e99
-
218e99
-    if (ioctl(dfd, USB_GET_DEVICEINFO, &dev_info) < 0) {
218e99
-#ifdef DEBUG
218e99
-        printf("usb_host_device_open: failed to grab device info - %s\n",
218e99
-               strerror(errno));
218e99
-#endif
218e99
-        goto fail_dfd;
218e99
-    }
218e99
-
218e99
-    d = usb_create(guest_bus, "usb-host");
218e99
-    dev = DO_UPCAST(USBHostDevice, dev, d);
218e99
-
218e99
-    if (dev_info.udi_speed == 1) {
218e99
-        dev->dev.speed = USB_SPEED_LOW - 1;
218e99
-        dev->dev.speedmask = USB_SPEED_MASK_LOW;
218e99
-    } else {
218e99
-        dev->dev.speed = USB_SPEED_FULL - 1;
218e99
-        dev->dev.speedmask = USB_SPEED_MASK_FULL;
218e99
-    }
218e99
-
218e99
-    if (strncmp(dev_info.udi_product, "product", 7) != 0) {
218e99
-        pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc),
218e99
-                dev_info.udi_product);
218e99
-    } else {
218e99
-        snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc),
218e99
-                 "host:%s", devname);
218e99
-    }
218e99
-
218e99
-    pstrcpy(dev->devpath, sizeof(dev->devpath), "/dev/");
218e99
-    pstrcat(dev->devpath, sizeof(dev->devpath), dev_info.udi_devnames[0]);
218e99
-
218e99
-    /* Mark the endpoints as not yet open */
218e99
-    for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
218e99
-        dev->ep_fd[i] = -1;
218e99
-    }
218e99
-
218e99
-    ioctl(dfd, USB_SETDEBUG, &ugendebug);
218e99
-
218e99
-    ret = (USBDevice *)dev;
218e99
-
218e99
-fail_dfd:
218e99
-    close(dfd);
218e99
-fail_bfd:
218e99
-    close(bfd);
218e99
-fail:
218e99
-    return ret;
218e99
-}
218e99
-
218e99
-static void usb_host_class_initfn(ObjectClass *klass, void *data)
218e99
-{
218e99
-    USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
218e99
-
218e99
-    uc->product_desc   = "USB Host Device";
218e99
-    uc->init           = usb_host_initfn;
218e99
-    uc->handle_reset   = usb_host_handle_reset;
218e99
-    uc->handle_control = usb_host_handle_control;
218e99
-    uc->handle_data    = usb_host_handle_data;
218e99
-    uc->handle_destroy = usb_host_handle_destroy;
218e99
-}
218e99
-
218e99
-static const TypeInfo usb_host_dev_info = {
218e99
-    .name          = "usb-host",
218e99
-    .parent        = TYPE_USB_DEVICE,
218e99
-    .instance_size = sizeof(USBHostDevice),
218e99
-    .class_init    = usb_host_class_initfn,
218e99
-};
218e99
-
218e99
-static void usb_host_register_types(void)
218e99
-{
218e99
-    type_register_static(&usb_host_dev_info);
218e99
-}
218e99
-
218e99
-type_init(usb_host_register_types)
218e99
-
218e99
-static int usb_host_scan(void *opaque, USBScanFunc *func)
218e99
-{
218e99
-    struct usb_device_info bus_info;
218e99
-    struct usb_device_info dev_info;
218e99
-    uint16_t vendor_id, product_id, class_id, speed;
218e99
-    int bfd, dfd, bus, address;
218e99
-    char busbuf[20], devbuf[20], product_name[256];
218e99
-    int ret = 0;
218e99
-
218e99
-    for (bus = 0; bus < 10; bus++) {
218e99
-
218e99
-        snprintf(busbuf, sizeof(busbuf) - 1, "/dev/usb%d", bus);
218e99
-        bfd = open(busbuf, O_RDWR);
218e99
-        if (bfd < 0)
218e99
-	    continue;
218e99
-
218e99
-        for (address = 1; address < 127; address++) {
218e99
-
218e99
-            bus_info.udi_addr = address;
218e99
-            if (ioctl(bfd, USB_DEVICEINFO, &bus_info) < 0)
218e99
-                continue;
218e99
-
218e99
-            /* only list devices that can be used by generic layer */
218e99
-            if (strncmp(bus_info.udi_devnames[0], "ugen", 4) != 0)
218e99
-                continue;
218e99
-
218e99
-#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
218e99
-            snprintf(devbuf, sizeof(devbuf) - 1, "/dev/%s", bus_info.udi_devnames[0]);
218e99
-#else
218e99
-            snprintf(devbuf, sizeof(devbuf) - 1, "/dev/%s.00", bus_info.udi_devnames[0]);
218e99
-#endif
218e99
-
218e99
-            dfd = open(devbuf, O_RDONLY);
218e99
-            if (dfd < 0) {
218e99
-#ifdef DEBUG
218e99
-                printf("usb_host_scan: couldn't open device %s - %s\n", devbuf,
218e99
-                       strerror(errno));
218e99
-#endif
218e99
-                continue;
218e99
-            }
218e99
-
218e99
-            if (ioctl(dfd, USB_GET_DEVICEINFO, &dev_info) < 0)
218e99
-                printf("usb_host_scan: couldn't get device information for %s - %s\n",
218e99
-                       devbuf, strerror(errno));
218e99
-
218e99
-            /* XXX: might need to fixup endianness of word values before copying over */
218e99
-
218e99
-            vendor_id = dev_info.udi_vendorNo;
218e99
-            product_id = dev_info.udi_productNo;
218e99
-            class_id = dev_info.udi_class;
218e99
-            speed = dev_info.udi_speed;
218e99
-
218e99
-            if (strncmp(dev_info.udi_product, "product", 7) != 0)
218e99
-                pstrcpy(product_name, sizeof(product_name),
218e99
-                        dev_info.udi_product);
218e99
-            else
218e99
-                product_name[0] = '\0';
218e99
-
218e99
-            ret = func(opaque, bus, address, class_id, vendor_id,
218e99
-                       product_id, product_name, speed);
218e99
-
218e99
-            close(dfd);
218e99
-
218e99
-            if (ret)
218e99
-                goto the_end;
218e99
-        }
218e99
-
218e99
-        close(bfd);
218e99
-    }
218e99
-
218e99
-the_end:
218e99
-    return ret;
218e99
-}
218e99
-
218e99
-typedef struct FindDeviceState {
218e99
-    int vendor_id;
218e99
-    int product_id;
218e99
-    int bus_num;
218e99
-    int addr;
218e99
-} FindDeviceState;
218e99
-
218e99
-static int usb_host_find_device_scan(void *opaque, int bus_num, int addr,
218e99
-                                     int class_id,
218e99
-                                     int vendor_id, int product_id,
218e99
-                                     const char *product_name, int speed)
218e99
-{
218e99
-    FindDeviceState *s = opaque;
218e99
-    if (vendor_id == s->vendor_id &&
218e99
-        product_id == s->product_id) {
218e99
-        s->bus_num = bus_num;
218e99
-        s->addr = addr;
218e99
-        return 1;
218e99
-     } else {
218e99
-        return 0;
218e99
-     }
218e99
-}
218e99
-
218e99
-
218e99
-/* the syntax is :
218e99
-   'bus.addr' (decimal numbers) or
218e99
-   'vendor_id:product_id' (hexa numbers) */
218e99
-static int usb_host_find_device(int *pbus_num, int *paddr,
218e99
-                                const char *devname)
218e99
-{
218e99
-    const char *p;
218e99
-    int ret;
218e99
-    FindDeviceState fs;
218e99
-
218e99
-    p = strchr(devname, '.');
218e99
-    if (p) {
218e99
-        *pbus_num = strtoul(devname, NULL, 0);
218e99
-        *paddr = strtoul(p + 1, NULL, 0);
218e99
-        return 0;
218e99
-    }
218e99
-    p = strchr(devname, ':');
218e99
-    if (p) {
218e99
-        fs.vendor_id = strtoul(devname, NULL, 16);
218e99
-        fs.product_id = strtoul(p + 1, NULL, 16);
218e99
-        ret = usb_host_scan(&fs, usb_host_find_device_scan);
218e99
-        if (ret) {
218e99
-            *pbus_num = fs.bus_num;
218e99
-            *paddr = fs.addr;
218e99
-            return 0;
218e99
-        }
218e99
-     }
218e99
-     return -1;
218e99
-}
218e99
-
218e99
-/**********************/
218e99
-/* USB host device info */
218e99
-
218e99
-struct usb_class_info {
218e99
-    int class;
218e99
-    const char *class_name;
218e99
-};
218e99
-
218e99
-static const struct usb_class_info usb_class_info[] = {
218e99
-    { USB_CLASS_AUDIO, "Audio"},
218e99
-    { USB_CLASS_COMM, "Communication"},
218e99
-    { USB_CLASS_HID, "HID"},
218e99
-    { USB_CLASS_HUB, "Hub" },
218e99
-    { USB_CLASS_PHYSICAL, "Physical" },
218e99
-    { USB_CLASS_PRINTER, "Printer" },
218e99
-    { USB_CLASS_MASS_STORAGE, "Storage" },
218e99
-    { USB_CLASS_CDC_DATA, "Data" },
218e99
-    { USB_CLASS_APP_SPEC, "Application Specific" },
218e99
-    { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
218e99
-    { USB_CLASS_STILL_IMAGE, "Still Image" },
218e99
-    { USB_CLASS_CSCID, "Smart Card" },
218e99
-    { USB_CLASS_CONTENT_SEC, "Content Security" },
218e99
-    { -1, NULL }
218e99
-};
218e99
-
218e99
-static const char *usb_class_str(uint8_t class)
218e99
-{
218e99
-    const struct usb_class_info *p;
218e99
-    for (p = usb_class_info; p->class != -1; p++) {
218e99
-        if (p->class == class)
218e99
-            break;
218e99
-    }
218e99
-    return p->class_name;
218e99
-}
218e99
-
218e99
-static void usb_info_device(Monitor *mon, int bus_num, int addr, int class_id,
218e99
-                            int vendor_id, int product_id,
218e99
-                            const char *product_name,
218e99
-                            int speed)
218e99
-{
218e99
-    const char *class_str, *speed_str;
218e99
-
218e99
-    switch(speed) {
218e99
-    case USB_SPEED_LOW:
218e99
-        speed_str = "1.5";
218e99
-        break;
218e99
-    case USB_SPEED_FULL:
218e99
-        speed_str = "12";
218e99
-        break;
218e99
-    case USB_SPEED_HIGH:
218e99
-        speed_str = "480";
218e99
-        break;
218e99
-    default:
218e99
-        speed_str = "?";
218e99
-        break;
218e99
-    }
218e99
-
218e99
-    monitor_printf(mon, "  Device %d.%d, speed %s Mb/s\n",
218e99
-                   bus_num, addr, speed_str);
218e99
-    class_str = usb_class_str(class_id);
218e99
-    if (class_str)
218e99
-        monitor_printf(mon, "    %s:", class_str);
218e99
-    else
218e99
-        monitor_printf(mon, "    Class %02x:", class_id);
218e99
-    monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id);
218e99
-    if (product_name[0] != '\0')
218e99
-        monitor_printf(mon, ", %s", product_name);
218e99
-    monitor_printf(mon, "\n");
218e99
-}
218e99
-
218e99
-static int usb_host_info_device(void *opaque,
218e99
-                                int bus_num, int addr,
218e99
-                                int class_id,
218e99
-                                int vendor_id, int product_id,
218e99
-                                const char *product_name,
218e99
-                                int speed)
218e99
-{
218e99
-    Monitor *mon = opaque;
218e99
-
218e99
-    usb_info_device(mon, bus_num, addr, class_id, vendor_id, product_id,
218e99
-                    product_name, speed);
218e99
-    return 0;
218e99
-}
218e99
-
218e99
-void usb_host_info(Monitor *mon, const QDict *qdict)
218e99
-{
218e99
-    usb_host_scan(mon, usb_host_info_device);
218e99
-}
218e99
diff --git a/hw/usb/host-linux.c b/hw/usb/host-linux.c
218e99
deleted file mode 100644
218e99
index ca09a89..0000000
218e99
--- a/hw/usb/host-linux.c
218e99
+++ /dev/null
218e99
@@ -1,1910 +0,0 @@
218e99
-/*
218e99
- * Linux host USB redirector
218e99
- *
218e99
- * Copyright (c) 2005 Fabrice Bellard
218e99
- *
218e99
- * Copyright (c) 2008 Max Krasnyansky
218e99
- *      Support for host device auto connect & disconnect
218e99
- *      Major rewrite to support fully async operation
218e99
- *
218e99
- * Copyright 2008 TJ <linux@tjworld.net>
218e99
- *      Added flexible support for /dev/bus/usb /sys/bus/usb/devices in addition
218e99
- *      to the legacy /proc/bus/usb USB device discovery and handling
218e99
- *
218e99
- * Permission is hereby granted, free of charge, to any person obtaining a copy
218e99
- * of this software and associated documentation files (the "Software"), to deal
218e99
- * in the Software without restriction, including without limitation the rights
218e99
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
218e99
- * copies of the Software, and to permit persons to whom the Software is
218e99
- * furnished to do so, subject to the following conditions:
218e99
- *
218e99
- * The above copyright notice and this permission notice shall be included in
218e99
- * all copies or substantial portions of the Software.
218e99
- *
218e99
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
218e99
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
218e99
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
218e99
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
218e99
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
218e99
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
218e99
- * THE SOFTWARE.
218e99
- */
218e99
-
218e99
-#include "qemu-common.h"
218e99
-#include "qemu/timer.h"
218e99
-#include "monitor/monitor.h"
218e99
-#include "sysemu/sysemu.h"
218e99
-#include "trace.h"
218e99
-
218e99
-#include <dirent.h>
218e99
-#include <sys/ioctl.h>
218e99
-
218e99
-#include <linux/usbdevice_fs.h>
218e99
-#include <linux/version.h>
218e99
-#include "hw/usb.h"
218e99
-#include "hw/usb/desc.h"
218e99
-#include "hw/usb/host.h"
218e99
-
218e99
-#ifdef CONFIG_USB_LIBUSB
218e99
-# define DEVNAME "usb-host-linux"
218e99
-#else
218e99
-# define DEVNAME "usb-host"
218e99
-#endif
218e99
-
218e99
-/* We redefine it to avoid version problems */
218e99
-struct usb_ctrltransfer {
218e99
-    uint8_t  bRequestType;
218e99
-    uint8_t  bRequest;
218e99
-    uint16_t wValue;
218e99
-    uint16_t wIndex;
218e99
-    uint16_t wLength;
218e99
-    uint32_t timeout;
218e99
-    void *data;
218e99
-};
218e99
-
218e99
-typedef int USBScanFunc(void *opaque, int bus_num, int addr, const char *port,
218e99
-                        int class_id, int vendor_id, int product_id,
218e99
-                        const char *product_name, int speed);
218e99
-
218e99
-//#define DEBUG
218e99
-
218e99
-#ifdef DEBUG
218e99
-#define DPRINTF printf
218e99
-#else
218e99
-#define DPRINTF(...)
218e99
-#endif
218e99
-
218e99
-#define PRODUCT_NAME_SZ 32
218e99
-#define MAX_PORTLEN 16
218e99
-
218e99
-/* endpoint association data */
218e99
-#define ISO_FRAME_DESC_PER_URB 32
218e99
-
218e99
-/* devio.c limits single requests to 16k */
218e99
-#define MAX_USBFS_BUFFER_SIZE 16384
218e99
-
218e99
-typedef struct AsyncURB AsyncURB;
218e99
-
218e99
-struct endp_data {
218e99
-    uint8_t halted;
218e99
-    uint8_t iso_started;
218e99
-    AsyncURB *iso_urb;
218e99
-    int iso_urb_idx;
218e99
-    int iso_buffer_used;
218e99
-    int inflight;
218e99
-};
218e99
-
218e99
-enum USBHostDeviceOptions {
218e99
-    USB_HOST_OPT_PIPELINE,
218e99
-};
218e99
-
218e99
-typedef struct USBHostDevice {
218e99
-    USBDevice dev;
218e99
-    int       fd;
218e99
-    int       hub_fd;
218e99
-    int       hub_port;
218e99
-
218e99
-    uint8_t   descr[8192];
218e99
-    int       descr_len;
218e99
-    int       closing;
218e99
-    uint32_t  iso_urb_count;
218e99
-    uint32_t  options;
218e99
-    Notifier  exit;
218e99
-    QEMUBH    *bh;
218e99
-
218e99
-    struct endp_data ep_in[USB_MAX_ENDPOINTS];
218e99
-    struct endp_data ep_out[USB_MAX_ENDPOINTS];
218e99
-    QLIST_HEAD(, AsyncURB) aurbs;
218e99
-
218e99
-    /* Host side address */
218e99
-    int bus_num;
218e99
-    int addr;
218e99
-    char port[MAX_PORTLEN];
218e99
-    struct USBAutoFilter match;
218e99
-    int32_t bootindex;
218e99
-    int seen, errcount;
218e99
-
218e99
-    QTAILQ_ENTRY(USBHostDevice) next;
218e99
-} USBHostDevice;
218e99
-
218e99
-static QTAILQ_HEAD(, USBHostDevice) hostdevs = QTAILQ_HEAD_INITIALIZER(hostdevs);
218e99
-
218e99
-static int usb_host_close(USBHostDevice *dev);
218e99
-static void usb_host_auto_check(void *unused);
218e99
-static int usb_host_read_file(char *line, size_t line_size,
218e99
-                            const char *device_file, const char *device_name);
218e99
-static void usb_linux_update_endp_table(USBHostDevice *s);
218e99
-
218e99
-static int usb_host_usbfs_type(USBHostDevice *s, USBPacket *p)
218e99
-{
218e99
-    static const int usbfs[] = {
218e99
-        [USB_ENDPOINT_XFER_CONTROL] = USBDEVFS_URB_TYPE_CONTROL,
218e99
-        [USB_ENDPOINT_XFER_ISOC]    = USBDEVFS_URB_TYPE_ISO,
218e99
-        [USB_ENDPOINT_XFER_BULK]    = USBDEVFS_URB_TYPE_BULK,
218e99
-        [USB_ENDPOINT_XFER_INT]     = USBDEVFS_URB_TYPE_INTERRUPT,
218e99
-    };
218e99
-    uint8_t type = p->ep->type;
218e99
-    assert(type < ARRAY_SIZE(usbfs));
218e99
-    return usbfs[type];
218e99
-}
218e99
-
218e99
-static int usb_host_do_reset(USBHostDevice *dev)
218e99
-{
218e99
-    struct timeval s, e;
218e99
-    uint32_t usecs;
218e99
-    int ret;
218e99
-
218e99
-    gettimeofday(&s, NULL);
218e99
-    ret = ioctl(dev->fd, USBDEVFS_RESET);
218e99
-    gettimeofday(&e, NULL);
218e99
-    usecs = (e.tv_sec  - s.tv_sec) * 1000000;
218e99
-    usecs += e.tv_usec - s.tv_usec;
218e99
-    if (usecs > 1000000) {
218e99
-        /* more than a second, something is fishy, broken usb device? */
218e99
-        fprintf(stderr, "husb: device %d:%d reset took %d.%06d seconds\n",
218e99
-                dev->bus_num, dev->addr, usecs / 1000000, usecs % 1000000);
218e99
-    }
218e99
-    return ret;
218e99
-}
218e99
-
218e99
-static struct endp_data *get_endp(USBHostDevice *s, int pid, int ep)
218e99
-{
218e99
-    struct endp_data *eps = pid == USB_TOKEN_IN ? s->ep_in : s->ep_out;
218e99
-    assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT);
218e99
-    assert(ep > 0 && ep <= USB_MAX_ENDPOINTS);
218e99
-    return eps + ep - 1;
218e99
-}
218e99
-
218e99
-static int is_isoc(USBHostDevice *s, int pid, int ep)
218e99
-{
218e99
-    return usb_ep_get_type(&s->dev, pid, ep) == USB_ENDPOINT_XFER_ISOC;
218e99
-}
218e99
-
218e99
-static int is_valid(USBHostDevice *s, int pid, int ep)
218e99
-{
218e99
-    return usb_ep_get_type(&s->dev, pid, ep) != USB_ENDPOINT_XFER_INVALID;
218e99
-}
218e99
-
218e99
-static int is_halted(USBHostDevice *s, int pid, int ep)
218e99
-{
218e99
-    return get_endp(s, pid, ep)->halted;
218e99
-}
218e99
-
218e99
-static void clear_halt(USBHostDevice *s, int pid, int ep)
218e99
-{
218e99
-    trace_usb_host_ep_clear_halt(s->bus_num, s->addr, ep);
218e99
-    get_endp(s, pid, ep)->halted = 0;
218e99
-}
218e99
-
218e99
-static void set_halt(USBHostDevice *s, int pid, int ep)
218e99
-{
218e99
-    if (ep != 0) {
218e99
-        trace_usb_host_ep_set_halt(s->bus_num, s->addr, ep);
218e99
-        get_endp(s, pid, ep)->halted = 1;
218e99
-    }
218e99
-}
218e99
-
218e99
-static int is_iso_started(USBHostDevice *s, int pid, int ep)
218e99
-{
218e99
-    return get_endp(s, pid, ep)->iso_started;
218e99
-}
218e99
-
218e99
-static void clear_iso_started(USBHostDevice *s, int pid, int ep)
218e99
-{
218e99
-    trace_usb_host_iso_stop(s->bus_num, s->addr, ep);
218e99
-    get_endp(s, pid, ep)->iso_started = 0;
218e99
-}
218e99
-
218e99
-static void set_iso_started(USBHostDevice *s, int pid, int ep)
218e99
-{
218e99
-    struct endp_data *e = get_endp(s, pid, ep);
218e99
-
218e99
-    trace_usb_host_iso_start(s->bus_num, s->addr, ep);
218e99
-    if (!e->iso_started) {
218e99
-        e->iso_started = 1;
218e99
-        e->inflight = 0;
218e99
-    }
218e99
-}
218e99
-
218e99
-static int change_iso_inflight(USBHostDevice *s, int pid, int ep, int value)
218e99
-{
218e99
-    struct endp_data *e = get_endp(s, pid, ep);
218e99
-
218e99
-    e->inflight += value;
218e99
-    return e->inflight;
218e99
-}
218e99
-
218e99
-static void set_iso_urb(USBHostDevice *s, int pid, int ep, AsyncURB *iso_urb)
218e99
-{
218e99
-    get_endp(s, pid, ep)->iso_urb = iso_urb;
218e99
-}
218e99
-
218e99
-static AsyncURB *get_iso_urb(USBHostDevice *s, int pid, int ep)
218e99
-{
218e99
-    return get_endp(s, pid, ep)->iso_urb;
218e99
-}
218e99
-
218e99
-static void set_iso_urb_idx(USBHostDevice *s, int pid, int ep, int i)
218e99
-{
218e99
-    get_endp(s, pid, ep)->iso_urb_idx = i;
218e99
-}
218e99
-
218e99
-static int get_iso_urb_idx(USBHostDevice *s, int pid, int ep)
218e99
-{
218e99
-    return get_endp(s, pid, ep)->iso_urb_idx;
218e99
-}
218e99
-
218e99
-static void set_iso_buffer_used(USBHostDevice *s, int pid, int ep, int i)
218e99
-{
218e99
-    get_endp(s, pid, ep)->iso_buffer_used = i;
218e99
-}
218e99
-
218e99
-static int get_iso_buffer_used(USBHostDevice *s, int pid, int ep)
218e99
-{
218e99
-    return get_endp(s, pid, ep)->iso_buffer_used;
218e99
-}
218e99
-
218e99
-/*
218e99
- * Async URB state.
218e99
- * We always allocate iso packet descriptors even for bulk transfers
218e99
- * to simplify allocation and casts.
218e99
- */
218e99
-struct AsyncURB
218e99
-{
218e99
-    struct usbdevfs_urb urb;
218e99
-    struct usbdevfs_iso_packet_desc isocpd[ISO_FRAME_DESC_PER_URB];
218e99
-    USBHostDevice *hdev;
218e99
-    QLIST_ENTRY(AsyncURB) next;
218e99
-
218e99
-    /* For regular async urbs */
218e99
-    USBPacket     *packet;
218e99
-    int more; /* large transfer, more urbs follow */
218e99
-
218e99
-    /* For buffered iso handling */
218e99
-    int iso_frame_idx; /* -1 means in flight */
218e99
-};
218e99
-
218e99
-static AsyncURB *async_alloc(USBHostDevice *s)
218e99
-{
218e99
-    AsyncURB *aurb = g_malloc0(sizeof(AsyncURB));
218e99
-    aurb->hdev = s;
218e99
-    QLIST_INSERT_HEAD(&s->aurbs, aurb, next);
218e99
-    return aurb;
218e99
-}
218e99
-
218e99
-static void async_free(AsyncURB *aurb)
218e99
-{
218e99
-    QLIST_REMOVE(aurb, next);
218e99
-    g_free(aurb);
218e99
-}
218e99
-
218e99
-static void do_disconnect(USBHostDevice *s)
218e99
-{
218e99
-    usb_host_close(s);
218e99
-    usb_host_auto_check(NULL);
218e99
-}
218e99
-
218e99
-static void async_complete(void *opaque)
218e99
-{
218e99
-    USBHostDevice *s = opaque;
218e99
-    AsyncURB *aurb;
218e99
-    int urbs = 0;
218e99
-
218e99
-    while (1) {
218e99
-        USBPacket *p;
218e99
-
218e99
-        int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb);
218e99
-        if (r < 0) {
218e99
-            if (errno == EAGAIN) {
218e99
-                if (urbs > 2) {
218e99
-                    /* indicates possible latency issues */
218e99
-                    trace_usb_host_iso_many_urbs(s->bus_num, s->addr, urbs);
218e99
-                }
218e99
-                return;
218e99
-            }
218e99
-            if (errno == ENODEV) {
218e99
-                if (!s->closing) {
218e99
-                    trace_usb_host_disconnect(s->bus_num, s->addr);
218e99
-                    do_disconnect(s);
218e99
-                }
218e99
-                return;
218e99
-            }
218e99
-
218e99
-            perror("USBDEVFS_REAPURBNDELAY");
218e99
-            return;
218e99
-        }
218e99
-
218e99
-        DPRINTF("husb: async completed. aurb %p status %d alen %d\n",
218e99
-                aurb, aurb->urb.status, aurb->urb.actual_length);
218e99
-
218e99
-        /* If this is a buffered iso urb mark it as complete and don't do
218e99
-           anything else (it is handled further in usb_host_handle_iso_data) */
218e99
-        if (aurb->iso_frame_idx == -1) {
218e99
-            int inflight;
218e99
-            int pid = (aurb->urb.endpoint & USB_DIR_IN) ?
218e99
-                USB_TOKEN_IN : USB_TOKEN_OUT;
218e99
-            int ep = aurb->urb.endpoint & 0xf;
218e99
-            if (aurb->urb.status == -EPIPE) {
218e99
-                set_halt(s, pid, ep);
218e99
-            }
218e99
-            aurb->iso_frame_idx = 0;
218e99
-            urbs++;
218e99
-            inflight = change_iso_inflight(s, pid, ep, -1);
218e99
-            if (inflight == 0 && is_iso_started(s, pid, ep)) {
218e99
-                /* can be latency issues, or simply end of stream */
218e99
-                trace_usb_host_iso_out_of_bufs(s->bus_num, s->addr, ep);
218e99
-            }
218e99
-            continue;
218e99
-        }
218e99
-
218e99
-        p = aurb->packet;
218e99
-        trace_usb_host_urb_complete(s->bus_num, s->addr, aurb, aurb->urb.status,
218e99
-                                    aurb->urb.actual_length, aurb->more);
218e99
-
218e99
-        if (p) {
218e99
-            switch (aurb->urb.status) {
218e99
-            case 0:
218e99
-                p->actual_length += aurb->urb.actual_length;
218e99
-                if (!aurb->more) {
218e99
-                    /* Clear previous ASYNC status */
218e99
-                    p->status = USB_RET_SUCCESS;
218e99
-                }
218e99
-                break;
218e99
-
218e99
-            case -EPIPE:
218e99
-                set_halt(s, p->pid, p->ep->nr);
218e99
-                p->status = USB_RET_STALL;
218e99
-                break;
218e99
-
218e99
-            case -EOVERFLOW:
218e99
-                p->status = USB_RET_BABBLE;
218e99
-                break;
218e99
-
218e99
-            default:
218e99
-                p->status = USB_RET_IOERROR;
218e99
-                break;
218e99
-            }
218e99
-
218e99
-            if (aurb->urb.type == USBDEVFS_URB_TYPE_CONTROL) {
218e99
-                trace_usb_host_req_complete(s->bus_num, s->addr, p,
218e99
-                                            p->status, aurb->urb.actual_length);
218e99
-                usb_generic_async_ctrl_complete(&s->dev, p);
218e99
-            } else if (!aurb->more) {
218e99
-                trace_usb_host_req_complete(s->bus_num, s->addr, p,
218e99
-                                            p->status, aurb->urb.actual_length);
218e99
-                usb_packet_complete(&s->dev, p);
218e99
-            }
218e99
-        }
218e99
-
218e99
-        async_free(aurb);
218e99
-    }
218e99
-}
218e99
-
218e99
-static void usb_host_async_cancel(USBDevice *dev, USBPacket *p)
218e99
-{
218e99
-    USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
218e99
-    AsyncURB *aurb;
218e99
-
218e99
-    trace_usb_host_req_canceled(s->bus_num, s->addr, p);
218e99
-
218e99
-    QLIST_FOREACH(aurb, &s->aurbs, next) {
218e99
-        if (p != aurb->packet) {
218e99
-            continue;
218e99
-        }
218e99
-
218e99
-        trace_usb_host_urb_canceled(s->bus_num, s->addr, aurb);
218e99
-
218e99
-        /* Mark it as dead (see async_complete above) */
218e99
-        aurb->packet = NULL;
218e99
-
218e99
-        int r = ioctl(s->fd, USBDEVFS_DISCARDURB, aurb);
218e99
-        if (r < 0) {
218e99
-            DPRINTF("husb: async. discard urb failed errno %d\n", errno);
218e99
-        }
218e99
-    }
218e99
-}
218e99
-
218e99
-static int usb_host_open_device(int bus, int addr)
218e99
-{
218e99
-    const char *usbfs = NULL;
218e99
-    char filename[32];
218e99
-    struct stat st;
218e99
-    int fd, rc;
218e99
-
218e99
-    rc = stat("/dev/bus/usb", &st);
218e99
-    if (rc == 0 && S_ISDIR(st.st_mode)) {
218e99
-        /* udev-created device nodes available */
218e99
-        usbfs = "/dev/bus/usb";
218e99
-    } else {
218e99
-        /* fallback: usbfs mounted below /proc */
218e99
-        usbfs = "/proc/bus/usb";
218e99
-    }
218e99
-
218e99
-    snprintf(filename, sizeof(filename), "%s/%03d/%03d",
218e99
-             usbfs, bus, addr);
218e99
-    fd = open(filename, O_RDWR | O_NONBLOCK);
218e99
-    if (fd < 0) {
218e99
-        fprintf(stderr, "husb: open %s: %s\n", filename, strerror(errno));
218e99
-    }
218e99
-    return fd;
218e99
-}
218e99
-
218e99
-static int usb_host_claim_port(USBHostDevice *s)
218e99
-{
218e99
-#ifdef USBDEVFS_CLAIM_PORT
218e99
-    char *h, hub_name[64], line[1024];
218e99
-    int hub_addr, ret;
218e99
-
218e99
-    snprintf(hub_name, sizeof(hub_name), "%d-%s",
218e99
-             s->match.bus_num, s->match.port);
218e99
-
218e99
-    /* try strip off last ".$portnr" to get hub */
218e99
-    h = strrchr(hub_name, '.');
218e99
-    if (h != NULL) {
218e99
-        s->hub_port = atoi(h+1);
218e99
-        *h = '\0';
218e99
-    } else {
218e99
-        /* no dot in there -> it is the root hub */
218e99
-        snprintf(hub_name, sizeof(hub_name), "usb%d",
218e99
-                 s->match.bus_num);
218e99
-        s->hub_port = atoi(s->match.port);
218e99
-    }
218e99
-
218e99
-    if (!usb_host_read_file(line, sizeof(line), "devnum",
218e99
-                            hub_name)) {
218e99
-        return -1;
218e99
-    }
218e99
-    if (sscanf(line, "%d", &hub_addr) != 1) {
218e99
-        return -1;
218e99
-    }
218e99
-
218e99
-    s->hub_fd = usb_host_open_device(s->match.bus_num, hub_addr);
218e99
-    if (s->hub_fd < 0) {
218e99
-        return -1;
218e99
-    }
218e99
-
218e99
-    ret = ioctl(s->hub_fd, USBDEVFS_CLAIM_PORT, &s->hub_port);
218e99
-    if (ret < 0) {
218e99
-        close(s->hub_fd);
218e99
-        s->hub_fd = -1;
218e99
-        return -1;
218e99
-    }
218e99
-
218e99
-    trace_usb_host_claim_port(s->match.bus_num, hub_addr, s->hub_port);
218e99
-    return 0;
218e99
-#else
218e99
-    return -1;
218e99
-#endif
218e99
-}
218e99
-
218e99
-static void usb_host_release_port(USBHostDevice *s)
218e99
-{
218e99
-    if (s->hub_fd == -1) {
218e99
-        return;
218e99
-    }
218e99
-#ifdef USBDEVFS_RELEASE_PORT
218e99
-    ioctl(s->hub_fd, USBDEVFS_RELEASE_PORT, &s->hub_port);
218e99
-#endif
218e99
-    close(s->hub_fd);
218e99
-    s->hub_fd = -1;
218e99
-}
218e99
-
218e99
-static int usb_host_disconnect_ifaces(USBHostDevice *dev, int nb_interfaces)
218e99
-{
218e99
-    /* earlier Linux 2.4 do not support that */
218e99
-#ifdef USBDEVFS_DISCONNECT
218e99
-    struct usbdevfs_ioctl ctrl;
218e99
-    int ret, interface;
218e99
-
218e99
-    for (interface = 0; interface < nb_interfaces; interface++) {
218e99
-        ctrl.ioctl_code = USBDEVFS_DISCONNECT;
218e99
-        ctrl.ifno = interface;
218e99
-        ctrl.data = 0;
218e99
-        ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl);
218e99
-        if (ret < 0 && errno != ENODATA) {
218e99
-            perror("USBDEVFS_DISCONNECT");
218e99
-            return -1;
218e99
-        }
218e99
-    }
218e99
-#endif
218e99
-    return 0;
218e99
-}
218e99
-
218e99
-static int usb_linux_get_num_interfaces(USBHostDevice *s)
218e99
-{
218e99
-    char device_name[64], line[1024];
218e99
-    int num_interfaces = 0;
218e99
-
218e99
-    sprintf(device_name, "%d-%s", s->bus_num, s->port);
218e99
-    if (!usb_host_read_file(line, sizeof(line), "bNumInterfaces",
218e99
-                            device_name)) {
218e99
-        return -1;
218e99
-    }
218e99
-    if (sscanf(line, "%d", &num_interfaces) != 1) {
218e99
-        return -1;
218e99
-    }
218e99
-    return num_interfaces;
218e99
-}
218e99
-
218e99
-static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration)
218e99
-{
218e99
-    const char *op = NULL;
218e99
-    int dev_descr_len, config_descr_len;
218e99
-    int interface, nb_interfaces;
218e99
-    int ret, i;
218e99
-
218e99
-    for (i = 0; i < USB_MAX_INTERFACES; i++) {
218e99
-        dev->dev.altsetting[i] = 0;
218e99
-    }
218e99
-
218e99
-    if (configuration == 0) { /* address state - ignore */
218e99
-        dev->dev.ninterfaces   = 0;
218e99
-        dev->dev.configuration = 0;
218e99
-        return 1;
218e99
-    }
218e99
-
218e99
-    DPRINTF("husb: claiming interfaces. config %d\n", configuration);
218e99
-
218e99
-    i = 0;
218e99
-    dev_descr_len = dev->descr[0];
218e99
-    if (dev_descr_len > dev->descr_len) {
218e99
-        fprintf(stderr, "husb: update iface failed. descr too short\n");
218e99
-        return 0;
218e99
-    }
218e99
-
218e99
-    i += dev_descr_len;
218e99
-    while (i < dev->descr_len) {
218e99
-        DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n",
218e99
-                i, dev->descr_len,
218e99
-               dev->descr[i], dev->descr[i+1]);
218e99
-
218e99
-        if (dev->descr[i+1] != USB_DT_CONFIG) {
218e99
-            i += dev->descr[i];
218e99
-            continue;
218e99
-        }
218e99
-        config_descr_len = dev->descr[i];
218e99
-
218e99
-        DPRINTF("husb: config #%d need %d\n", dev->descr[i + 5], configuration);
218e99
-
218e99
-        if (configuration == dev->descr[i + 5]) {
218e99
-            configuration = dev->descr[i + 5];
218e99
-            break;
218e99
-        }
218e99
-
218e99
-        i += config_descr_len;
218e99
-    }
218e99
-
218e99
-    if (i >= dev->descr_len) {
218e99
-        fprintf(stderr,
218e99
-                "husb: update iface failed. no matching configuration\n");
218e99
-        return 0;
218e99
-    }
218e99
-    nb_interfaces = dev->descr[i + 4];
218e99
-
218e99
-    if (usb_host_disconnect_ifaces(dev, nb_interfaces) < 0) {
218e99
-        goto fail;
218e99
-    }
218e99
-
218e99
-    /* XXX: only grab if all interfaces are free */
218e99
-    for (interface = 0; interface < nb_interfaces; interface++) {
218e99
-        op = "USBDEVFS_CLAIMINTERFACE";
218e99
-        ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface);
218e99
-        if (ret < 0) {
218e99
-            goto fail;
218e99
-        }
218e99
-    }
218e99
-
218e99
-    trace_usb_host_claim_interfaces(dev->bus_num, dev->addr,
218e99
-                                    nb_interfaces, configuration);
218e99
-
218e99
-    dev->dev.ninterfaces   = nb_interfaces;
218e99
-    dev->dev.configuration = configuration;
218e99
-    return 1;
218e99
-
218e99
-fail:
218e99
-    if (errno == ENODEV) {
218e99
-        do_disconnect(dev);
218e99
-    }
218e99
-    perror(op);
218e99
-    return 0;
218e99
-}
218e99
-
218e99
-static int usb_host_release_interfaces(USBHostDevice *s)
218e99
-{
218e99
-    int ret, i;
218e99
-
218e99
-    trace_usb_host_release_interfaces(s->bus_num, s->addr);
218e99
-
218e99
-    for (i = 0; i < s->dev.ninterfaces; i++) {
218e99
-        ret = ioctl(s->fd, USBDEVFS_RELEASEINTERFACE, &i);
218e99
-        if (ret < 0) {
218e99
-            perror("USBDEVFS_RELEASEINTERFACE");
218e99
-            return 0;
218e99
-        }
218e99
-    }
218e99
-    return 1;
218e99
-}
218e99
-
218e99
-static void usb_host_handle_reset(USBDevice *dev)
218e99
-{
218e99
-    USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
218e99
-
218e99
-    trace_usb_host_reset(s->bus_num, s->addr);
218e99
-
218e99
-    usb_host_do_reset(s);
218e99
-
218e99
-    usb_host_claim_interfaces(s, 0);
218e99
-    usb_linux_update_endp_table(s);
218e99
-}
218e99
-
218e99
-static void usb_host_handle_destroy(USBDevice *dev)
218e99
-{
218e99
-    USBHostDevice *s = (USBHostDevice *)dev;
218e99
-
218e99
-    usb_host_release_port(s);
218e99
-    usb_host_close(s);
218e99
-    QTAILQ_REMOVE(&hostdevs, s, next);
218e99
-    qemu_remove_exit_notifier(&s->exit);
218e99
-}
218e99
-
218e99
-/* iso data is special, we need to keep enough urbs in flight to make sure
218e99
-   that the controller never runs out of them, otherwise the device will
218e99
-   likely suffer a buffer underrun / overrun. */
218e99
-static AsyncURB *usb_host_alloc_iso(USBHostDevice *s, int pid, uint8_t ep)
218e99
-{
218e99
-    AsyncURB *aurb;
218e99
-    int i, j, len = usb_ep_get_max_packet_size(&s->dev, pid, ep);
218e99
-
218e99
-    aurb = g_malloc0(s->iso_urb_count * sizeof(*aurb));
218e99
-    for (i = 0; i < s->iso_urb_count; i++) {
218e99
-        aurb[i].urb.endpoint      = ep;
218e99
-        aurb[i].urb.buffer_length = ISO_FRAME_DESC_PER_URB * len;
218e99
-        aurb[i].urb.buffer        = g_malloc(aurb[i].urb.buffer_length);
218e99
-        aurb[i].urb.type          = USBDEVFS_URB_TYPE_ISO;
218e99
-        aurb[i].urb.flags         = USBDEVFS_URB_ISO_ASAP;
218e99
-        aurb[i].urb.number_of_packets = ISO_FRAME_DESC_PER_URB;
218e99
-        for (j = 0 ; j < ISO_FRAME_DESC_PER_URB; j++)
218e99
-            aurb[i].urb.iso_frame_desc[j].length = len;
218e99
-        if (pid == USB_TOKEN_IN) {
218e99
-            aurb[i].urb.endpoint |= 0x80;
218e99
-            /* Mark as fully consumed (idle) */
218e99
-            aurb[i].iso_frame_idx = ISO_FRAME_DESC_PER_URB;
218e99
-        }
218e99
-    }
218e99
-    set_iso_urb(s, pid, ep, aurb);
218e99
-
218e99
-    return aurb;
218e99
-}
218e99
-
218e99
-static void usb_host_stop_n_free_iso(USBHostDevice *s, int pid, uint8_t ep)
218e99
-{
218e99
-    AsyncURB *aurb;
218e99
-    int i, ret, killed = 0, free = 1;
218e99
-
218e99
-    aurb = get_iso_urb(s, pid, ep);
218e99
-    if (!aurb) {
218e99
-        return;
218e99
-    }
218e99
-
218e99
-    for (i = 0; i < s->iso_urb_count; i++) {
218e99
-        /* in flight? */
218e99
-        if (aurb[i].iso_frame_idx == -1) {
218e99
-            ret = ioctl(s->fd, USBDEVFS_DISCARDURB, &aurb[i]);
218e99
-            if (ret < 0) {
218e99
-                perror("USBDEVFS_DISCARDURB");
218e99
-                free = 0;
218e99
-                continue;
218e99
-            }
218e99
-            killed++;
218e99
-        }
218e99
-    }
218e99
-
218e99
-    /* Make sure any urbs we've killed are reaped before we free them */
218e99
-    if (killed) {
218e99
-        async_complete(s);
218e99
-    }
218e99
-
218e99
-    for (i = 0; i < s->iso_urb_count; i++) {
218e99
-        g_free(aurb[i].urb.buffer);
218e99
-    }
218e99
-
218e99
-    if (free)
218e99
-        g_free(aurb);
218e99
-    else
218e99
-        printf("husb: leaking iso urbs because of discard failure\n");
218e99
-    set_iso_urb(s, pid, ep, NULL);
218e99
-    set_iso_urb_idx(s, pid, ep, 0);
218e99
-    clear_iso_started(s, pid, ep);
218e99
-}
218e99
-
218e99
-static void urb_status_to_usb_ret(int status, USBPacket *p)
218e99
-{
218e99
-    switch (status) {
218e99
-    case -EPIPE:
218e99
-        p->status = USB_RET_STALL;
218e99
-        break;
218e99
-    case -EOVERFLOW:
218e99
-        p->status = USB_RET_BABBLE;
218e99
-        break;
218e99
-    default:
218e99
-        p->status = USB_RET_IOERROR;
218e99
-    }
218e99
-}
218e99
-
218e99
-static void usb_host_handle_iso_data(USBHostDevice *s, USBPacket *p, int in)
218e99
-{
218e99
-    AsyncURB *aurb;
218e99
-    int i, j, max_packet_size, offset, len;
218e99
-    uint8_t *buf;
218e99
-
218e99
-    max_packet_size = p->ep->max_packet_size;
218e99
-    if (max_packet_size == 0) {
218e99
-        p->status = USB_RET_NAK;
218e99
-        return;
218e99
-    }
218e99
-
218e99
-    aurb = get_iso_urb(s, p->pid, p->ep->nr);
218e99
-    if (!aurb) {
218e99
-        aurb = usb_host_alloc_iso(s, p->pid, p->ep->nr);
218e99
-    }
218e99
-
218e99
-    i = get_iso_urb_idx(s, p->pid, p->ep->nr);
218e99
-    j = aurb[i].iso_frame_idx;
218e99
-    if (j >= 0 && j < ISO_FRAME_DESC_PER_URB) {
218e99
-        if (in) {
218e99
-            /* Check urb status  */
218e99
-            if (aurb[i].urb.status) {
218e99
-                urb_status_to_usb_ret(aurb[i].urb.status, p);
218e99
-                /* Move to the next urb */
218e99
-                aurb[i].iso_frame_idx = ISO_FRAME_DESC_PER_URB - 1;
218e99
-            /* Check frame status */
218e99
-            } else if (aurb[i].urb.iso_frame_desc[j].status) {
218e99
-                urb_status_to_usb_ret(aurb[i].urb.iso_frame_desc[j].status, p);
218e99
-            /* Check the frame fits */
218e99
-            } else if (aurb[i].urb.iso_frame_desc[j].actual_length
218e99
-                       > p->iov.size) {
218e99
-                printf("husb: received iso data is larger then packet\n");
218e99
-                p->status = USB_RET_BABBLE;
218e99
-            /* All good copy data over */
218e99
-            } else {
218e99
-                len = aurb[i].urb.iso_frame_desc[j].actual_length;
218e99
-                buf  = aurb[i].urb.buffer +
218e99
-                    j * aurb[i].urb.iso_frame_desc[0].length;
218e99
-                usb_packet_copy(p, buf, len);
218e99
-            }
218e99
-        } else {
218e99
-            len = p->iov.size;
218e99
-            offset = (j == 0) ? 0 : get_iso_buffer_used(s, p->pid, p->ep->nr);
218e99
-
218e99
-            /* Check the frame fits */
218e99
-            if (len > max_packet_size) {
218e99
-                printf("husb: send iso data is larger then max packet size\n");
218e99
-                p->status = USB_RET_NAK;
218e99
-                return;
218e99
-            }
218e99
-
218e99
-            /* All good copy data over */
218e99
-            usb_packet_copy(p, aurb[i].urb.buffer + offset, len);
218e99
-            aurb[i].urb.iso_frame_desc[j].length = len;
218e99
-            offset += len;
218e99
-            set_iso_buffer_used(s, p->pid, p->ep->nr, offset);
218e99
-
218e99
-            /* Start the stream once we have buffered enough data */
218e99
-            if (!is_iso_started(s, p->pid, p->ep->nr) && i == 1 && j == 8) {
218e99
-                set_iso_started(s, p->pid, p->ep->nr);
218e99
-            }
218e99
-        }
218e99
-        aurb[i].iso_frame_idx++;
218e99
-        if (aurb[i].iso_frame_idx == ISO_FRAME_DESC_PER_URB) {
218e99
-            i = (i + 1) % s->iso_urb_count;
218e99
-            set_iso_urb_idx(s, p->pid, p->ep->nr, i);
218e99
-        }
218e99
-    } else {
218e99
-        if (in) {
218e99
-            set_iso_started(s, p->pid, p->ep->nr);
218e99
-        } else {
218e99
-            DPRINTF("hubs: iso out error no free buffer, dropping packet\n");
218e99
-        }
218e99
-    }
218e99
-
218e99
-    if (is_iso_started(s, p->pid, p->ep->nr)) {
218e99
-        /* (Re)-submit all fully consumed / filled urbs */
218e99
-        for (i = 0; i < s->iso_urb_count; i++) {
218e99
-            if (aurb[i].iso_frame_idx == ISO_FRAME_DESC_PER_URB) {
218e99
-                if (ioctl(s->fd, USBDEVFS_SUBMITURB, &aurb[i]) < 0) {
218e99
-                    perror("USBDEVFS_SUBMITURB");
218e99
-                    if (!in || p->status == USB_RET_SUCCESS) {
218e99
-                        switch(errno) {
218e99
-                        case ETIMEDOUT:
218e99
-                            p->status = USB_RET_NAK;
218e99
-                            break;
218e99
-                        case EPIPE:
218e99
-                        default:
218e99
-                            p->status = USB_RET_STALL;
218e99
-                        }
218e99
-                    }
218e99
-                    break;
218e99
-                }
218e99
-                aurb[i].iso_frame_idx = -1;
218e99
-                change_iso_inflight(s, p->pid, p->ep->nr, 1);
218e99
-            }
218e99
-        }
218e99
-    }
218e99
-}
218e99
-
218e99
-static void usb_host_handle_data(USBDevice *dev, USBPacket *p)
218e99
-{
218e99
-    USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
218e99
-    struct usbdevfs_urb *urb;
218e99
-    AsyncURB *aurb;
218e99
-    int ret, rem, prem, v;
218e99
-    uint8_t *pbuf;
218e99
-    uint8_t ep;
218e99
-
218e99
-    trace_usb_host_req_data(s->bus_num, s->addr, p,
218e99
-                            p->pid == USB_TOKEN_IN,
218e99
-                            p->ep->nr, p->iov.size);
218e99
-
218e99
-    if (!is_valid(s, p->pid, p->ep->nr)) {
218e99
-        p->status = USB_RET_NAK;
218e99
-        trace_usb_host_req_complete(s->bus_num, s->addr, p,
218e99
-                                    p->status, p->actual_length);
218e99
-        return;
218e99
-    }
218e99
-
218e99
-    if (p->pid == USB_TOKEN_IN) {
218e99
-        ep = p->ep->nr | 0x80;
218e99
-    } else {
218e99
-        ep = p->ep->nr;
218e99
-    }
218e99
-
218e99
-    if (is_halted(s, p->pid, p->ep->nr)) {
218e99
-        unsigned int arg = ep;
218e99
-        ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &arg;;
218e99
-        if (ret < 0) {
218e99
-            perror("USBDEVFS_CLEAR_HALT");
218e99
-            p->status = USB_RET_NAK;
218e99
-            trace_usb_host_req_complete(s->bus_num, s->addr, p,
218e99
-                                        p->status, p->actual_length);
218e99
-            return;
218e99
-        }
218e99
-        clear_halt(s, p->pid, p->ep->nr);
218e99
-    }
218e99
-
218e99
-    if (is_isoc(s, p->pid, p->ep->nr)) {
218e99
-        usb_host_handle_iso_data(s, p, p->pid == USB_TOKEN_IN);
218e99
-        return;
218e99
-    }
218e99
-
218e99
-    v = 0;
218e99
-    prem = 0;
218e99
-    pbuf = NULL;
218e99
-    rem = p->iov.size;
218e99
-    do {
218e99
-        if (prem == 0 && rem > 0) {
218e99
-            assert(v < p->iov.niov);
218e99
-            prem = p->iov.iov[v].iov_len;
218e99
-            pbuf = p->iov.iov[v].iov_base;
218e99
-            assert(prem <= rem);
218e99
-            v++;
218e99
-        }
218e99
-        aurb = async_alloc(s);
218e99
-        aurb->packet = p;
218e99
-
218e99
-        urb = &aurb->urb;
218e99
-        urb->endpoint      = ep;
218e99
-        urb->type          = usb_host_usbfs_type(s, p);
218e99
-        urb->usercontext   = s;
218e99
-        urb->buffer        = pbuf;
218e99
-        urb->buffer_length = prem;
218e99
-
218e99
-        if (urb->buffer_length > MAX_USBFS_BUFFER_SIZE) {
218e99
-            urb->buffer_length = MAX_USBFS_BUFFER_SIZE;
218e99
-        }
218e99
-        pbuf += urb->buffer_length;
218e99
-        prem -= urb->buffer_length;
218e99
-        rem  -= urb->buffer_length;
218e99
-        if (rem) {
218e99
-            aurb->more         = 1;
218e99
-        }
218e99
-
218e99
-        trace_usb_host_urb_submit(s->bus_num, s->addr, aurb,
218e99
-                                  urb->buffer_length, aurb->more);
218e99
-        ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
218e99
-
218e99
-        DPRINTF("husb: data submit: ep 0x%x, len %u, more %d, packet %p, aurb %p\n",
218e99
-                urb->endpoint, urb->buffer_length, aurb->more, p, aurb);
218e99
-
218e99
-        if (ret < 0) {
218e99
-            perror("USBDEVFS_SUBMITURB");
218e99
-            async_free(aurb);
218e99
-
218e99
-            switch(errno) {
218e99
-            case ETIMEDOUT:
218e99
-                p->status = USB_RET_NAK;
218e99
-                trace_usb_host_req_complete(s->bus_num, s->addr, p,
218e99
-                                            p->status, p->actual_length);
218e99
-                break;
218e99
-            case EPIPE:
218e99
-            default:
218e99
-                p->status = USB_RET_STALL;
218e99
-                trace_usb_host_req_complete(s->bus_num, s->addr, p,
218e99
-                                            p->status, p->actual_length);
218e99
-            }
218e99
-            return;
218e99
-        }
218e99
-    } while (rem > 0);
218e99
-
218e99
-    p->status = USB_RET_ASYNC;
218e99
-}
218e99
-
218e99
-static int ctrl_error(void)
218e99
-{
218e99
-    if (errno == ETIMEDOUT) {
218e99
-        return USB_RET_NAK;
218e99
-    } else {
218e99
-        return USB_RET_STALL;
218e99
-    }
218e99
-}
218e99
-
218e99
-static void usb_host_set_address(USBHostDevice *s, int addr)
218e99
-{
218e99
-    trace_usb_host_set_address(s->bus_num, s->addr, addr);
218e99
-    s->dev.addr = addr;
218e99
-}
218e99
-
218e99
-static void usb_host_set_config(USBHostDevice *s, int config, USBPacket *p)
218e99
-{
218e99
-    int ret, first = 1;
218e99
-
218e99
-    trace_usb_host_set_config(s->bus_num, s->addr, config);
218e99
-
218e99
-    usb_host_release_interfaces(s);
218e99
-
218e99
-again:
218e99
-    ret = ioctl(s->fd, USBDEVFS_SETCONFIGURATION, &config);
218e99
-
218e99
-    DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config, ret, errno);
218e99
-
218e99
-    if (ret < 0 && errno == EBUSY && first) {
218e99
-        /* happens if usb device is in use by host drivers */
218e99
-        int count = usb_linux_get_num_interfaces(s);
218e99
-        if (count > 0) {
218e99
-            DPRINTF("husb: busy -> disconnecting %d interfaces\n", count);
218e99
-            usb_host_disconnect_ifaces(s, count);
218e99
-            first = 0;
218e99
-            goto again;
218e99
-        }
218e99
-    }
218e99
-
218e99
-    if (ret < 0) {
218e99
-        p->status = ctrl_error();
218e99
-        return;
218e99
-    }
218e99
-    usb_host_claim_interfaces(s, config);
218e99
-    usb_linux_update_endp_table(s);
218e99
-}
218e99
-
218e99
-static void usb_host_set_interface(USBHostDevice *s, int iface, int alt,
218e99
-                                   USBPacket *p)
218e99
-{
218e99
-    struct usbdevfs_setinterface si;
218e99
-    int i, ret;
218e99
-
218e99
-    trace_usb_host_set_interface(s->bus_num, s->addr, iface, alt);
218e99
-
218e99
-    for (i = 1; i <= USB_MAX_ENDPOINTS; i++) {
218e99
-        if (is_isoc(s, USB_TOKEN_IN, i)) {
218e99
-            usb_host_stop_n_free_iso(s, USB_TOKEN_IN, i);
218e99
-        }
218e99
-        if (is_isoc(s, USB_TOKEN_OUT, i)) {
218e99
-            usb_host_stop_n_free_iso(s, USB_TOKEN_OUT, i);
218e99
-        }
218e99
-    }
218e99
-
218e99
-    if (iface >= USB_MAX_INTERFACES) {
218e99
-        p->status = USB_RET_STALL;
218e99
-        return;
218e99
-    }
218e99
-
218e99
-    si.interface  = iface;
218e99
-    si.altsetting = alt;
218e99
-    ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si);
218e99
-
218e99
-    DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
218e99
-            iface, alt, ret, errno);
218e99
-
218e99
-    if (ret < 0) {
218e99
-        p->status = ctrl_error();
218e99
-        return;
218e99
-    }
218e99
-
218e99
-    s->dev.altsetting[iface] = alt;
218e99
-    usb_linux_update_endp_table(s);
218e99
-}
218e99
-
218e99
-static void usb_host_handle_control(USBDevice *dev, USBPacket *p,
218e99
-               int request, int value, int index, int length, uint8_t *data)
218e99
-{
218e99
-    USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
218e99
-    struct usbdevfs_urb *urb;
218e99
-    AsyncURB *aurb;
218e99
-    int ret;
218e99
-
218e99
-    /*
218e99
-     * Process certain standard device requests.
218e99
-     * These are infrequent and are processed synchronously.
218e99
-     */
218e99
-
218e99
-    /* Note request is (bRequestType << 8) | bRequest */
218e99
-    trace_usb_host_req_control(s->bus_num, s->addr, p, request, value, index);
218e99
-
218e99
-    switch (request) {
218e99
-    case DeviceOutRequest | USB_REQ_SET_ADDRESS:
218e99
-        usb_host_set_address(s, value);
218e99
-        trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
218e99
-        return;
218e99
-
218e99
-    case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
218e99
-        usb_host_set_config(s, value & 0xff, p);
218e99
-        trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
218e99
-        return;
218e99
-
218e99
-    case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
218e99
-        usb_host_set_interface(s, index, value, p);
218e99
-        trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
218e99
-        return;
218e99
-
218e99
-    case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
218e99
-        if (value == 0) { /* clear halt */
218e99
-            int pid = (index & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT;
218e99
-            ioctl(s->fd, USBDEVFS_CLEAR_HALT, &index);
218e99
-            clear_halt(s, pid, index & 0x0f);
218e99
-            trace_usb_host_req_emulated(s->bus_num, s->addr, p, 0);
218e99
-            return;
218e99
-        }
218e99
-    }
218e99
-
218e99
-    /* The rest are asynchronous */
218e99
-    if (length > sizeof(dev->data_buf)) {
218e99
-        fprintf(stderr, "husb: ctrl buffer too small (%d > %zu)\n",
218e99
-                length, sizeof(dev->data_buf));
218e99
-        p->status = USB_RET_STALL;
218e99
-        return;
218e99
-    }
218e99
-
218e99
-    aurb = async_alloc(s);
218e99
-    aurb->packet = p;
218e99
-
218e99
-    /*
218e99
-     * Setup ctrl transfer.
218e99
-     *
218e99
-     * s->ctrl is laid out such that data buffer immediately follows
218e99
-     * 'req' struct which is exactly what usbdevfs expects.
218e99
-     */
218e99
-    urb = &aurb->urb;
218e99
-
218e99
-    urb->type     = USBDEVFS_URB_TYPE_CONTROL;
218e99
-    urb->endpoint = p->ep->nr;
218e99
-
218e99
-    urb->buffer        = &dev->setup_buf;
218e99
-    urb->buffer_length = length + 8;
218e99
-
218e99
-    urb->usercontext = s;
218e99
-
218e99
-    trace_usb_host_urb_submit(s->bus_num, s->addr, aurb,
218e99
-                              urb->buffer_length, aurb->more);
218e99
-    ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
218e99
-
218e99
-    DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb);
218e99
-
218e99
-    if (ret < 0) {
218e99
-        DPRINTF("husb: submit failed. errno %d\n", errno);
218e99
-        async_free(aurb);
218e99
-
218e99
-        switch(errno) {
218e99
-        case ETIMEDOUT:
218e99
-            p->status = USB_RET_NAK;
218e99
-            break;
218e99
-        case EPIPE:
218e99
-        default:
218e99
-            p->status = USB_RET_STALL;
218e99
-            break;
218e99
-        }
218e99
-        return;
218e99
-    }
218e99
-
218e99
-    p->status = USB_RET_ASYNC;
218e99
-}
218e99
-
218e99
-static void usb_linux_update_endp_table(USBHostDevice *s)
218e99
-{
218e99
-    static const char *tname[] = {
218e99
-        [USB_ENDPOINT_XFER_CONTROL] = "control",
218e99
-        [USB_ENDPOINT_XFER_ISOC]    = "isoc",
218e99
-        [USB_ENDPOINT_XFER_BULK]    = "bulk",
218e99
-        [USB_ENDPOINT_XFER_INT]     = "int",
218e99
-    };
218e99
-    uint8_t devep, type;
218e99
-    uint16_t mps, v, p;
218e99
-    int ep, pid;
218e99
-    unsigned int i, configuration = -1, interface = -1, altsetting = -1;
218e99
-    struct endp_data *epd;
218e99
-    USBDescriptor *d;
218e99
-    bool active = false;
218e99
-
218e99
-    usb_ep_reset(&s->dev);
218e99
-
218e99
-    for (i = 0;; i += d->bLength) {
218e99
-        if (i+2 >= s->descr_len) {
218e99
-            break;
218e99
-        }
218e99
-        d = (void *)(s->descr + i);
218e99
-        if (d->bLength < 2) {
218e99
-            trace_usb_host_parse_error(s->bus_num, s->addr,
218e99
-                                       "descriptor too short");
218e99
-            return;
218e99
-        }
218e99
-        if (i + d->bLength > s->descr_len) {
218e99
-            trace_usb_host_parse_error(s->bus_num, s->addr,
218e99
-                                       "descriptor too long");
218e99
-            return;
218e99
-        }
218e99
-        switch (d->bDescriptorType) {
218e99
-        case 0:
218e99
-            trace_usb_host_parse_error(s->bus_num, s->addr,
218e99
-                                       "invalid descriptor type");
218e99
-            return;
218e99
-        case USB_DT_DEVICE:
218e99
-            if (d->bLength < 0x12) {
218e99
-                trace_usb_host_parse_error(s->bus_num, s->addr,
218e99
-                                           "device descriptor too short");
218e99
-                return;
218e99
-            }
218e99
-            v = (d->u.device.idVendor_hi << 8) | d->u.device.idVendor_lo;
218e99
-            p = (d->u.device.idProduct_hi << 8) | d->u.device.idProduct_lo;
218e99
-            trace_usb_host_parse_device(s->bus_num, s->addr, v, p);
218e99
-            break;
218e99
-        case USB_DT_CONFIG:
218e99
-            if (d->bLength < 0x09) {
218e99
-                trace_usb_host_parse_error(s->bus_num, s->addr,
218e99
-                                           "config descriptor too short");
218e99
-                return;
218e99
-            }
218e99
-            configuration = d->u.config.bConfigurationValue;
218e99
-            active = (configuration == s->dev.configuration);
218e99
-            trace_usb_host_parse_config(s->bus_num, s->addr,
218e99
-                                        configuration, active);
218e99
-            break;
218e99
-        case USB_DT_INTERFACE:
218e99
-            if (d->bLength < 0x09) {
218e99
-                trace_usb_host_parse_error(s->bus_num, s->addr,
218e99
-                                           "interface descriptor too short");
218e99
-                return;
218e99
-            }
218e99
-            interface = d->u.interface.bInterfaceNumber;
218e99
-            altsetting = d->u.interface.bAlternateSetting;
218e99
-            active = (configuration == s->dev.configuration) &&
218e99
-                (altsetting == s->dev.altsetting[interface]);
218e99
-            trace_usb_host_parse_interface(s->bus_num, s->addr,
218e99
-                                           interface, altsetting, active);
218e99
-            break;
218e99
-        case USB_DT_ENDPOINT:
218e99
-            if (d->bLength < 0x07) {
218e99
-                trace_usb_host_parse_error(s->bus_num, s->addr,
218e99
-                                           "endpoint descriptor too short");
218e99
-                return;
218e99
-            }
218e99
-            devep = d->u.endpoint.bEndpointAddress;
218e99
-            pid = (devep & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT;
218e99
-            ep = devep & 0xf;
218e99
-            if (ep == 0) {
218e99
-                trace_usb_host_parse_error(s->bus_num, s->addr,
218e99
-                                           "invalid endpoint address");
218e99
-                return;
218e99
-            }
218e99
-
218e99
-            type = d->u.endpoint.bmAttributes & 0x3;
218e99
-            mps = d->u.endpoint.wMaxPacketSize_lo |
218e99
-                (d->u.endpoint.wMaxPacketSize_hi << 8);
218e99
-            trace_usb_host_parse_endpoint(s->bus_num, s->addr, ep,
218e99
-                                          (devep & USB_DIR_IN) ? "in" : "out",
218e99
-                                          tname[type], active);
218e99
-
218e99
-            if (active) {
218e99
-                usb_ep_set_max_packet_size(&s->dev, pid, ep, mps);
218e99
-                assert(usb_ep_get_type(&s->dev, pid, ep) ==
218e99
-                       USB_ENDPOINT_XFER_INVALID);
218e99
-                usb_ep_set_type(&s->dev, pid, ep, type);
218e99
-                usb_ep_set_ifnum(&s->dev, pid, ep, interface);
218e99
-                if ((s->options & (1 << USB_HOST_OPT_PIPELINE)) &&
218e99
-                    (type == USB_ENDPOINT_XFER_BULK) &&
218e99
-                    (pid == USB_TOKEN_OUT)) {
218e99
-                    usb_ep_set_pipeline(&s->dev, pid, ep, true);
218e99
-                }
218e99
-
218e99
-                epd = get_endp(s, pid, ep);
218e99
-                epd->halted = 0;
218e99
-            }
218e99
-
218e99
-            break;
218e99
-        default:
218e99
-            trace_usb_host_parse_unknown(s->bus_num, s->addr,
218e99
-                                         d->bLength, d->bDescriptorType);
218e99
-            break;
218e99
-        }
218e99
-    }
218e99
-}
218e99
-
218e99
-/*
218e99
- * Check if we can safely redirect a usb2 device to a usb1 virtual controller,
218e99
- * this function assumes this is safe, if:
218e99
- * 1) There are no isoc endpoints
218e99
- * 2) There are no interrupt endpoints with a max_packet_size > 64
218e99
- * Note bulk endpoints with a max_packet_size > 64 in theory also are not
218e99
- * usb1 compatible, but in practice this seems to work fine.
218e99
- */
218e99
-static int usb_linux_full_speed_compat(USBHostDevice *dev)
218e99
-{
218e99
-    int i, packet_size;
218e99
-
218e99
-    /*
218e99
-     * usb_linux_update_endp_table only registers info about ep in the current
218e99
-     * interface altsettings, so we need to parse the descriptors again.
218e99
-     */
218e99
-    for (i = 0; (i + 5) < dev->descr_len; i += dev->descr[i]) {
218e99
-        if (dev->descr[i + 1] == USB_DT_ENDPOINT) {
218e99
-            switch (dev->descr[i + 3] & 0x3) {
218e99
-            case 0x00: /* CONTROL */
218e99
-                break;
218e99
-            case 0x01: /* ISO */
218e99
-                return 0;
218e99
-            case 0x02: /* BULK */
218e99
-                break;
218e99
-            case 0x03: /* INTERRUPT */
218e99
-                packet_size = dev->descr[i + 4] + (dev->descr[i + 5] << 8);
218e99
-                if (packet_size > 64)
218e99
-                    return 0;
218e99
-                break;
218e99
-            }
218e99
-        }
218e99
-    }
218e99
-    return 1;
218e99
-}
218e99
-
218e99
-static int usb_host_open(USBHostDevice *dev, int bus_num,
218e99
-                         int addr, const char *port,
218e99
-                         const char *prod_name, int speed)
218e99
-{
218e99
-    int fd = -1, ret;
218e99
-
218e99
-    trace_usb_host_open_started(bus_num, addr);
218e99
-
218e99
-    if (dev->fd != -1) {
218e99
-        goto fail;
218e99
-    }
218e99
-
218e99
-    fd = usb_host_open_device(bus_num, addr);
218e99
-    if (fd < 0) {
218e99
-        goto fail;
218e99
-    }
218e99
-    DPRINTF("husb: opened %s\n", buf);
218e99
-
218e99
-    dev->bus_num = bus_num;
218e99
-    dev->addr = addr;
218e99
-    pstrcpy(dev->port, sizeof(dev->port), port);
218e99
-    dev->fd = fd;
218e99
-
218e99
-    /* read the device description */
218e99
-    dev->descr_len = read(fd, dev->descr, sizeof(dev->descr));
218e99
-    if (dev->descr_len <= 0) {
218e99
-        perror("husb: reading device data failed");
218e99
-        goto fail;
218e99
-    }
218e99
-
218e99
-#ifdef DEBUG
218e99
-    {
218e99
-        int x;
218e99
-        printf("=== begin dumping device descriptor data ===\n");
218e99
-        for (x = 0; x < dev->descr_len; x++) {
218e99
-            printf("%02x ", dev->descr[x]);
218e99
-        }
218e99
-        printf("\n=== end dumping device descriptor data ===\n");
218e99
-    }
218e99
-#endif
218e99
-
218e99
-
218e99
-    /* start unconfigured -- we'll wait for the guest to set a configuration */
218e99
-    if (!usb_host_claim_interfaces(dev, 0)) {
218e99
-        goto fail;
218e99
-    }
218e99
-
218e99
-    usb_ep_init(&dev->dev);
218e99
-    usb_linux_update_endp_table(dev);
218e99
-
218e99
-    if (speed == -1) {
218e99
-        struct usbdevfs_connectinfo ci;
218e99
-
218e99
-        ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);
218e99
-        if (ret < 0) {
218e99
-            perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
218e99
-            goto fail;
218e99
-        }
218e99
-
218e99
-        if (ci.slow) {
218e99
-            speed = USB_SPEED_LOW;
218e99
-        } else {
218e99
-            speed = USB_SPEED_HIGH;
218e99
-        }
218e99
-    }
218e99
-    dev->dev.speed = speed;
218e99
-    dev->dev.speedmask = (1 << speed);
218e99
-    if (dev->dev.speed == USB_SPEED_HIGH && usb_linux_full_speed_compat(dev)) {
218e99
-        dev->dev.speedmask |= USB_SPEED_MASK_FULL;
218e99
-    }
218e99
-
218e99
-    trace_usb_host_open_success(bus_num, addr);
218e99
-
218e99
-    if (!prod_name || prod_name[0] == '\0') {
218e99
-        snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc),
218e99
-                 "host:%d.%d", bus_num, addr);
218e99
-    } else {
218e99
-        pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc),
218e99
-                prod_name);
218e99
-    }
218e99
-
218e99
-    ret = usb_device_attach(&dev->dev);
218e99
-    if (ret) {
218e99
-        goto fail;
218e99
-    }
218e99
-
218e99
-    /* USB devio uses 'write' flag to check for async completions */
218e99
-    qemu_set_fd_handler(dev->fd, NULL, async_complete, dev);
218e99
-
218e99
-    return 0;
218e99
-
218e99
-fail:
218e99
-    trace_usb_host_open_failure(bus_num, addr);
218e99
-    if (dev->fd != -1) {
218e99
-        close(dev->fd);
218e99
-        dev->fd = -1;
218e99
-    }
218e99
-    return -1;
218e99
-}
218e99
-
218e99
-static int usb_host_close(USBHostDevice *dev)
218e99
-{
218e99
-    int i;
218e99
-
218e99
-    if (dev->fd == -1) {
218e99
-        return -1;
218e99
-    }
218e99
-
218e99
-    trace_usb_host_close(dev->bus_num, dev->addr);
218e99
-
218e99
-    qemu_set_fd_handler(dev->fd, NULL, NULL, NULL);
218e99
-    dev->closing = 1;
218e99
-    for (i = 1; i <= USB_MAX_ENDPOINTS; i++) {
218e99
-        if (is_isoc(dev, USB_TOKEN_IN, i)) {
218e99
-            usb_host_stop_n_free_iso(dev, USB_TOKEN_IN, i);
218e99
-        }
218e99
-        if (is_isoc(dev, USB_TOKEN_OUT, i)) {
218e99
-            usb_host_stop_n_free_iso(dev, USB_TOKEN_OUT, i);
218e99
-        }
218e99
-    }
218e99
-    async_complete(dev);
218e99
-    dev->closing = 0;
218e99
-    if (dev->dev.attached) {
218e99
-        usb_device_detach(&dev->dev);
218e99
-    }
218e99
-    usb_host_do_reset(dev);
218e99
-    close(dev->fd);
218e99
-    dev->fd = -1;
218e99
-    return 0;
218e99
-}
218e99
-
218e99
-static void usb_host_exit_notifier(struct Notifier *n, void *data)
218e99
-{
218e99
-    USBHostDevice *s = container_of(n, USBHostDevice, exit);
218e99
-
218e99
-    usb_host_release_port(s);
218e99
-    if (s->fd != -1) {
218e99
-        usb_host_do_reset(s);
218e99
-    }
218e99
-}
218e99
-
218e99
-/*
218e99
- * This is *NOT* about restoring state.  We have absolutely no idea
218e99
- * what state the host device is in at the moment and whenever it is
218e99
- * still present in the first place.  Attemping to contine where we
218e99
- * left off is impossible.
218e99
- *
218e99
- * What we are going to to to here is emulate a surprise removal of
218e99
- * the usb device passed through, then kick host scan so the device
218e99
- * will get re-attached (and re-initialized by the guest) in case it
218e99
- * is still present.
218e99
- *
218e99
- * As the device removal will change the state of other devices (usb
218e99
- * host controller, most likely interrupt controller too) we have to
218e99
- * wait with it until *all* vmstate is loaded.  Thus post_load just
218e99
- * kicks a bottom half which then does the actual work.
218e99
- */
218e99
-static void usb_host_post_load_bh(void *opaque)
218e99
-{
218e99
-    USBHostDevice *dev = opaque;
218e99
-
218e99
-    if (dev->fd != -1) {
218e99
-        usb_host_close(dev);
218e99
-    }
218e99
-    if (dev->dev.attached) {
218e99
-        usb_device_detach(&dev->dev);
218e99
-    }
218e99
-    usb_host_auto_check(NULL);
218e99
-}
218e99
-
218e99
-static int usb_host_post_load(void *opaque, int version_id)
218e99
-{
218e99
-    USBHostDevice *dev = opaque;
218e99
-
218e99
-    qemu_bh_schedule(dev->bh);
218e99
-    return 0;
218e99
-}
218e99
-
218e99
-static int usb_host_initfn(USBDevice *dev)
218e99
-{
218e99
-    USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
218e99
-
218e99
-    dev->flags |= (1 << USB_DEV_FLAG_IS_HOST);
218e99
-    dev->auto_attach = 0;
218e99
-    s->fd = -1;
218e99
-    s->hub_fd = -1;
218e99
-
218e99
-    QTAILQ_INSERT_TAIL(&hostdevs, s, next);
218e99
-    s->exit.notify = usb_host_exit_notifier;
218e99
-    qemu_add_exit_notifier(&s->exit);
218e99
-    s->bh = qemu_bh_new(usb_host_post_load_bh, s);
218e99
-    usb_host_auto_check(NULL);
218e99
-
218e99
-    if (s->match.bus_num != 0 && s->match.port != NULL) {
218e99
-        usb_host_claim_port(s);
218e99
-    }
218e99
-    add_boot_device_path(s->bootindex, &dev->qdev, NULL);
218e99
-    return 0;
218e99
-}
218e99
-
218e99
-static const VMStateDescription vmstate_usb_host = {
218e99
-    .name = DEVNAME,
218e99
-    .version_id = 1,
218e99
-    .minimum_version_id = 1,
218e99
-    .post_load = usb_host_post_load,
218e99
-    .fields = (VMStateField[]) {
218e99
-        VMSTATE_USB_DEVICE(dev, USBHostDevice),
218e99
-        VMSTATE_END_OF_LIST()
218e99
-    }
218e99
-};
218e99
-
218e99
-static Property usb_host_dev_properties[] = {
218e99
-    DEFINE_PROP_UINT32("hostbus",  USBHostDevice, match.bus_num,    0),
218e99
-    DEFINE_PROP_UINT32("hostaddr", USBHostDevice, match.addr,       0),
218e99
-    DEFINE_PROP_STRING("hostport", USBHostDevice, match.port),
218e99
-    DEFINE_PROP_HEX32("vendorid",  USBHostDevice, match.vendor_id,  0),
218e99
-    DEFINE_PROP_HEX32("productid", USBHostDevice, match.product_id, 0),
218e99
-    DEFINE_PROP_UINT32("isobufs",  USBHostDevice, iso_urb_count,    4),
218e99
-    DEFINE_PROP_INT32("bootindex", USBHostDevice, bootindex,        -1),
218e99
-    DEFINE_PROP_BIT("pipeline",    USBHostDevice, options,
218e99
-                    USB_HOST_OPT_PIPELINE, true),
218e99
-    DEFINE_PROP_END_OF_LIST(),
218e99
-};
218e99
-
218e99
-static void usb_host_class_initfn(ObjectClass *klass, void *data)
218e99
-{
218e99
-    DeviceClass *dc = DEVICE_CLASS(klass);
218e99
-    USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
218e99
-
218e99
-    uc->init           = usb_host_initfn;
218e99
-    uc->product_desc   = "USB Host Device";
218e99
-    uc->cancel_packet  = usb_host_async_cancel;
218e99
-    uc->handle_data    = usb_host_handle_data;
218e99
-    uc->handle_control = usb_host_handle_control;
218e99
-    uc->handle_reset   = usb_host_handle_reset;
218e99
-    uc->handle_destroy = usb_host_handle_destroy;
218e99
-    dc->vmsd = &vmstate_usb_host;
218e99
-    dc->props = usb_host_dev_properties;
218e99
-}
218e99
-
218e99
-static const TypeInfo usb_host_dev_info = {
218e99
-    .name          = DEVNAME,
218e99
-    .parent        = TYPE_USB_DEVICE,
218e99
-    .instance_size = sizeof(USBHostDevice),
218e99
-    .class_init    = usb_host_class_initfn,
218e99
-};
218e99
-
218e99
-static void usb_host_register_types(void)
218e99
-{
218e99
-    type_register_static(&usb_host_dev_info);
218e99
-}
218e99
-
218e99
-type_init(usb_host_register_types)
218e99
-
218e99
-/*
218e99
- * Read sys file-system device file
218e99
- *
218e99
- * @line address of buffer to put file contents in
218e99
- * @line_size size of line
218e99
- * @device_file path to device file (printf format string)
218e99
- * @device_name device being opened (inserted into device_file)
218e99
- *
218e99
- * @return 0 failed, 1 succeeded ('line' contains data)
218e99
- */
218e99
-static int usb_host_read_file(char *line, size_t line_size,
218e99
-                              const char *device_file, const char *device_name)
218e99
-{
218e99
-    FILE *f;
218e99
-    int ret = 0;
218e99
-    char filename[PATH_MAX];
218e99
-
218e99
-    snprintf(filename, PATH_MAX, "/sys/bus/usb/devices/%s/%s", device_name,
218e99
-             device_file);
218e99
-    f = fopen(filename, "r");
218e99
-    if (f) {
218e99
-        ret = fgets(line, line_size, f) != NULL;
218e99
-        fclose(f);
218e99
-    }
218e99
-
218e99
-    return ret;
218e99
-}
218e99
-
218e99
-/*
218e99
- * Use /sys/bus/usb/devices/ directory to determine host's USB
218e99
- * devices.
218e99
- *
218e99
- * This code is based on Robert Schiele's original patches posted to
218e99
- * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
218e99
- */
218e99
-static int usb_host_scan(void *opaque, USBScanFunc *func)
218e99
-{
218e99
-    DIR *dir = NULL;
218e99
-    char line[1024];
218e99
-    int bus_num, addr, speed, class_id, product_id, vendor_id;
218e99
-    int ret = 0;
218e99
-    char port[MAX_PORTLEN];
218e99
-    char product_name[512];
218e99
-    struct dirent *de;
218e99
-
218e99
-    dir = opendir("/sys/bus/usb/devices");
218e99
-    if (!dir) {
218e99
-        perror("husb: opendir /sys/bus/usb/devices");
218e99
-        fprintf(stderr, "husb: please make sure sysfs is mounted at /sys\n");
218e99
-        goto the_end;
218e99
-    }
218e99
-
218e99
-    while ((de = readdir(dir))) {
218e99
-        if (de->d_name[0] != '.' && !strchr(de->d_name, ':')) {
218e99
-            if (sscanf(de->d_name, "%d-%7[0-9.]", &bus_num, port) < 2) {
218e99
-                continue;
218e99
-            }
218e99
-
218e99
-            if (!usb_host_read_file(line, sizeof(line), "devnum", de->d_name)) {
218e99
-                goto the_end;
218e99
-            }
218e99
-            if (sscanf(line, "%d", &addr) != 1) {
218e99
-                goto the_end;
218e99
-            }
218e99
-            if (!usb_host_read_file(line, sizeof(line), "bDeviceClass",
218e99
-                                    de->d_name)) {
218e99
-                goto the_end;
218e99
-            }
218e99
-            if (sscanf(line, "%x", &class_id) != 1) {
218e99
-                goto the_end;
218e99
-            }
218e99
-
218e99
-            if (!usb_host_read_file(line, sizeof(line), "idVendor",
218e99
-                                    de->d_name)) {
218e99
-                goto the_end;
218e99
-            }
218e99
-            if (sscanf(line, "%x", &vendor_id) != 1) {
218e99
-                goto the_end;
218e99
-            }
218e99
-            if (!usb_host_read_file(line, sizeof(line), "idProduct",
218e99
-                                    de->d_name)) {
218e99
-                goto the_end;
218e99
-            }
218e99
-            if (sscanf(line, "%x", &product_id) != 1) {
218e99
-                goto the_end;
218e99
-            }
218e99
-            if (!usb_host_read_file(line, sizeof(line), "product",
218e99
-                                    de->d_name)) {
218e99
-                *product_name = 0;
218e99
-            } else {
218e99
-                if (strlen(line) > 0) {
218e99
-                    line[strlen(line) - 1] = '\0';
218e99
-                }
218e99
-                pstrcpy(product_name, sizeof(product_name), line);
218e99
-            }
218e99
-
218e99
-            if (!usb_host_read_file(line, sizeof(line), "speed", de->d_name)) {
218e99
-                goto the_end;
218e99
-            }
218e99
-            if (!strcmp(line, "5000\n")) {
218e99
-                speed = USB_SPEED_SUPER;
218e99
-            } else if (!strcmp(line, "480\n")) {
218e99
-                speed = USB_SPEED_HIGH;
218e99
-            } else if (!strcmp(line, "1.5\n")) {
218e99
-                speed = USB_SPEED_LOW;
218e99
-            } else {
218e99
-                speed = USB_SPEED_FULL;
218e99
-            }
218e99
-
218e99
-            ret = func(opaque, bus_num, addr, port, class_id, vendor_id,
218e99
-                       product_id, product_name, speed);
218e99
-            if (ret) {
218e99
-                goto the_end;
218e99
-            }
218e99
-        }
218e99
-    }
218e99
- the_end:
218e99
-    if (dir) {
218e99
-        closedir(dir);
218e99
-    }
218e99
-    return ret;
218e99
-}
218e99
-
218e99
-static QEMUTimer *usb_auto_timer;
218e99
-static VMChangeStateEntry *usb_vmstate;
218e99
-
218e99
-static int usb_host_auto_scan(void *opaque, int bus_num,
218e99
-                              int addr, const char *port,
218e99
-                              int class_id, int vendor_id, int product_id,
218e99
-                              const char *product_name, int speed)
218e99
-{
218e99
-    struct USBAutoFilter *f;
218e99
-    struct USBHostDevice *s;
218e99
-
218e99
-    /* Ignore hubs */
218e99
-    if (class_id == 9)
218e99
-        return 0;
218e99
-
218e99
-    QTAILQ_FOREACH(s, &hostdevs, next) {
218e99
-        f = &s->match;
218e99
-
218e99
-        if (f->bus_num > 0 && f->bus_num != bus_num) {
218e99
-            continue;
218e99
-        }
218e99
-        if (f->addr > 0 && f->addr != addr) {
218e99
-            continue;
218e99
-        }
218e99
-        if (f->port != NULL && strcmp(f->port, port) != 0) {
218e99
-            continue;
218e99
-        }
218e99
-
218e99
-        if (f->vendor_id > 0 && f->vendor_id != vendor_id) {
218e99
-            continue;
218e99
-        }
218e99
-
218e99
-        if (f->product_id > 0 && f->product_id != product_id) {
218e99
-            continue;
218e99
-        }
218e99
-        /* We got a match */
218e99
-        s->seen++;
218e99
-        if (s->errcount >= 3) {
218e99
-            return 0;
218e99
-        }
218e99
-
218e99
-        /* Already attached ? */
218e99
-        if (s->fd != -1) {
218e99
-            return 0;
218e99
-        }
218e99
-        DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num, addr);
218e99
-
218e99
-        if (usb_host_open(s, bus_num, addr, port, product_name, speed) < 0) {
218e99
-            s->errcount++;
218e99
-        }
218e99
-        break;
218e99
-    }
218e99
-
218e99
-    return 0;
218e99
-}
218e99
-
218e99
-static void usb_host_vm_state(void *unused, int running, RunState state)
218e99
-{
218e99
-    if (running) {
218e99
-        usb_host_auto_check(unused);
218e99
-    }
218e99
-}
218e99
-
218e99
-static void usb_host_auto_check(void *unused)
218e99
-{
218e99
-    struct USBHostDevice *s;
218e99
-    int unconnected = 0;
218e99
-
218e99
-    if (runstate_is_running()) {
218e99
-        usb_host_scan(NULL, usb_host_auto_scan);
218e99
-
218e99
-        QTAILQ_FOREACH(s, &hostdevs, next) {
218e99
-            if (s->fd == -1) {
218e99
-                unconnected++;
218e99
-            }
218e99
-            if (s->seen == 0) {
218e99
-                s->errcount = 0;
218e99
-            }
218e99
-            s->seen = 0;
218e99
-        }
218e99
-
218e99
-        if (unconnected == 0) {
218e99
-            /* nothing to watch */
218e99
-            if (usb_auto_timer) {
218e99
-                qemu_del_timer(usb_auto_timer);
218e99
-                trace_usb_host_auto_scan_disabled();
218e99
-            }
218e99
-            return;
218e99
-        }
218e99
-    }
218e99
-
218e99
-    if (!usb_vmstate) {
218e99
-        usb_vmstate = qemu_add_vm_change_state_handler(usb_host_vm_state, NULL);
218e99
-    }
218e99
-    if (!usb_auto_timer) {
218e99
-        usb_auto_timer = qemu_new_timer_ms(rt_clock, usb_host_auto_check, NULL);
218e99
-        if (!usb_auto_timer) {
218e99
-            return;
218e99
-        }
218e99
-        trace_usb_host_auto_scan_enabled();
218e99
-    }
218e99
-    qemu_mod_timer(usb_auto_timer, qemu_get_clock_ms(rt_clock) + 2000);
218e99
-}
218e99
-
218e99
-#ifndef CONFIG_USB_LIBUSB
218e99
-
218e99
-/**********************/
218e99
-/* USB host device info */
218e99
-
218e99
-struct usb_class_info {
218e99
-    int class;
218e99
-    const char *class_name;
218e99
-};
218e99
-
218e99
-static const struct usb_class_info usb_class_info[] = {
218e99
-    { USB_CLASS_AUDIO, "Audio"},
218e99
-    { USB_CLASS_COMM, "Communication"},
218e99
-    { USB_CLASS_HID, "HID"},
218e99
-    { USB_CLASS_HUB, "Hub" },
218e99
-    { USB_CLASS_PHYSICAL, "Physical" },
218e99
-    { USB_CLASS_PRINTER, "Printer" },
218e99
-    { USB_CLASS_MASS_STORAGE, "Storage" },
218e99
-    { USB_CLASS_CDC_DATA, "Data" },
218e99
-    { USB_CLASS_APP_SPEC, "Application Specific" },
218e99
-    { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
218e99
-    { USB_CLASS_STILL_IMAGE, "Still Image" },
218e99
-    { USB_CLASS_CSCID, "Smart Card" },
218e99
-    { USB_CLASS_CONTENT_SEC, "Content Security" },
218e99
-    { -1, NULL }
218e99
-};
218e99
-
218e99
-static const char *usb_class_str(uint8_t class)
218e99
-{
218e99
-    const struct usb_class_info *p;
218e99
-    for(p = usb_class_info; p->class != -1; p++) {
218e99
-        if (p->class == class) {
218e99
-            break;
218e99
-        }
218e99
-    }
218e99
-    return p->class_name;
218e99
-}
218e99
-
218e99
-static void usb_info_device(Monitor *mon, int bus_num,
218e99
-                            int addr, const char *port,
218e99
-                            int class_id, int vendor_id, int product_id,
218e99
-                            const char *product_name,
218e99
-                            int speed)
218e99
-{
218e99
-    const char *class_str, *speed_str;
218e99
-
218e99
-    switch(speed) {
218e99
-    case USB_SPEED_LOW:
218e99
-        speed_str = "1.5";
218e99
-        break;
218e99
-    case USB_SPEED_FULL:
218e99
-        speed_str = "12";
218e99
-        break;
218e99
-    case USB_SPEED_HIGH:
218e99
-        speed_str = "480";
218e99
-        break;
218e99
-    case USB_SPEED_SUPER:
218e99
-        speed_str = "5000";
218e99
-        break;
218e99
-    default:
218e99
-        speed_str = "?";
218e99
-        break;
218e99
-    }
218e99
-
218e99
-    monitor_printf(mon, "  Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
218e99
-                   bus_num, addr, port, speed_str);
218e99
-    class_str = usb_class_str(class_id);
218e99
-    if (class_str) {
218e99
-        monitor_printf(mon, "    %s:", class_str);
218e99
-    } else {
218e99
-        monitor_printf(mon, "    Class %02x:", class_id);
218e99
-    }
218e99
-    monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id);
218e99
-    if (product_name[0] != '\0') {
218e99
-        monitor_printf(mon, ", %s", product_name);
218e99
-    }
218e99
-    monitor_printf(mon, "\n");
218e99
-}
218e99
-
218e99
-static int usb_host_info_device(void *opaque, int bus_num, int addr,
218e99
-                                const char *path, int class_id,
218e99
-                                int vendor_id, int product_id,
218e99
-                                const char *product_name,
218e99
-                                int speed)
218e99
-{
218e99
-    Monitor *mon = opaque;
218e99
-
218e99
-    usb_info_device(mon, bus_num, addr, path, class_id, vendor_id, product_id,
218e99
-                    product_name, speed);
218e99
-    return 0;
218e99
-}
218e99
-
218e99
-static void dec2str(int val, char *str, size_t size)
218e99
-{
218e99
-    if (val == 0) {
218e99
-        snprintf(str, size, "*");
218e99
-    } else {
218e99
-        snprintf(str, size, "%d", val);
218e99
-    }
218e99
-}
218e99
-
218e99
-static void hex2str(int val, char *str, size_t size)
218e99
-{
218e99
-    if (val == 0) {
218e99
-        snprintf(str, size, "*");
218e99
-    } else {
218e99
-        snprintf(str, size, "%04x", val);
218e99
-    }
218e99
-}
218e99
-
218e99
-void usb_host_info(Monitor *mon, const QDict *qdict)
218e99
-{
218e99
-    struct USBAutoFilter *f;
218e99
-    struct USBHostDevice *s;
218e99
-
218e99
-    usb_host_scan(mon, usb_host_info_device);
218e99
-
218e99
-    if (QTAILQ_EMPTY(&hostdevs)) {
218e99
-        return;
218e99
-    }
218e99
-
218e99
-    monitor_printf(mon, "  Auto filters:\n");
218e99
-    QTAILQ_FOREACH(s, &hostdevs, next) {
218e99
-        char bus[10], addr[10], vid[10], pid[10];
218e99
-        f = &s->match;
218e99
-        dec2str(f->bus_num, bus, sizeof(bus));
218e99
-        dec2str(f->addr, addr, sizeof(addr));
218e99
-        hex2str(f->vendor_id, vid, sizeof(vid));
218e99
-        hex2str(f->product_id, pid, sizeof(pid));
218e99
-        monitor_printf(mon, "    Bus %s, Addr %s, Port %s, ID %s:%s\n",
218e99
-                       bus, addr, f->port ? f->port : "*", vid, pid);
218e99
-    }
218e99
-}
218e99
-
218e99
-#endif
218e99
-- 
218e99
1.7.1
218e99