From patchwork Tue Jan 26 10:41:23 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerd Hoffmann X-Patchwork-Id: 8120111 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id C3B999F818 for ; Tue, 26 Jan 2016 10:44:16 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 2E7152025B for ; Tue, 26 Jan 2016 10:44:16 +0000 (UTC) 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.kernel.org (Postfix) with ESMTPS id 73A7920256 for ; Tue, 26 Jan 2016 10:44:15 +0000 (UTC) Received: from localhost ([::1]:42958 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aO16c-0006rX-R0 for patchwork-qemu-devel@patchwork.kernel.org; Tue, 26 Jan 2016 05:44:14 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41373) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aO146-0001hF-Ga for qemu-devel@nongnu.org; Tue, 26 Jan 2016 05:41:39 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aO144-0007mb-GW for qemu-devel@nongnu.org; Tue, 26 Jan 2016 05:41:38 -0500 Received: from mx1.redhat.com ([209.132.183.28]:33840) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aO144-0007mG-BY for qemu-devel@nongnu.org; Tue, 26 Jan 2016 05:41:36 -0500 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (Postfix) with ESMTPS id 9C785C0AC90E for ; Tue, 26 Jan 2016 10:41:35 +0000 (UTC) Received: from nilsson.home.kraxel.org (ovpn-116-40.ams2.redhat.com [10.36.116.40]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u0QAfYPl022571; Tue, 26 Jan 2016 05:41:35 -0500 Received: by nilsson.home.kraxel.org (Postfix, from userid 500) id 1C2BE80C52; Tue, 26 Jan 2016 11:41:34 +0100 (CET) From: Gerd Hoffmann To: qemu-devel@nongnu.org Date: Tue, 26 Jan 2016 11:41:23 +0100 Message-Id: <1453804885-15544-3-git-send-email-kraxel@redhat.com> In-Reply-To: <1453804885-15544-1-git-send-email-kraxel@redhat.com> References: <1453804885-15544-1-git-send-email-kraxel@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Markus Armbruster , Gerd Hoffmann Subject: [Qemu-devel] [PATCH 2/4] usb: add attached property 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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org 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), so we can't enable write access universally but have to do it on a case by case base. Signed-off-by: Gerd Hoffmann Reviewed-by: Markus Armbruster --- hw/usb/bus.c | 42 ++++++++++++++++++++++++++++++++++++++++++ include/hw/usb.h | 1 + 2 files changed, 43 insertions(+) diff --git a/hw/usb/bus.c b/hw/usb/bus.c index dd28041..17e0479 100644 --- a/hw/usb/bus.c +++ b/hw/usb/bus.c @@ -733,6 +733,47 @@ 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); @@ -746,6 +787,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 f8432f9..6eaa19c 100644 --- a/include/hw/usb.h +++ b/include/hw/usb.h @@ -346,6 +346,7 @@ typedef struct USBDeviceClass { const char *product_desc; const USBDesc *usb_desc; + bool attached_settable; } USBDeviceClass; typedef struct USBPortOps {