From patchwork Wed Jun 22 11:48:45 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerd Hoffmann X-Patchwork-Id: 9192563 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 3C288601C0 for ; Wed, 22 Jun 2016 11:59:00 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2B1A227F9C for ; Wed, 22 Jun 2016 11:59:00 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1FBE7283F1; Wed, 22 Jun 2016 11:59:00 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id BECB027F9C for ; Wed, 22 Jun 2016 11:58:59 +0000 (UTC) Received: from localhost ([::1]:57470 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bFgo6-0005Vw-DH for patchwork-qemu-devel@patchwork.kernel.org; Wed, 22 Jun 2016 07:58:58 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40323) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bFgeT-0003Lo-VD for qemu-devel@nongnu.org; Wed, 22 Jun 2016 07:49:08 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bFgeL-0001wS-En for qemu-devel@nongnu.org; Wed, 22 Jun 2016 07:49:00 -0400 Received: from mx1.redhat.com ([209.132.183.28]:53766) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bFgeL-0001vZ-8Z for qemu-devel@nongnu.org; Wed, 22 Jun 2016 07:48:53 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5C4477F6A8 for ; Wed, 22 Jun 2016 11:48:51 +0000 (UTC) Received: from nilsson.home.kraxel.org (ovpn-116-26.ams2.redhat.com [10.36.116.26]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u5MBmo4B029196; Wed, 22 Jun 2016 07:48:50 -0400 Received: by nilsson.home.kraxel.org (Postfix, from userid 500) id B110E827AE; Wed, 22 Jun 2016 13:48:49 +0200 (CEST) From: Gerd Hoffmann To: qemu-devel@nongnu.org Date: Wed, 22 Jun 2016 13:48:45 +0200 Message-Id: <1466596127-3330-4-git-send-email-kraxel@redhat.com> In-Reply-To: <1466596127-3330-1-git-send-email-kraxel@redhat.com> References: <1466596127-3330-1-git-send-email-kraxel@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Wed, 22 Jun 2016 11:48:51 +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 3/5] usb: Add QOM property "attached". 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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP USB devices in attached state are visible to the guest. This patch adds a QOM property for this. Write access is opt-in per device. Some devices manage attached state automatically (usb-host, usb-serial, usb-redir), so we can't enable write access universally but have to do it on a case by case base. So far, no device opts in. Signed-off-by: Gerd Hoffmann Reviewed-by: Markus Armbruster Message-id: 1465984019-28963-4-git-send-email-kraxel@redhat.com [ minor codestyle fix ] Signed-off-by: Gerd Hoffmann --- hw/usb/bus.c | 43 +++++++++++++++++++++++++++++++++++++++++++ include/hw/usb.h | 1 + 2 files changed, 44 insertions(+) diff --git a/hw/usb/bus.c b/hw/usb/bus.c index afd70ea..c28ccb8 100644 --- a/hw/usb/bus.c +++ b/hw/usb/bus.c @@ -736,6 +736,48 @@ USBDevice *usbdevice_create(const char *cmdline) return dev; } +static bool usb_get_attached(Object *obj, Error **errp) +{ + USBDevice *dev = USB_DEVICE(obj); + + return dev->attached; +} + +static void usb_set_attached(Object *obj, bool value, Error **errp) +{ + USBDevice *dev = USB_DEVICE(obj); + Error *err = NULL; + + if (dev->attached == value) { + return; + } + + if (value) { + usb_device_attach(dev, &err); + if (err) { + error_propagate(errp, err); + } + } else { + usb_device_detach(dev); + } +} + +static void usb_device_instance_init(Object *obj) +{ + USBDevice *dev = USB_DEVICE(obj); + USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); + + if (klass->attached_settable) { + object_property_add_bool(obj, "attached", + usb_get_attached, usb_set_attached, + NULL); + } else { + object_property_add_bool(obj, "attached", + usb_get_attached, NULL, + NULL); + } +} + static void usb_device_class_init(ObjectClass *klass, void *data) { DeviceClass *k = DEVICE_CLASS(klass); @@ -749,6 +791,7 @@ static const TypeInfo usb_device_type_info = { .name = TYPE_USB_DEVICE, .parent = TYPE_DEVICE, .instance_size = sizeof(USBDevice), + .instance_init = usb_device_instance_init, .abstract = true, .class_size = sizeof(USBDeviceClass), .class_init = usb_device_class_init, diff --git a/include/hw/usb.h b/include/hw/usb.h index 8f39470..847c9de 100644 --- a/include/hw/usb.h +++ b/include/hw/usb.h @@ -347,6 +347,7 @@ typedef struct USBDeviceClass { const char *product_desc; const USBDesc *usb_desc; + bool attached_settable; } USBDeviceClass; typedef struct USBPortOps {