* [PATCH RESEND v2] usb: xhci-pci: Add Power handling for Card readers on Apple systems
@ 2026-11-23 4:44 Atharva Tiwari
2026-11-23 4:57 ` sashiko-bot
2026-11-23 4:57 ` Greg Kroah-Hartman
0 siblings, 2 replies; 3+ messages in thread
From: Atharva Tiwari @ 2026-11-23 4:44 UTC (permalink / raw)
Cc: Atharva Tiwari, Greg Kroah-Hartman, Mathias Nyman,
open list:USB SUBSYSTEM, open list
Add Power handling for Card readers on Apple systems using SPWR
ACPI method, SPWR(1) turns on the card reader, SPWR(0) turns off
the card reader, on newer systems SPWR just writes a variable in EC.
On previous systems it writes to GPIO pins.
Its also used by the macOS driver with the function
AppleUSB30XHCICardReaderPort::setDevicePower in AppleUSBXHCIPCI.kext.
(This should fix https://bugzilla.kernel.org/show_bug.cgi?id=111201
But thats not confirmed)
(Tested on iMac20,1)
Changes in v2:
- Fixed grammatical errors.
- Added CONFIG_ACPI check
- Checked for xhci_suspend success before turning off power
Link to v1:
https://lore.kernel.org/all/20261116231926.5891-1-atharvatiwarilinuxdev@gmail.com/
Signed-off-by: Atharva Tiwari <atharvatiwarilinuxdev@gmail.com>
---
drivers/usb/core/usb-acpi.c | 34 ++++++++++++++++++++++++++++++++++
drivers/usb/host/xhci-pci.c | 25 ++++++++++++++++++++++++-
drivers/usb/host/xhci.h | 1 +
3 files changed, 59 insertions(+), 1 deletion(-)
diff --git a/drivers/usb/core/usb-acpi.c b/drivers/usb/core/usb-acpi.c
index 489dbdc96..69f86b2b6 100644
--- a/drivers/usb/core/usb-acpi.c
+++ b/drivers/usb/core/usb-acpi.c
@@ -12,6 +12,7 @@
#include <linux/acpi.h>
#include <linux/pci.h>
#include <linux/usb/hcd.h>
+#include <linux/platform_data/x86/apple.h>
#include "hub.h"
@@ -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;
+ }
+
+ 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);
+}
+
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);
status = acpi_evaluate_object(handle, "_UPC", NULL, &buffer);
if (ACPI_FAILURE(status))
goto out;
diff --git a/drivers/usb/host/xhci-pci.c b/drivers/usb/host/xhci-pci.c
index e278db940..31961de97 100644
--- a/drivers/usb/host/xhci-pci.c
+++ b/drivers/usb/host/xhci-pci.c
@@ -15,6 +15,7 @@
#include <linux/reset.h>
#include <linux/suspend.h>
#include <linux/bitfield.h>
+#include <linux/platform_data/x86/apple.h>
#include "xhci.h"
#include "xhci-trace.h"
@@ -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 */
+
if (xhci->quirks & XHCI_RESET_ON_RESUME)
xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
"QUIRK: Resetting on resume");
@@ -562,10 +575,14 @@ static void xhci_find_lpm_incapable_ports(struct usb_hcd *hcd, struct usb_device
}
}
}
-
+static void xhci_apple_card_reader_power(struct pci_dev *pdev, u64 on)
+{
+ acpi_execute_simple_method(ACPI_HANDLE(&pdev->dev), "SPWR", on);
+}
#else
static void xhci_pme_acpi_rtd3_enable(struct pci_dev *dev) { }
static void xhci_find_lpm_incapable_ports(struct usb_hcd *hcd, struct usb_device *hdev) { }
+static void xhci_apple_card_reader_power(struct pci_dev *pdev, u64 on) { }
#endif /* CONFIG_ACPI */
/* called during probe() after chip reset completes */
@@ -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);
+
if (ret && (xhci->quirks & XHCI_SSIC_PORT_UNUSED))
xhci_ssic_port_unused_quirk(hcd, false);
@@ -895,6 +915,9 @@ static int xhci_pci_resume(struct usb_hcd *hcd, pm_message_t msg)
if (xhci->quirks & XHCI_PME_STUCK_QUIRK)
xhci_pme_quirk(hcd);
+ if (xhci->quirks & XHCI_APPLE_CARD_READER_POWER)
+ xhci_apple_card_reader_power(pdev, 1);
+
return xhci_resume(xhci, power_lost, is_auto_resume);
}
diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h
index ec4bfeb48..579ddea52 100644
--- a/drivers/usb/host/xhci.h
+++ b/drivers/usb/host/xhci.h
@@ -1652,6 +1652,7 @@ struct xhci_hcd {
#define XHCI_CDNS_SCTX_QUIRK BIT_ULL(48)
#define XHCI_ETRON_HOST BIT_ULL(49)
#define XHCI_LIMIT_ENDPOINT_INTERVAL_9 BIT_ULL(50)
+#define XHCI_APPLE_CARD_READER_POWER BIT_ULL(51)
unsigned int num_active_eps;
unsigned int limit_active_eps;
--
2.55.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH RESEND v2] usb: xhci-pci: Add Power handling for Card readers on Apple systems
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
2026-11-23 4:57 ` Greg Kroah-Hartman
1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-23 4:57 UTC (permalink / raw)
To: Atharva Tiwari; +Cc: open list:USB SUBSYSTEM
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH RESEND v2] usb: xhci-pci: Add Power handling for Card readers on Apple systems
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
@ 2026-11-23 4:57 ` Greg Kroah-Hartman
1 sibling, 0 replies; 3+ messages in thread
From: Greg Kroah-Hartman @ 2026-11-23 4:57 UTC (permalink / raw)
To: Atharva Tiwari; +Cc: Mathias Nyman, open list:USB SUBSYSTEM, open list
On Sun, Nov 22, 2026 at 11:44:09PM -0500, Atharva Tiwari wrote:
> Add Power handling for Card readers on Apple systems using SPWR
> ACPI method, SPWR(1) turns on the card reader, SPWR(0) turns off
> the card reader, on newer systems SPWR just writes a variable in EC.
> On previous systems it writes to GPIO pins.
>
> Its also used by the macOS driver with the function
> AppleUSB30XHCICardReaderPort::setDevicePower in AppleUSBXHCIPCI.kext.
>
> (This should fix https://bugzilla.kernel.org/show_bug.cgi?id=111201
> But thats not confirmed)
>
> (Tested on iMac20,1)
>
> Changes in v2:
> - Fixed grammatical errors.
> - Added CONFIG_ACPI check
> - Checked for xhci_suspend success before turning off power
changes go below the --- line.
thanks,
greg k-h
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-23 4:57 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2026-11-23 4:57 ` Greg Kroah-Hartman
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox