From nobody Mon Feb 9 09:15:38 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=1558681675; cv=none; d=zoho.com; s=zohoarc; b=DaAOrLgC1C1i9plGSbxzST0e0lIaf8pU3XS7fVytpM+34Crw/HhYlnnjcd2QGyRvnjZKoKH4IDY2OHHB1t8wvWk8c8fg/b1Rtc3xCgGxYxvXs1+K04WkjBfl9wZo0DLud6smQqBu0LFZz/HjT+eiVvXalC/vljRtfPQn7vpWx5k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1558681675; 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=sgJALrKenlZetA1EGfa7rYqUP1+N3gOmIDaLioMzK7I=; b=EkYaNNAkH8ibFHzqE0GvmAUE4nsMr89ZHNlYUr8SwiADTjnxTynfLxAuZP34bBs9tWAr7A/1mD4OhE+Xs2u/d9HXvEMszxSyL4sy84wW2kem/2FXpuo/1nWgFnzmmQplgLoLVVNfFQGN3Nbg+CHdCAxiOS2Owj6uc7OT4h1Cqys= 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 1558681675344181.03712642222354; Fri, 24 May 2019 00:07:55 -0700 (PDT) Received: from localhost ([127.0.0.1]:50139 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hU4Iu-0000Wa-9G for importer@patchew.org; Fri, 24 May 2019 03:07:48 -0400 Received: from eggs.gnu.org ([209.51.188.92]:45850) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hU4Ea-0005PV-ST for qemu-devel@nongnu.org; Fri, 24 May 2019 03:03:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hU4EY-0006LM-TV for qemu-devel@nongnu.org; Fri, 24 May 2019 03:03:20 -0400 Received: from mx1.redhat.com ([209.132.183.28]:33670) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hU4EW-0006GO-Vy for qemu-devel@nongnu.org; Fri, 24 May 2019 03:03:18 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 56238368E3 for ; Fri, 24 May 2019 07:03:14 +0000 (UTC) Received: from sirius.home.kraxel.org (ovpn-116-101.ams2.redhat.com [10.36.116.101]) by smtp.corp.redhat.com (Postfix) with ESMTP id DFE725B689; Fri, 24 May 2019 07:03:12 +0000 (UTC) Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id E43911753C; Fri, 24 May 2019 09:03:10 +0200 (CEST) From: Gerd Hoffmann To: qemu-devel@nongnu.org Date: Fri, 24 May 2019 09:03:10 +0200 Message-Id: <20190524070310.4952-6-kraxel@redhat.com> In-Reply-To: <20190524070310.4952-1-kraxel@redhat.com> References: <20190524070310.4952-1-kraxel@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Fri, 24 May 2019 07:03:14 +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] [PATCH v2 5/5] 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: 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 --- 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