From nobody Fri Sep 12 04:48:24 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 D68DE326D67 for ; Thu, 11 Sep 2025 14:21:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757600485; cv=none; b=PJybyg/JHzwl3nfnrS+eLH2dUKDqs/fKzw0dZ1h8KQsDemXCZUhL8i1Usto/GXED+2BUxmlUdzwd4MlPKJ903oRo7O9p0HpwJIRLiYNBzrEekW9DO2my7E5LJgg15hPRVf0hNy31d5vyLV/2TTOYAb4kEB0SWla+Tc0vu+zJ1LQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757600485; c=relaxed/simple; bh=+J7/cO9gXG5kRzc2nEtM3dWl5SfXmoHOiY9WvXMCTBA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=U3ySPcJmMvDVRNd1wtWbnmY/uh2HyIfTMfRmpFM4Y8T4Krzwmy+VGkK1cD5rkSIQEbFHdE7vZSrNz/yidlFaYeJFLijXZJCmpmsWoBZfyU6sM8W9tI4xKdcPFHJ7cg4IXeYw80xxFBnKc9zFcEIM1dVGiFeOds30ZYT0mHSy8MM= 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=syaqVM/z; arc=none smtp.client-ip=209.85.216.74 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="syaqVM/z" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-32b57b0aa25so851991a91.0 for ; Thu, 11 Sep 2025 07:21:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1757600483; x=1758205283; 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=dwcx5geWuvQ/zEMPdZ6AIVOaVOt2ShiW7ysN/KvEWSI=; b=syaqVM/znuoQDiRgGLWwigCmlilDjdRqE1wenLD+hH/LGOKmPiSoF/WefLrq9v0k2q S6rkZLdcpUGnNHsVTHpADLN7xrxxk9AxFvi6thNTzSCdhim6pLRY2DPnfiwANWtb2yCN 3jhyxiNEhjg7N6oEiAErzhJqU74uUa/ZIqdC/F0cvB5koufp1/aova04onloXvJnZO6z WRjOD1YGxS5tczXj8H4y7hWQKv5O4LrTXSCsJnct5T5NwBrCipNKdU1nZHwFeXG3fdN6 cCxHHFwqloztjCH8eRXVlQ1ehOn9uQ1c2TQu3pOy9oCr0YP3bHdd+zg7PcmQiIjTIieW oIZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1757600483; x=1758205283; 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=dwcx5geWuvQ/zEMPdZ6AIVOaVOt2ShiW7ysN/KvEWSI=; b=MN5AJlda56BYDwby/qiZ1IFAnPVKa5NvSPcas5nYhHip+8MyIbPnWirkFd6ZXyW63N F4O6lgxNKIczHmFgDItmROMBkpDHiA8qcZvN8B6+cjB6Jp4d/81I1UBE8HtdxgqBYCo9 MSZx5TtaQSJt1/tK97PHfpRVlqXfBiZ0X/WKY3QKPCbDIEnnddbqVgrXp6fwEaQEoNtg /30FpwBLsHW27rXRptk7vn09yrTOPXiZlEG7l76iv4mytgOBTt66dFy37v01AKG6yKPB hY7uPJXFuM1yqHRESp3da4SF4vXlWuCxLBGqxCbe82EFwOhvKPO1JBjL1qjNcCs5Z4NM Oiqw== X-Forwarded-Encrypted: i=1; AJvYcCXvxOvu+GVrPmO6E+I672xUyJdP1qcj++fOzjEz8Lc1oigYVChAlwdkLcTu6bWUWfxXYg2yb1UnVwduq+w=@vger.kernel.org X-Gm-Message-State: AOJu0Yx3rMQufJ08Iwh8cpvKj02No0W4nOTlQdzRvCX5V0/sfWird+0/ dolJUXbF0j44FmoJy4H+uWmRZ0Sb54orvQ0JOFiQQnE/J5b6P8OuYw+k+klayw25Oie2Xqk/jCH TzWJdPNaUdgsLJ0s9yw== X-Google-Smtp-Source: AGHT+IFzlYBons8HbckVQJEgtKyyQaEqlGaW4mw3hEeEKKFZ+HhHUPGmHhs2V3X+S3wu7586m23KOGhjXW0Ds7w= X-Received: from pjuw5.prod.google.com ([2002:a17:90a:d605:b0:32b:8ba7:306a]) (user=guanyulin job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4a0c:b0:32b:9489:c860 with SMTP id 98e67ed59e1d1-32d43f2f2a5mr22104288a91.11.1757600481383; Thu, 11 Sep 2025 07:21:21 -0700 (PDT) Date: Thu, 11 Sep 2025 14:20:16 +0000 In-Reply-To: <20250911142051.90822-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: <20250911142051.90822-1-guanyulin@google.com> X-Mailer: git-send-email 2.51.0.384.g4c02a37b29-goog Message-ID: <20250911142051.90822-5-guanyulin@google.com> Subject: [PATCH v16 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, amardeep.rai@intel.com, dominique.martinet@atmark-techno.com, quic_wcheng@quicinc.com, sakari.ailus@linux.intel.com, 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 25358cf3e324..6418cfb0fb37 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.51.0.384.g4c02a37b29-goog