From patchwork Tue May 31 09:39:42 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans de Goede X-Patchwork-Id: 97998 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 6987AB6F6C for ; Tue, 31 May 2011 19:40:53 +1000 (EST) Received: from localhost ([::1]:39652 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QRLRS-0000LI-Pj for incoming@patchwork.ozlabs.org; Tue, 31 May 2011 05:40:50 -0400 Received: from eggs.gnu.org ([140.186.70.92]:48454) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QRLQF-0006IX-4z for qemu-devel@nongnu.org; Tue, 31 May 2011 05:39:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QRLQE-0003Ny-6x for qemu-devel@nongnu.org; Tue, 31 May 2011 05:39:35 -0400 Received: from mx1.redhat.com ([209.132.183.28]:29413) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QRLQD-0003Nl-VB for qemu-devel@nongnu.org; Tue, 31 May 2011 05:39:34 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p4V9dX5A009502 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Tue, 31 May 2011 05:39:33 -0400 Received: from shalem.localdomain.com (vpn1-4-207.ams2.redhat.com [10.36.4.207]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id p4V9dSsD003893; Tue, 31 May 2011 05:39:32 -0400 From: Hans de Goede To: qemu-devel@nongnu.org Date: Tue, 31 May 2011 11:39:42 +0200 Message-Id: <1306834783-13070-4-git-send-email-hdegoede@redhat.com> In-Reply-To: <1306834783-13070-1-git-send-email-hdegoede@redhat.com> References: <1306834783-13070-1-git-send-email-hdegoede@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 Cc: Hans de Goede Subject: [Qemu-devel] [PATCH 3/4] usb-bus: Automatically select right usb bus based on device and bus speed X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org If the bus was not explictly specified by the user, automatically select the right usb bus based on device and bus speed. --- hw/qdev.c | 2 +- hw/usb-bus.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++- hw/usb.h | 1 + 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/hw/qdev.c b/hw/qdev.c index 9519f5d..f032412 100644 --- a/hw/qdev.c +++ b/hw/qdev.c @@ -260,11 +260,11 @@ DeviceState *qdev_device_add(QemuOpts *opts) qdev_free(qdev); return NULL; } + qdev->opts = opts; if (qdev_init(qdev) < 0) { qerror_report(QERR_DEVICE_INIT_FAILED, driver); return NULL; } - qdev->opts = opts; return qdev; } diff --git a/hw/usb-bus.c b/hw/usb-bus.c index 1817d5c..00b2df8 100644 --- a/hw/usb-bus.c +++ b/hw/usb-bus.c @@ -72,6 +72,9 @@ static int usb_qdev_init(DeviceState *qdev, DeviceInfo *base) pstrcpy(dev->product_desc, sizeof(dev->product_desc), info->product_desc); dev->info = info; dev->auto_attach = 1; + if (qdev->opts && qemu_opt_get(qdev->opts, "bus")) { + dev->bus_specified = 1; + } QLIST_INIT(&dev->strings); rc = dev->info->init(dev); if (rc == 0 && dev->auto_attach) @@ -114,6 +117,8 @@ void usb_qdev_register_many(USBDeviceInfo *info) USBDevice *usb_create(USBBus *bus, const char *name) { DeviceState *dev; + USBDevice *usbdev; + int bus_specified = 1; #if 1 /* temporary stopgap until all usb is properly qdev-ified */ @@ -123,11 +128,15 @@ USBDevice *usb_create(USBBus *bus, const char *name) return NULL; fprintf(stderr, "%s: no bus specified, using \"%s\" for \"%s\"\n", __FUNCTION__, bus->qbus.name, name); + bus_specified = 0; } #endif dev = qdev_create(&bus->qbus, name); - return DO_UPCAST(USBDevice, qdev, dev); + usbdev = DO_UPCAST(USBDevice, qdev, dev); + usbdev->bus_specified = bus_specified; + + return usbdev; } USBDevice *usb_create_simple(USBBus *bus, const char *name) @@ -171,6 +180,35 @@ void usb_unregister_port(USBBus *bus, USBPort *port) bus->nfree--; } +static USBBus *find_bus_by_speed(int speedmask) +{ + USBBus *bus; + USBPort *port; + + QTAILQ_FOREACH(bus, &busses, next) { + port = QTAILQ_FIRST(&bus->free); + if (port && (port->speedmask & speedmask)) { + return bus; + } + } + return NULL; +} + +static USBBus *find_best_bus_by_speed(USBDevice *dev) +{ + USBBus *bus; + + /* If the device supports high speed, first try to find a highspeed port */ + if (dev->speedmask & USB_SPEED_MASK_HIGH) { + bus = find_bus_by_speed(USB_SPEED_MASK_HIGH); + if (bus) { + return bus; + } + } + + return find_bus_by_speed(dev->speedmask); +} + static int do_attach(USBDevice *dev) { USBBus *bus = usb_bus_from_device(dev); @@ -181,6 +219,19 @@ static int do_attach(USBDevice *dev) dev->product_desc); return -1; } + + /* If the bus was not explicitly specified, select one by speed */ + if (!dev->bus_specified) { + USBBus *newbus = find_best_bus_by_speed(dev); + if (newbus && newbus != bus) { + /* A bit tricky, but safe since we're not attached */ + QLIST_REMOVE(&dev->qdev, sibling); + dev->qdev.parent_bus = &newbus->qbus; + QLIST_INSERT_HEAD(&newbus->qbus.children, &dev->qdev, sibling); + bus = newbus; + } + } + if (bus->nfree == 0) { fprintf(stderr, "Error: tried to attach usb device %s to a bus with no free ports\n", dev->product_desc); diff --git a/hw/usb.h b/hw/usb.h index 5623f34..807f9e3 100644 --- a/hw/usb.h +++ b/hw/usb.h @@ -177,6 +177,7 @@ struct USBDevice { char product_desc[32]; int auto_attach; int attached; + int bus_specified; int32_t state; uint8_t setup_buf[8];