usb: xhci-pci: Add Power handling for Card readers on Apple systems
Subsystems (2)
PATCH v4 1/1 usb: xhci-pci: Add Power handling for Card readers on Apple systems
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 v4:
- Check for !do_wakeup in suspend to prevent breaking sd card in runtime pm
- Set card reader power to 0 on xhci_pci_remove
- Added blank line
- Fixed comment style
Changes in v3:
- Added changes at correct location
- Removed accidental usb-acpi code.
Changes in v2:
- Fixed grammatical errors.
- Added CONFIG_ACPI check
- Checked for xhci_suspend success before turning off power
Link to v3:
https://lore.kernel.org/all/[email protected]/
Link to v2:
https://lore.kernel.org/all/[email protected]/
Link to v1:
https://lore.kernel.org/all/[email protected]/
drivers/usb/host/xhci-pci.c | 28 ++++++++++++++++++++++++++++ drivers/usb/host/xhci.h | 1 + 2 files changed, 29 insertions(+)
drivers/usb/host/xhci-pci.c +28 -0
@@ -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,19 @@ 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 machines with a card reader, the reader is powered +* off at boot. Evaluate SPWR(1) to turn it on, so it works at +* boot. SPWR(0) turns it off again. +*/ +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" );
@@ -563,9 +577,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 */
@@ -750,6 +769,9 @@ void xhci_pci_remove(struct pci_dev *dev)usb_hcd_pci_remove (dev); +if (xhci->quirks &XHCI_APPLE_CARD_READER_POWER ) +xhci_apple_card_reader_power (dev,0 ); +/* Workaround for spurious wakeups at shutdown with HSW */ if (set_power_d3)pci_set_power_state (dev, PCI_D3hot);
@@ -853,6 +875,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 ) && !do_wakeup) +xhci_apple_card_reader_power (pdev,0 ); +if (ret && (xhci->quirks &XHCI_SSIC_PORT_UNUSED ))xhci_ssic_port_unused_quirk (hcd, false);
@@ -895,6 +920,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); }
drivers/usb/host/xhci.h +1 -0
@@ -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;
Signature · 1 line
2.55.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] usb: xhci-pci: use-after-free of xhci in xhci_pci_remove() - [Medium] usb: xhci-pci: card reader…
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] usb: xhci-pci: use-after-free of xhci in xhci_pci_remove()
- [Medium] usb: xhci-pci: card reader left powered on after probe failure or shutdown
- [Low] usb: xhci-pci: duplicate SPWR call and early power-on in xhci_pci_quirks()
- [Low] usb: xhci-pci: commit message does not explain why SPWR is needed
--
commit d69011e93a3dca622d8ee15905ffc3ff76202bd1
Author: Atharva Tiwari <[email protected]>
Quoted 10 lines
usb: xhci-pci: Add Power handling for Card readers on Apple systems
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::setDevicePowerin AppleUSBXHCIPCI.kext.
Quoted 6 lines
(This should fix https://bugzilla.kernel.org/show_bug.cgi?id=111201
But thats not confirmed)(Tested on iMac20,1)
Signed-off-by Atharva Tiwari <[email protected]>
[Severity: Low]
This isn't a bug, but the commit message describes what SPWR(1) and SPWR(0) do without explaining why the driver must call them. As the code comment notes, the card reader starts powered off at boot on these Apple machines and fails to enumerate unless SPWR(1) is evaluated.
Quoted 4 lines
drivers/usb/host/xhci-pci.c +0
-0
[ ... ]
Quoted 6 lines
drivers/usb/host/xhci-pci.c +2
-0@@ -517,6 +518,19 @@ 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 +/*
drivers/usb/host/xhci-pci.c static void xhci_pci_quirks(struct device *dev, struct xhci_hcd *xhci) · line 532
Quoted 10 lines
drivers/usb/host/xhci-pci.c +10
-0+* On Apple machines with a card reader, the reader is powered +* off at boot. Evaluate SPWR(1) to turn it on, so it works at +* boot. SPWR(0) turns it off again. +*/ +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]
As reported by Greg Kroah-Hartman on v3 [1], this adds an #ifdef CONFIG_ACPI block in xhci_pci_quirks() and open-codes acpi_execute_simple_method() instead of using the xhci_apple_card_reader_power() helper defined below.
xhci_pci_quirks() also runs inside xhci_gen_setup() before xhci_halt() and xhci_reset() reset the controller. Since ACPI_HANDLE() and acpi_has_method() already have !CONFIG_ACPI stubs, xhci_pci_quirks() can just set XHCI_APPLE_CARD_READER_POWER without the #ifdef, and xhci_pci_setup() can call xhci_apple_card_reader_power(pdev, 1) after xhci_gen_setup() finishes, matching XHCI_PME_STUCK_QUIRK and xhci_pme_acpi_rtd3_enable().
[ ... ]
drivers/usb/host/xhci-pci.c void xhci_pci_remove(struct pci_dev *dev) · line 777
Quoted 10 lines
drivers/usb/host/xhci-pci.c +3
-0@@ -750,6 +769,9 @@ void xhci_pci_remove(struct pci_dev *dev)usb_hcd_pci_remove (dev); +if (xhci->quirks &XHCI_APPLE_CARD_READER_POWER ) +xhci_apple_card_reader_power (dev,0 ); +/* Workaround for spurious wakeups at shutdown with HSW */ if (set_power_d3)pci_set_power_state (dev, PCI_D3hot);
[Severity: High]
Does checking xhci->quirks after usb_hcd_pci_remove(dev) cause a use-after-free of xhci?
xhci points into primary_hcd->hcd_priv, and usb_hcd_pci_remove() drops the final reference on the primary struct usb_hcd via usb_put_hcd(hcd) -> hcd_release() -> kfree(hcd). Notice how set_power_d3 caches xhci->quirks & XHCI_SPURIOUS_WAKEUP into a local bool at the top of xhci_pci_remove() before usb_hcd_pci_remove() frees xhci.
[Severity: Medium]
Can the card reader be left powered on after a probe failure or system shutdown?
If probe fails after SPWR(1) has been evaluated (for example later in xhci_gen_setup(), usb_add_hcd(), or xhci_pci_common_probe(), which unwinds via usb_hcd_pci_remove() rather than xhci_pci_remove()), or when xhci_pci_shutdown() halts the controller on system shutdown, xhci_apple_card_reader_power(dev, 0) is never called.
[1] https://lore.kernel.org/r/2026112342-engorge-polymer-0717@gregkh
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1