From patchwork Thu Aug 25 15:06:55 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerd Hoffmann X-Patchwork-Id: 111593 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 4E3E4B6F8D for ; Fri, 26 Aug 2011 01:27:00 +1000 (EST) Received: from localhost ([::1]:50247 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QwbWz-0006dB-Qu for incoming@patchwork.ozlabs.org; Thu, 25 Aug 2011 11:07:45 -0400 Received: from eggs.gnu.org ([140.186.70.92]:37017) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QwbWO-0004Pg-CC for qemu-devel@nongnu.org; Thu, 25 Aug 2011 11:07:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QwbWM-0002PM-5Y for qemu-devel@nongnu.org; Thu, 25 Aug 2011 11:07:08 -0400 Received: from mx1.redhat.com ([209.132.183.28]:59053) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QwbWL-0002P6-UY for qemu-devel@nongnu.org; Thu, 25 Aug 2011 11:07:06 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p7PF75H9015236 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 25 Aug 2011 11:07:05 -0400 Received: from rincewind.home.kraxel.org (ovpn-116-22.ams2.redhat.com [10.36.116.22]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p7PF73Q1006499; Thu, 25 Aug 2011 11:07:03 -0400 Received: by rincewind.home.kraxel.org (Postfix, from userid 500) id 6379C40A95; Thu, 25 Aug 2011 17:06:58 +0200 (CEST) From: Gerd Hoffmann To: qemu-devel@nongnu.org Date: Thu, 25 Aug 2011 17:06:55 +0200 Message-Id: <1314284817-9034-6-git-send-email-kraxel@redhat.com> In-Reply-To: <1314284817-9034-1-git-send-email-kraxel@redhat.com> References: <1314284817-9034-1-git-send-email-kraxel@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 Cc: Gerd Hoffmann Subject: [Qemu-devel] [PATCH 5/7] usb-host: fix configuration tracking. 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 It is perfectly fine to leave the usb device in unconfigured state (USBHostDevice->configuration == 0). Just do that and wait for the guest to explicitly set a configuration. This is closer to what real hardware does and it also simplifies the device initialization. There is no need to figure how the device is configured on the host. Signed-off-by: Gerd Hoffmann --- usb-linux.c | 80 +++++++++++++--------------------------------------------- 1 files changed, 18 insertions(+), 62 deletions(-) diff --git a/usb-linux.c b/usb-linux.c index b939cfc..3b0f53f 100644 --- a/usb-linux.c +++ b/usb-linux.c @@ -407,8 +407,11 @@ static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration) int interface, nb_interfaces; int ret, i; - if (configuration == 0) /* address state - ignore */ + if (configuration == 0) { /* address state - ignore */ + dev->ninterfaces = 0; + dev->configuration = 0; return 1; + } DPRINTF("husb: claiming interfaces. config %d\n", configuration); @@ -433,7 +436,7 @@ static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration) DPRINTF("husb: config #%d need %d\n", dev->descr[i + 5], configuration); - if (configuration < 0 || configuration == dev->descr[i + 5]) { + if (configuration == dev->descr[i + 5]) { configuration = dev->descr[i + 5]; break; } @@ -835,6 +838,7 @@ static int usb_host_set_config(USBHostDevice *s, int config) return ctrl_error(); } usb_host_claim_interfaces(s, config); + usb_linux_update_endp_table(s); return 0; } @@ -941,51 +945,6 @@ static int usb_host_handle_control(USBDevice *dev, USBPacket *p, return USB_RET_ASYNC; } -static int usb_linux_get_configuration(USBHostDevice *s) -{ - uint8_t configuration; - struct usb_ctrltransfer ct; - int ret; - - if (usb_fs_type == USB_FS_SYS) { - char device_name[32], line[1024]; - int configuration; - - sprintf(device_name, "%d-%s", s->bus_num, s->port); - - if (!usb_host_read_file(line, sizeof(line), "bConfigurationValue", - device_name)) { - goto usbdevfs; - } - if (sscanf(line, "%d", &configuration) != 1) { - goto usbdevfs; - } - return configuration; - } - -usbdevfs: - ct.bRequestType = USB_DIR_IN; - ct.bRequest = USB_REQ_GET_CONFIGURATION; - ct.wValue = 0; - ct.wIndex = 0; - ct.wLength = 1; - ct.data = &configuration; - ct.timeout = 50; - - ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct); - if (ret < 0) { - perror("usb_linux_get_configuration"); - return -1; - } - - /* in address state */ - if (configuration == 0) { - return -1; - } - - return configuration; -} - static uint8_t usb_linux_get_alt_setting(USBHostDevice *s, uint8_t configuration, uint8_t interface) { @@ -1031,16 +990,16 @@ usbdevfs: static int usb_linux_update_endp_table(USBHostDevice *s) { uint8_t *descriptors; - uint8_t devep, type, configuration, alt_interface; + uint8_t devep, type, alt_interface; int interface, length, i; for (i = 0; i < MAX_ENDPOINTS; i++) s->endp_table[i].type = INVALID_EP_TYPE; - i = usb_linux_get_configuration(s); - if (i < 0) - return 1; - configuration = i; + if (s->configuration == 0) { + /* not configured yet -- leave all endpoints disabled */ + return 0; + } /* get the desired configuration, interface, and endpoint descriptors * from device description */ @@ -1049,8 +1008,9 @@ static int usb_linux_update_endp_table(USBHostDevice *s) i = 0; if (descriptors[i + 1] != USB_DT_CONFIG || - descriptors[i + 5] != configuration) { - DPRINTF("invalid descriptor data - configuration\n"); + descriptors[i + 5] != s->configuration) { + fprintf(stderr, "invalid descriptor data - configuration %d\n", + s->configuration); return 1; } i += descriptors[i]; @@ -1064,7 +1024,8 @@ static int usb_linux_update_endp_table(USBHostDevice *s) } interface = descriptors[i + 2]; - alt_interface = usb_linux_get_alt_setting(s, configuration, interface); + alt_interface = usb_linux_get_alt_setting(s, s->configuration, + interface); /* the current interface descriptor is the active interface * and has endpoints */ @@ -1204,13 +1165,8 @@ static int usb_host_open(USBHostDevice *dev, int bus_num, #endif - /* - * Initial configuration is -1 which makes us claim first - * available config. We used to start with 1, which does not - * always work. I've seen devices where first config starts - * with 2. - */ - if (!usb_host_claim_interfaces(dev, -1)) { + /* start unconfigured -- we'll wait for the guest to set a configuration */ + if (!usb_host_claim_interfaces(dev, 0)) { goto fail; }