From patchwork Tue Aug 16 08:38:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ray Chi X-Patchwork-Id: 597794 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 21A55C2BB41 for ; Tue, 16 Aug 2022 09:44:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233902AbiHPJod (ORCPT ); Tue, 16 Aug 2022 05:44:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54534 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233909AbiHPJn4 (ORCPT ); Tue, 16 Aug 2022 05:43:56 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B2C5C1FCD3 for ; Tue, 16 Aug 2022 01:39:06 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-333f0d49585so8989507b3.9 for ; Tue, 16 Aug 2022 01:39:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=qW4FpPn+3U6rWBjf7f7ZHOYJOIjzLtnGkLPoxDCEiqo=; b=PcLu0Pjn7hJcarJlBUUfD59TZO6YllkuNLqEkpOvYOBrZiRYizQWXRTIWEsPSKaB/b h05wxNQy+Y/vuxoIQd8q0jesq9TRfz2hDHPWW4l8SmNYJGUQE5pNOY6KNh2sQvafro2x naxUGO/uIcR+Kk6Co3VJCXd7NtA+lvj+SFYxw/KculVDjhcKNxFWCrVITzM/240ghQrc DTpXdF2hEccRuwMu/1ngAGcpLPcsWd1TDlk+UNkoTh4K13o7XUZ/xfELjbRnZR92nnn6 saPDPi1CyZSJYq9Tq8Wt+dnb78SKxmFZqk6K8EvHyU+F5Id12rvafFRDhkQTLjyp96nU msqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=qW4FpPn+3U6rWBjf7f7ZHOYJOIjzLtnGkLPoxDCEiqo=; b=i9B0URB6zqMnqAOLy6d1LXRRna1ymcCxCTAaJiXHWEOW9EHJASQKXLgJLXNKa19BWD 5XRJRfRfdfWgB/IB4LI06OMkLclYWxYLU5MXDKNCU4UAkHhZd3OqHAh4wtXOqcloSMOo m8+MQHbMMSnphL2B0xbXLnJoHeTUB3+giBrf43ifcfcso4wMoP40SxMayzLpOeefX4ci dh77dhFUgWsphbx+UGfMouCe7Z8ZLik+rhK4ln0sW9iR4tI0UuQbqUvmpg1aPIlWxVmd 421WWmhgwc1SPAllqWPu5kxvzxE6sg5HBaq5Dat1EYt2TekQ5jveG4hFLMM34u145+Sg W4Ww== X-Gm-Message-State: ACgBeo0fXe3lDBuV0t2AclO1clq1BvulK0F8ESMY6o+uHyZTRcN+TgBx pLX1/4v9cxNJnZtZMA+SkzGIC1P4mDw= X-Google-Smtp-Source: AA6agR5THf29zvJ3EExhdVRpGCeWm8v3qH+6f6RsLDJEdTyDlC+jve0nJf4SN1Yr9fHCyeLFWEXEilhrMqs= X-Received: from raychi.tao.corp.google.com ([2401:fa00:fc:202:5bfb:2ce6:1e1b:f496]) (user=raychi job=sendgmr) by 2002:a81:8414:0:b0:323:e952:eb8a with SMTP id u20-20020a818414000000b00323e952eb8amr16321867ywf.103.1660639146073; Tue, 16 Aug 2022 01:39:06 -0700 (PDT) Date: Tue, 16 Aug 2022 16:38:53 +0800 In-Reply-To: <20220816083854.1491886-1-raychi@google.com> Message-Id: <20220816083854.1491886-2-raychi@google.com> Mime-Version: 1.0 References: <20220816083854.1491886-1-raychi@google.com> X-Mailer: git-send-email 2.37.1.595.g718a3a8f04-goog Subject: [PATCH 1/2] usb: core: add a hook to check port init status From: Ray Chi To: gregkh@linuxfoundation.org, mathias.nyman@intel.com, stern@rowland.harvard.edu Cc: badhri@google.com, albertccwang@google.com, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, Ray Chi Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org This patch add a hook to check the port init status. Currently, only usbcore knows port init status even if the result is bad. It will cause a USB host keep doing USB enumeration for a long time when the USB host connects to a broken USB accessory. The hc_driver could use the hook to know port init status and do possible error handling according to platform requirements or limitations. Signed-off-by: Ray Chi --- drivers/usb/core/hub.c | 14 ++++++++++++++ include/linux/usb/hcd.h | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c index 2633acde7ac1..6ce6092816cb 100644 --- a/drivers/usb/core/hub.c +++ b/drivers/usb/core/hub.c @@ -4659,6 +4659,16 @@ static int hub_enable_device(struct usb_device *udev) return hcd->driver->enable_device(hcd, udev); } +static int hub_port_check_init_status(struct usb_device *udev, int r) +{ + struct usb_hcd *hcd = bus_to_hcd(udev->bus); + + if (!hcd->driver->check_init_status) + return 0; + + return hcd->driver->check_init_status(hcd, udev, r); +} + /* Reset device, (re)assign address, get device descriptor. * Device connection must be stable, no more debouncing needed. * Returns device in USB_STATE_ADDRESS, except on error. @@ -4855,6 +4865,10 @@ hub_port_init(struct usb_hub *hub, struct usb_device *udev, int port1, buf->bMaxPacketSize0; kfree(buf); + retval = hub_port_check_init_status(udev, r); + if (retval < 0) + goto fail; + retval = hub_port_reset(hub, port1, udev, delay, false); if (retval < 0) /* error or disconnect */ goto fail; diff --git a/include/linux/usb/hcd.h b/include/linux/usb/hcd.h index 67f8713d3fa3..8fa30b4a6b7d 100644 --- a/include/linux/usb/hcd.h +++ b/include/linux/usb/hcd.h @@ -297,6 +297,14 @@ struct hc_driver { gfp_t mem_flags); void (*unmap_urb_for_dma)(struct usb_hcd *hcd, struct urb *urb); + /* + * (optional) HCD could get the information of GET_DESCRIPTOR by this hook. + * In general, it is not necessary unless the enumeration takes long + * time to do. The host controller could know the enumeration status by + * this hook and do some error handlings. + */ + int (*check_init_status)(struct usb_hcd *hcd, struct usb_device *udev, int r); + /* hw synch, freeing endpoint resources that urb_dequeue can't */ void (*endpoint_disable)(struct usb_hcd *hcd, struct usb_host_endpoint *ep); From patchwork Tue Aug 16 08:38:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ray Chi X-Patchwork-Id: 597792 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DC994C3F6B0 for ; Tue, 16 Aug 2022 10:33:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234767AbiHPKdt (ORCPT ); Tue, 16 Aug 2022 06:33:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46084 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234456AbiHPKdX (ORCPT ); Tue, 16 Aug 2022 06:33:23 -0400 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B38B33419 for ; Tue, 16 Aug 2022 01:39:13 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id z32-20020a631920000000b0042903cb8dfcso1983134pgl.5 for ; Tue, 16 Aug 2022 01:39:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=bB93z6epEEktgqWPKqCS2yAkD9gaQQnHEzx7X0wo89k=; b=LBUekVZbKwjYB6iREXgnr6/dM7aUHkeeUVgIKavekxJRHMpaR5GmbH36Wo7f35PhMr qFed3KzKfZEhK6wbXZCuUJg0wj88/OsxPnGsu7Mx+2hcwgoKib/YEaqtlEdOJRIXaQ6N ujLmjUERqv4Xqmb0I3ospKzIO6NkBRgvK1ufjI/hB9DWc3yFWOKtGv1lGx7R2QFL7xmT hwx9srMqjWHG4F+T+cXDyL6WffHLHty/vt9nTIa/c7DQuVGJsXphgdSSK5ebrBEIB9Ve FCnGXHkRgYzC8TNkBtKXABrWNZYQE69QH2JUsIBGNuav6Ow3IKnI7JLSCIPZSgmNi9e5 FtQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=bB93z6epEEktgqWPKqCS2yAkD9gaQQnHEzx7X0wo89k=; b=CISxbLn9bixvHL2Bwf0EzHibd/CjL2aYsG/awkROQpfAfKStH8HtTo0ocueP5mRqgu 3ODg14r4W/T31pwFVIGQFeL8j/WLOOUxLJdLHo9QXv+cuIou52RmAVbwGRl+LDQVTklm cNyvwNX8P5t5hTUOW3Rgn3+n6j5EChWGvWqthB8/llujC+zlPagtvhtNRFZR64QD+XPu 9QAndbDv6yyBXRPqS6nwX2k2hbWruQMYODx/dtxQ5cX4+qDWc19ZNKosV21zkoF069wJ 7NtHQnJ/ZJpj9ajBwayEohe+wJcTvORLb0vIGmDV680ECzn2pFAb3dWaO5Cn3iLJzRgK CMKQ== X-Gm-Message-State: ACgBeo0X/dA5Kgmq/+lVv/L251xV6CownJ3cN+r/jzCuPWn8qKh20kq+ InLxDXAo3n8p9S4qtEDIl5GWG60xWl0= X-Google-Smtp-Source: AA6agR54uF8KKRbV8DX/kPh89GDgFvO2Xmrnjc3ibq1RSfDiZt18v2MUXc+jMD8Q35DJXDMBoxLKFfJ0VuM= X-Received: from raychi.tao.corp.google.com ([2401:fa00:fc:202:5bfb:2ce6:1e1b:f496]) (user=raychi job=sendgmr) by 2002:a05:6a00:10ca:b0:4f7:5af4:47b6 with SMTP id d10-20020a056a0010ca00b004f75af447b6mr20152772pfu.6.1660639152696; Tue, 16 Aug 2022 01:39:12 -0700 (PDT) Date: Tue, 16 Aug 2022 16:38:54 +0800 In-Reply-To: <20220816083854.1491886-1-raychi@google.com> Message-Id: <20220816083854.1491886-3-raychi@google.com> Mime-Version: 1.0 References: <20220816083854.1491886-1-raychi@google.com> X-Mailer: git-send-email 2.37.1.595.g718a3a8f04-goog Subject: [PATCH 2/2] usb: xhci: add check_init_status hook support From: Ray Chi To: gregkh@linuxfoundation.org, mathias.nyman@intel.com, stern@rowland.harvard.edu Cc: badhri@google.com, albertccwang@google.com, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, Ray Chi Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org In general, xHCI didn't do anything for port initialization. However, there are some requirement or limitation on various platforms, so vendors need to do some error handlings if the device connected to a broken USB accessory. This patch also add the hook to xhci_driver_overrides so that vendors can add their specific protection easily if needed. Signed-off-by: Ray Chi --- drivers/usb/host/xhci.c | 17 +++++++++++++++++ drivers/usb/host/xhci.h | 1 + 2 files changed, 18 insertions(+) diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c index 65858f607437..f237af9d6e2e 100644 --- a/drivers/usb/host/xhci.c +++ b/drivers/usb/host/xhci.c @@ -4358,6 +4358,20 @@ static int xhci_enable_device(struct usb_hcd *hcd, struct usb_device *udev) return xhci_setup_device(hcd, udev, SETUP_CONTEXT_ONLY); } +/* + * The function could get the status of port initialization. + */ +static int xhci_check_init_status(struct usb_hcd *hcd, struct usb_device *udev, int r) +{ + /* + * In general, this function is not necessory. Some platforms may + * need doing error handling when the port initialization takes a + * long time to do. The device can use the override callback to + * do specific handlings. + */ + return 0; +} + /* * Transfer the port index into real index in the HW port status * registers. Caculate offset between the port's PORTSC register @@ -5455,6 +5469,7 @@ static const struct hc_driver xhci_hc_driver = { .enable_device = xhci_enable_device, .update_hub_device = xhci_update_hub_device, .reset_device = xhci_discover_or_reset_device, + .check_init_status = xhci_check_init_status, /* * scheduling support @@ -5503,6 +5518,8 @@ void xhci_init_driver(struct hc_driver *drv, drv->check_bandwidth = over->check_bandwidth; if (over->reset_bandwidth) drv->reset_bandwidth = over->reset_bandwidth; + if (over->check_init_status) + drv->check_init_status = over->check_init_status; } } EXPORT_SYMBOL_GPL(xhci_init_driver); diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h index 1960b47acfb2..33ce873236e9 100644 --- a/drivers/usb/host/xhci.h +++ b/drivers/usb/host/xhci.h @@ -1944,6 +1944,7 @@ struct xhci_driver_overrides { struct usb_host_endpoint *ep); int (*check_bandwidth)(struct usb_hcd *, struct usb_device *); void (*reset_bandwidth)(struct usb_hcd *, struct usb_device *); + int (*check_init_status)(struct usb_hcd *hcd, struct usb_device *udev, int r); }; #define XHCI_CFC_DELAY 10