From nobody Tue Feb 10 00:58:46 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1559111865; cv=none; d=zoho.com; s=zohoarc; b=I9Gb9w9xHeRkWiaGt4rO27BMvdvdBKSL8BC4Qw8MNJYig7OIK50Yu6knS3+srFdPlbMBoKe83gR8yHHT8OoCtfAw1QHc7dMS4+E5qJOkUpqFY7RSpqGufJxaxlc3G80iUNn8Ny3zreECJcZdJdhEvWK/grdRofI6xfz3nFNEtyA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1559111865; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=IbzsQUjEGeJiH74rUBBG663huD/FlWu0IjSV/JifhPM=; b=n/wNrSsvRGxEzjXdDW8H60m2L86vrRGz2x5cklFiF5jIR4qWKUwWuRou8LwgFNAakqPkfYHGJxzk6vSoZ1E/5AkG3/06f5MeE4NSrj6Ktuf7bF/Wx3zytTeWGzEf8PliUrkWraXBJCIx1xZlsUh9sx39mhIdDfhmMkh2ynrNrFQ= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1559111865139816.4049637546261; Tue, 28 May 2019 23:37:45 -0700 (PDT) Received: from localhost ([127.0.0.1]:48144 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hVsDV-0006rx-8j for importer@patchew.org; Wed, 29 May 2019 02:37:41 -0400 Received: from eggs.gnu.org ([209.51.188.92]:42076) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hVs4q-0000ck-Hf for qemu-devel@nongnu.org; Wed, 29 May 2019 02:28:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hVs4o-0004gt-I4 for qemu-devel@nongnu.org; Wed, 29 May 2019 02:28:44 -0400 Received: from mx1.redhat.com ([209.132.183.28]:53476) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hVs4o-0004fw-BT for qemu-devel@nongnu.org; Wed, 29 May 2019 02:28:42 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 154AD7E423; Wed, 29 May 2019 06:28:41 +0000 (UTC) Received: from sirius.home.kraxel.org (ovpn-116-59.ams2.redhat.com [10.36.116.59]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2CF9E61B66; Wed, 29 May 2019 06:28:38 +0000 (UTC) Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id 41B7D17535; Wed, 29 May 2019 08:28:33 +0200 (CEST) From: Gerd Hoffmann To: qemu-devel@nongnu.org Date: Wed, 29 May 2019 08:28:31 +0200 Message-Id: <20190529062832.26483-9-kraxel@redhat.com> In-Reply-To: <20190529062832.26483-1-kraxel@redhat.com> References: <20190529062832.26483-1-kraxel@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Wed, 29 May 2019 06:28:41 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 8/9] usb-hub: emulate per port power switching X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , Gerd Hoffmann Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Add support for per port power switching. Virtual power of course ;) Use port-power=3Don property to enable this. Signed-off-by: Gerd Hoffmann Message-id: 20190524070310.4952-6-kraxel@redhat.com --- hw/usb/dev-hub.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/hw/usb/dev-hub.c b/hw/usb/dev-hub.c index 29f4d6723e26..2b64d6ef0349 100644 --- a/hw/usb/dev-hub.c +++ b/hw/usb/dev-hub.c @@ -24,6 +24,7 @@ #include "qemu/osdep.h" #include "qapi/error.h" #include "qemu-common.h" +#include "qemu/timer.h" #include "trace.h" #include "hw/usb.h" #include "desc.h" @@ -41,6 +42,8 @@ typedef struct USBHubState { USBDevice dev; USBEndpoint *intr; uint32_t num_ports; + bool port_power; + QEMUTimer *port_timer; USBHubPort ports[MAX_PORTS]; } USBHubState; =20 @@ -203,6 +206,20 @@ static bool usb_hub_port_update(USBHubPort *port) return notify; } =20 +static void usb_hub_port_update_timer(void *opaque) +{ + USBHubState *s =3D opaque; + bool notify =3D false; + int i; + + for (i =3D 0; i < s->num_ports; i++) { + notify |=3D usb_hub_port_update(&s->ports[i]); + } + if (notify) { + usb_wakeup(s->intr, 0); + } +} + static void usb_hub_attach(USBPort *port1) { USBHubState *s =3D port1->opaque; @@ -405,6 +422,11 @@ static void usb_hub_handle_control(USBDevice *dev, USB= Packet *p, usb_wakeup(s->intr, 0); break; case PORT_POWER: + if (s->port_power) { + int64_t now =3D qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); + usb_hub_port_set(port, PORT_STAT_POWER); + timer_mod(s->port_timer, now + 5000000); /* 5 ms */ + } break; default: goto fail; @@ -445,6 +467,14 @@ static void usb_hub_handle_control(USBDevice *dev, USB= Packet *p, case PORT_C_RESET: port->wPortChange &=3D ~PORT_STAT_C_RESET; break; + case PORT_POWER: + if (s->port_power) { + usb_hub_port_clear(port, PORT_STAT_POWER); + usb_hub_port_clear(port, PORT_STAT_CONNECTION); + usb_hub_port_clear(port, PORT_STAT_ENABLE); + usb_hub_port_clear(port, PORT_STAT_SUSPEND); + port->wPortChange =3D 0; + } default: goto fail; } @@ -457,6 +487,11 @@ static void usb_hub_handle_control(USBDevice *dev, USB= Packet *p, sizeof(qemu_hub_hub_descriptor)); data[2] =3D s->num_ports; =20 + if (s->port_power) { + data[3] &=3D ~0x03; + data[3] |=3D 0x01; + } + /* fill DeviceRemovable bits */ limit =3D DIV_ROUND_UP(s->num_ports + 1, 8) + 7; for (n =3D 7; n < limit; n++) { @@ -536,6 +571,9 @@ static void usb_hub_unrealize(USBDevice *dev, Error **e= rrp) usb_unregister_port(usb_bus_from_device(dev), &s->ports[i].port); } + + timer_del(s->port_timer); + timer_free(s->port_timer); } =20 static USBPortOps usb_hub_port_ops =3D { @@ -565,6 +603,8 @@ static void usb_hub_realize(USBDevice *dev, Error **err= p) =20 usb_desc_create_serial(dev); usb_desc_init(dev); + s->port_timer =3D timer_new_ns(QEMU_CLOCK_VIRTUAL, + usb_hub_port_update_timer, s); s->intr =3D usb_ep_get(dev, USB_TOKEN_IN, 1); for (i =3D 0; i < s->num_ports; i++) { port =3D &s->ports[i]; @@ -587,6 +627,24 @@ static const VMStateDescription vmstate_usb_hub_port = =3D { } }; =20 +static bool usb_hub_port_timer_needed(void *opaque) +{ + USBHubState *s =3D opaque; + + return s->port_power; +} + +static const VMStateDescription vmstate_usb_hub_port_timer =3D { + .name =3D "usb-hub/port-timer", + .version_id =3D 1, + .minimum_version_id =3D 1, + .needed =3D usb_hub_port_timer_needed, + .fields =3D (VMStateField[]) { + VMSTATE_TIMER_PTR(port_timer, USBHubState), + VMSTATE_END_OF_LIST() + }, +}; + static const VMStateDescription vmstate_usb_hub =3D { .name =3D "usb-hub", .version_id =3D 1, @@ -596,11 +654,16 @@ static const VMStateDescription vmstate_usb_hub =3D { VMSTATE_STRUCT_ARRAY(ports, USBHubState, MAX_PORTS, 0, vmstate_usb_hub_port, USBHubPort), VMSTATE_END_OF_LIST() + }, + .subsections =3D (const VMStateDescription * []) { + &vmstate_usb_hub_port_timer, + NULL } }; =20 static Property usb_hub_properties[] =3D { DEFINE_PROP_UINT32("ports", USBHubState, num_ports, 8), + DEFINE_PROP_BOOL("port-power", USBHubState, port_power, false), DEFINE_PROP_END_OF_LIST(), }; =20 --=20 2.18.1