All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Atharva Tiwari" <atharvatiwarilinuxdev@gmail.com>
Cc: "open list:USB SUBSYSTEM" <linux-usb@vger.kernel.org>
Subject: Re: [PATCH RESEND v2] usb: xhci-pci: Add Power handling for Card readers on Apple systems
Date: Mon, 23 Nov 2026 04:57:12 +0000	[thread overview]
Message-ID: <sashiko-outbox-228682@kernel.org> (raw)
In-Reply-To: <20261123044412.8721-1-atharvatiwarilinuxdev@gmail.com>

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [Medium] usb: acpi: unvalidated SSP package access in
  usb_acpi_apple_get_location()
- [Medium] usb: acpi: broken port peering in
  usb_acpi_apple_get_location()
- [Medium] usb: xhci-pci: xhci_pci_suspend() powers off Apple card
  reader in runtime PM
- [Medium] usb: xhci-pci: missing xhci_apple_card_reader_power() off on
  teardown
- [Low] usb: acpi: undocumented usb-acpi.c change and SSD comment
  mismatch
- [Low] usb: xhci-pci: duplicate SPWR call and early power-on in
  xhci_pci_quirks()
--

commit ff305eafd156778a9e4b723a3c6ba3da48ac220c
Author: Atharva Tiwari <atharvatiwarilinuxdev@gmail.com>

usb: xhci-pci: Add Power handling for Card readers on Apple systems

This commit adds ACPI SPWR power management for internal card readers on
x86 Apple systems in xhci-pci and adds ACPI port location lookup for XHC1
ports in usb-acpi.

> diff --git a/drivers/usb/core/usb-acpi.c b/drivers/usb/core/usb-acpi.c
> index 489dbdc96f94a..69f86b2b65cb0 100644
> --- a/drivers/usb/core/usb-acpi.c
> +++ b/drivers/usb/core/usb-acpi.c
[ ... ]
> @@ -199,6 +200,34 @@ static int usb_acpi_add_usb4_devlink(struct usb_device *udev)
>   */
>  #define USB_ACPI_LOCATION_VALID (1 << 31)
>  
> +static void
> +usb_acpi_apple_get_location(struct usb_port *port_dev, acpi_handle handle)
> +{
> +	struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
> +	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL } ;
> +	acpi_status status;
> +
> +	/* The function only applies to CPU's internal XHCI controller. */
> +	if (strncmp(acpi_device_bid(ACPI_COMPANION(to_usb_device(
> +					port_dev->dev.parent->parent)->bus->sysdev)),
> +					"XHC1", ACPI_NAMESEG_SIZE))
> +		return;
> +
> +	if (hdev->speed >= USB_SPEED_SUPER) {
> +		port_dev->location = USB_ACPI_LOCATION_VALID |
> +				acpi_fetch_acpi_dev(handle)->pnp.bus_address;
> +		return;
> +	}

[Severity: Medium]
Can this break port peering in find_and_link_peer() on SuperSpeed hubs on
the XHC1 bus?

hdev->bus->sysdev points to the host controller for every hub on the bus,
not only the root hub (!hdev->parent).  On any SuperSpeed port where
hdev->speed >= USB_SPEED_SUPER, this unconditionally overwrites
port_dev->location with the port's _ADR even if _PLD already set a
location, if hdev is a downstream hub, or if the companion USB 2.0 ports do
not implement SSP.  Because find_and_link_peer() skips default peering
whenever port_dev->location is non-zero, those SuperSpeed ports will fail
to match their USB 2.0 companion ports.

> +
> +	status = acpi_evaluate_object_typed(handle, "SSP", NULL, &buffer, ACPI_TYPE_PACKAGE);
> +	if (ACPI_FAILURE(status))
> +		return;
> +
> +	port_dev->location = USB_ACPI_LOCATION_VALID |
> +		((union acpi_object *)buffer.pointer)->package.elements[1].integer.value;
> +	kfree(buffer.pointer);
> +}

[Severity: Medium]
Can indexing package.elements[1].integer.value read out of bounds or read
a non-integer union member?

acpi_evaluate_object_typed() only verifies that the top-level object is
ACPI_TYPE_PACKAGE; it does not check package.count or element types.  If
SSP returns a package with fewer than two elements or elements[1] is not
ACPI_TYPE_INTEGER, this reads past the allocated elements array or mixes
union fields into port_dev->location (exposed via sysfs).

> +
>  static void
>  usb_acpi_get_connect_type(struct usb_port *port_dev, acpi_handle *handle)
>  {
> @@ -221,6 +250,11 @@ usb_acpi_get_connect_type(struct usb_port *port_dev, acpi_handle *handle)
>  		port_dev->location = USB_ACPI_LOCATION_VALID |
>  			pld->group_token << 8 | pld->group_position;
>  
> +	/* On x86 apple macs, Apple exposes location via SSD method
> +	 * instead of _PLD On USB 2.0, and on USB 3.0 it uses _ADR
> +	 */
> +	if (x86_apple_machine)
> +		usb_acpi_apple_get_location(port_dev, handle);

[Severity: Low]
This isn't a bug, but the usb-acpi.c port location changes are not
mentioned in the commit message, the comment here refers to an "SSD"
method while usb_acpi_apple_get_location() evaluates "SSP", and
usb_acpi_apple_get_location() recomputes
to_usb_device(port_dev->dev.parent->parent) instead of using hdev.

> diff --git a/drivers/usb/host/xhci-pci.c b/drivers/usb/host/xhci-pci.c
> index e278db940a583..31961de9761f8 100644
> --- a/drivers/usb/host/xhci-pci.c
> +++ b/drivers/usb/host/xhci-pci.c
[ ... ]
> @@ -517,6 +518,18 @@ static void xhci_pci_quirks(struct device *dev, struct xhci_hcd *xhci)
>  	if (xhci->hci_version >= 0x120)
>  		xhci->quirks |= XHCI_DEFAULT_PM_RUNTIME_ALLOW;
>  
> +#ifdef CONFIG_ACPI
> +	/* On Apple Machine with Card readers, we have to use SPWR(0/1) to turn
> +	 * off/on the card reader, And on boot the card reader is off, so we turn
> +	 * it on.
> +	 */
> +	if (x86_apple_machine && ACPI_HANDLE(&pdev->dev) &&
> +			acpi_has_method(ACPI_HANDLE(&pdev->dev), "SPWR")) {
> +		acpi_execute_simple_method(ACPI_HANDLE(&pdev->dev), "SPWR", 1);
> +		xhci->quirks |= XHCI_APPLE_CARD_READER_POWER;
> +	}
> +#endif /* CONFIG_ACPI */

[Severity: Low]
This isn't a bug, but xhci_pci_quirks() runs inside xhci_gen_setup() before
the controller is halted and reset, and this open-codes
acpi_execute_simple_method() under a separate #ifdef CONFIG_ACPI block
instead of using xhci_apple_card_reader_power().  Since ACPI_HANDLE() and
acpi_has_method() already have !CONFIG_ACPI stubs, could xhci_pci_quirks()
just set XHCI_APPLE_CARD_READER_POWER and let xhci_pci_setup() call
xhci_apple_card_reader_power(pdev, 1), matching XHCI_PME_STUCK_QUIRK and
xhci_pme_acpi_rtd3_enable()?

[Severity: Medium]
Does the card reader stay powered on if probe fails later, when the driver
is unbound in xhci_pci_remove(), or on system shutdown in
xhci_pci_shutdown()?  None of the probe error, removal, or shutdown paths
call xhci_apple_card_reader_power(pdev, 0).

[ ... ]
> @@ -853,6 +870,9 @@ static int xhci_pci_suspend(struct usb_hcd *hcd, bool do_wakeup)
>  	/* synchronize irq when using MSI-X */
>  	xhci_msix_sync_irqs(xhci);
>  
> +	if (!ret && (xhci->quirks & XHCI_APPLE_CARD_READER_POWER))
> +		xhci_apple_card_reader_power(pdev, 0);
> +

[Severity: Medium]
Does calling xhci_apple_card_reader_power(pdev, 0) here power off the card
reader during runtime suspend as well as system sleep?

hcd_pci_runtime_suspend() calls suspend_common(dev, PMSG_AUTO_SUSPEND) ->
xhci_pci_suspend(hcd, true), and controllers with hci_version >= 0x120
enable runtime PM by default via XHCI_DEFAULT_PM_RUNTIME_ALLOW.  Cutting
card reader power during runtime suspend prevents the reader from detecting
card insertion while the system is running, and removing power after
xhci_suspend() while PORT_WKDISC_E is armed on a connected port can
trigger a disconnect wake event or drop the device across suspend without
notifying USB core via usb_root_hub_lost_power().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261123044412.8721-1-atharvatiwarilinuxdev@gmail.com?part=1

  reply	other threads:[~2026-11-23  4:57 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23  4:44 [PATCH RESEND v2] usb: xhci-pci: Add Power handling for Card readers on Apple systems Atharva Tiwari
2026-11-23  4:57 ` sashiko-bot [this message]
2026-11-23  4:57 ` Greg Kroah-Hartman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-228682@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=atharvatiwarilinuxdev@gmail.com \
    --cc=linux-usb@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.