From nobody Sun Oct 5 18:17:26 2025 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ABB4B1A314F for ; Fri, 1 Aug 2025 03:40:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754019659; cv=none; b=o+v7tKVgvauSpYYBexDao7px3s7rmZpFUmFTnhBO7rKibwC6qX0WKOKESmBXClHrpNNxl+di3EoN8t/zQ/+CCzizu2NFLV3/REiKPlitaL4wCimZgPiZazJsTwsqnImLiPUKNfKogi65RJ9VCz5zNno+egJYsu0UffxZWsVhFyI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754019659; c=relaxed/simple; bh=teS/+aZOK08uAjWXkV1PKRDhG6i0pRZR3Pq9zRsGilU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XhwYbYJpoRUoY48quZk+7H7Q1MOcYG6kuUYvrJt/8jmEP8FkUgl4+ePFwDJGAYnFGRFBeWubPYrAQYex0pRo28iiqbRiZAPEV+e78ruAvwHP6DnSzLU8jZtIz7+nHi5O72CTKoEjZFjzu+ewdloI/GsbPjTRWwIRFzinSzdw0Yg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--guanyulin.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=UZZupN0A; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--guanyulin.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="UZZupN0A" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-75e28bcec3bso877081b3a.1 for ; Thu, 31 Jul 2025 20:40:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1754019657; x=1754624457; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=fEKyx7rEaL9ClT6i1Uqn92+E8d8b0N4HGm0M5DzW4vU=; b=UZZupN0AlQXxonEBsYn+QwUh76oQBDGb+k/ZaHuplGeVdSEFVDhYlfs+u2pkR4kNjX Zj9cCgr+rrUsT4gpqLRiNOt82q4xjtQdHEl+2rWUmJymKaDiXP/BUMmjtAt5MiLphmW/ TKORzZobJ1kQByrYbi3h7YETHzw/3R8GuzGmM/+du+wjQ2YZMWH+Gs1FcH1LtLDzro4k 80okH9mD9M6RAK/Rnd/CKSWhkO31UAQfqhM6Sp40/JVoR4P5kFLp211cIQ2rk6BkP+DZ Khd3AYSQ3gOIdsZ1AtSZQUB03qmcPB9Am6TuTm0iQjSLSij0leZqP/VnjhTjxeUSpgg/ RRtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754019657; x=1754624457; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=fEKyx7rEaL9ClT6i1Uqn92+E8d8b0N4HGm0M5DzW4vU=; b=OS0ija6C9qkFLCH9eg0a0ezKx7WTOdBv6jPhZK6Zjl/rhUeSqkpFCIrbCQycYBp7mL md/fkFf1cY7kd8IZdBFe/ZNdxyPegqjFekNDD5ceC54CIlBSg64af3/upt4jIlpr7OEZ EBG8oQoA9lso/Y6OPZ+tn86vhDdvQh414qzRF+YEpTvWjJqZscW9h1MPS6X79ig3Gw1F 2Hx1BtzXOiJY84PO8MNkjFgfTNg2Y9dgenDbppLlslvLikR6QIgX+GBAxrIcK8uneyeP rje9ngkhzncoY5Xq16N8xzeXeBn++iSutRa2SQVUcYhzsb6Ue3o7I06DMmuF9YRelScI L/sw== X-Forwarded-Encrypted: i=1; AJvYcCU4POCYlfSjUyVMV12xbY0uI/ZhOe13E8oEpHtFCJ0b+QOOMwDaEqLsbeg6ccKDiM3cvWoaI0V5YGx2SO8=@vger.kernel.org X-Gm-Message-State: AOJu0Yy3CoZXlTniPgPUNNjBKfaQq/xqoxDWvWQdtba4ouGumdz3v1nO QfyPRi0dEMUvxaMibVMJqUTgPqu3mLaN9uEkWRfkyFTkAXugtrSlQQgGJsmdlSLGGH7agr3PbrO QtDKDylgy81aVIJLF5A== X-Google-Smtp-Source: AGHT+IEUckZdgPpJBQcMD5oGM/FPiKa0Egdq8QTlH3cbIYwPC/r7pHST0Q+N93A0qNgQM2flPniCR9h/FZJnJRk= X-Received: from pfck16-n1.prod.google.com ([2002:a05:6a00:a050:10b0:748:fa5b:4163]) (user=guanyulin job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:a95:b0:748:ed51:1305 with SMTP id d2e1a72fcca58-76ab0a23c9amr14806735b3a.5.1754019657171; Thu, 31 Jul 2025 20:40:57 -0700 (PDT) Date: Fri, 1 Aug 2025 03:39:33 +0000 In-Reply-To: <20250801034004.3314737-1-guanyulin@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250801034004.3314737-1-guanyulin@google.com> X-Mailer: git-send-email 2.50.1.565.gc32cd1483b-goog Message-ID: <20250801034004.3314737-5-guanyulin@google.com> Subject: [PATCH v15 4/4] usb: host: enable USB offload during system sleep From: Guan-Yu Lin To: gregkh@linuxfoundation.org, mathias.nyman@intel.com, hannelotta@gmail.com, zijun.hu@oss.qualcomm.com, xu.yang_2@nxp.com, stern@rowland.harvard.edu, andriy.shevchenko@linux.intel.com, ben@decadent.org.uk, quic_wcheng@quicinc.com, krzysztof.kozlowski@linaro.org, dh10.jung@samsung.com Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, Guan-Yu Lin Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Sharing a USB controller with another entity via xhci-sideband driver creates power management complexities. To prevent the USB controller from being inadvertently deactivated while in use by the other entity, a usage-count based mechanism is implemented. This allows the system to manage power effectively, ensuring the controller remains available whenever needed. In order to maintain full functionality of an offloaded USB devices, several changes are made within the suspend flow of such devices: - skip usb_suspend_device() so that the port/hub are still active for USB transfers via offloaded path. - not suspending the endpoints which are used by USB interfaces marked with needs_remote_wakeup. Namely, skip usb_suspend_interface() and usb_hcd_flush_endpoint() on associated USB interfaces. This reserves a pending interrupt urb during system suspend for handling the interrupt transfer, which is necessary since remote wakeup doesn't apply in the offloaded USB devices when controller is still active. - not flushing the endpoints of actively offloaded USB devices. Given that the USB devices is used by another entity, unilaterally flush the endpoint might lead to unexpected behavior on another entity. - not suspending the xhci controller. This is done by skipping the suspend/resume callbacks in the xhci platform driver. Signed-off-by: Guan-Yu Lin --- drivers/usb/core/driver.c | 50 +++++++++++++++++++++++++++++++----- drivers/usb/host/xhci-plat.c | 19 ++++++++++++++ drivers/usb/host/xhci-plat.h | 1 + 3 files changed, 64 insertions(+), 6 deletions(-) diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c index c3177034b779..275c0fd573d4 100644 --- a/drivers/usb/core/driver.c +++ b/drivers/usb/core/driver.c @@ -1420,11 +1420,28 @@ static int usb_suspend_both(struct usb_device *udev= , pm_message_t msg) udev->state =3D=3D USB_STATE_SUSPENDED) goto done; =20 + if (msg.event =3D=3D PM_EVENT_SUSPEND && usb_offload_check(udev)) { + dev_dbg(&udev->dev, "device offloaded, skip suspend.\n"); + udev->offload_at_suspend =3D 1; + } + /* Suspend all the interfaces and then udev itself */ if (udev->actconfig) { n =3D udev->actconfig->desc.bNumInterfaces; for (i =3D n - 1; i >=3D 0; --i) { intf =3D udev->actconfig->interface[i]; + /* + * Don't suspend interfaces with remote wakeup while + * the controller is active. This preserves pending + * interrupt urbs, allowing interrupt events to be + * handled during system suspend. + */ + if (udev->offload_at_suspend && + intf->needs_remote_wakeup) { + dev_dbg(&intf->dev, + "device offloaded, skip suspend.\n"); + continue; + } status =3D usb_suspend_interface(udev, intf, msg); =20 /* Ignore errors during system sleep transitions */ @@ -1435,7 +1452,8 @@ static int usb_suspend_both(struct usb_device *udev, = pm_message_t msg) } } if (status =3D=3D 0) { - status =3D usb_suspend_device(udev, msg); + if (!udev->offload_at_suspend) + status =3D usb_suspend_device(udev, msg); =20 /* * Ignore errors from non-root-hub devices during @@ -1480,9 +1498,11 @@ static int usb_suspend_both(struct usb_device *udev,= pm_message_t msg) */ } else { udev->can_submit =3D 0; - for (i =3D 0; i < 16; ++i) { - usb_hcd_flush_endpoint(udev, udev->ep_out[i]); - usb_hcd_flush_endpoint(udev, udev->ep_in[i]); + if (!udev->offload_at_suspend) { + for (i =3D 0; i < 16; ++i) { + usb_hcd_flush_endpoint(udev, udev->ep_out[i]); + usb_hcd_flush_endpoint(udev, udev->ep_in[i]); + } } } =20 @@ -1524,17 +1544,35 @@ static int usb_resume_both(struct usb_device *udev,= pm_message_t msg) udev->can_submit =3D 1; =20 /* Resume the device */ - if (udev->state =3D=3D USB_STATE_SUSPENDED || udev->reset_resume) - status =3D usb_resume_device(udev, msg); + if (udev->state =3D=3D USB_STATE_SUSPENDED || udev->reset_resume) { + if (!udev->offload_at_suspend) + status =3D usb_resume_device(udev, msg); + else + dev_dbg(&udev->dev, + "device offloaded, skip resume.\n"); + } =20 /* Resume the interfaces */ if (status =3D=3D 0 && udev->actconfig) { for (i =3D 0; i < udev->actconfig->desc.bNumInterfaces; i++) { intf =3D udev->actconfig->interface[i]; + /* + * Interfaces with remote wakeup aren't suspended + * while the controller is active. This preserves + * pending interrupt urbs, allowing interrupt events + * to be handled during system suspend. + */ + if (udev->offload_at_suspend && + intf->needs_remote_wakeup) { + dev_dbg(&intf->dev, + "device offloaded, skip resume.\n"); + continue; + } usb_resume_interface(udev, intf, msg, udev->reset_resume); } } + udev->offload_at_suspend =3D 0; usb_mark_last_busy(udev); =20 done: diff --git a/drivers/usb/host/xhci-plat.c b/drivers/usb/host/xhci-plat.c index df2e942ad5f7..3a56d8f94519 100644 --- a/drivers/usb/host/xhci-plat.c +++ b/drivers/usb/host/xhci-plat.c @@ -20,6 +20,7 @@ #include #include #include +#include =20 #include "xhci.h" #include "xhci-plat.h" @@ -484,6 +485,15 @@ static int xhci_plat_suspend_common(struct device *dev) =20 static int xhci_plat_suspend(struct device *dev) { + struct usb_hcd *hcd =3D dev_get_drvdata(dev); + struct xhci_plat_priv *priv =3D hcd_to_xhci_priv(hcd); + + if (xhci_sideband_check(hcd)) { + priv->sideband_at_suspend =3D 1; + dev_dbg(dev, "sideband instance active, skip suspend.\n"); + return 0; + } + return xhci_plat_suspend_common(dev); } =20 @@ -536,6 +546,15 @@ static int xhci_plat_resume_common(struct device *dev,= bool power_lost) =20 static int xhci_plat_resume(struct device *dev) { + struct usb_hcd *hcd =3D dev_get_drvdata(dev); + struct xhci_plat_priv *priv =3D hcd_to_xhci_priv(hcd); + + if (priv->sideband_at_suspend) { + priv->sideband_at_suspend =3D 0; + dev_dbg(dev, "sideband instance active, skip resume.\n"); + return 0; + } + return xhci_plat_resume_common(dev, false); } =20 diff --git a/drivers/usb/host/xhci-plat.h b/drivers/usb/host/xhci-plat.h index fe4f95e690fa..2b32a93d2b76 100644 --- a/drivers/usb/host/xhci-plat.h +++ b/drivers/usb/host/xhci-plat.h @@ -16,6 +16,7 @@ struct xhci_plat_priv { const char *firmware_name; unsigned long long quirks; bool power_lost; + unsigned sideband_at_suspend:1; void (*plat_start)(struct usb_hcd *); int (*init_quirk)(struct usb_hcd *); int (*suspend_quirk)(struct usb_hcd *); --=20 2.50.1.565.gc32cd1483b-goog