usb: xhci-pci: Add Power handling for Card readers on Apple systems
Subsystems (2)
PATCH v2 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 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/[email protected]/
--- drivers/usb/host/xhci-pci.c | 25 ++++++++++++++++++++++++- drivers/usb/host/xhci.h | 1 + 2 files changed, 25 insertions(+), 1 deletion(-)
drivers/usb/host/xhci-pci.c +24 -1
@@ -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); }
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 3 potential issues to consider: - [Medium] usb: xhci-pci: xhci_pci_suspend() powers off Apple card reader in runtime PM - [Medium] usb…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [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: xhci-pci: duplicate SPWR call and early power-on in xhci_pci_quirks()
--
commit 9d162606edd929676ba53f7638bfed5c72201d2d
Author: Atharva Tiwari <[email protected]>
usb: xhci-pci: Add Power handling for Card readers on Apple systems
This commit adds an XHCI_APPLE_CARD_READER_POWER quirk on x86 Apple machines to toggle card reader power via the ACPI SPWR method during probe, suspend, and resume.
Quoted 4 lines
drivers/usb/host/xhci-pci.c +0
-0
[ ... ]
Quoted 9 lines
drivers/usb/host/xhci-pci.c +5
-0@@ -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. +*/
drivers/usb/host/xhci-pci.c static void xhci_pci_quirks(struct device *dev, struct xhci_hcd *xhci) · line 535
Quoted 10 lines
drivers/usb/host/xhci-pci.c +7
-0+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" );
[Severity: Medium]
Does this leave the card reader powered on if probe fails later in xhci_gen_setup() or xhci_pci_common_probe(), or when the driver is unbound or the system shuts down?
Neither the probe error paths, xhci_pci_remove(), nor xhci_pci_shutdown() call xhci_apple_card_reader_power(pdev, 0), and pci_device_remove() wakes the controller via pm_runtime_get_sync() -> xhci_pci_resume() before calling xhci_pci_remove(), turning SPWR(1) back on before the HCD is stopped.
Quoted 4 lines
drivers/usb/host/xhci-pci.c +0
-0@@ -562,10 +575,14 @@ static void xhci_find_lpm_incapable_ports(struct usb_hcd *hcd, struct usb_device } } }
drivers/usb/host/xhci-pci.c static void xhci_find_lpm_incapable_ports(struct usb_hcd *hcd, struct usb_device · line 586
Quoted 10 lines
drivers/usb/host/xhci-pci.c +5
-1- +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 */
[Severity: Low]
This isn't a bug, but xhci_pci_quirks() adds a separate #ifdef CONFIG_ACPI block and open-codes acpi_execute_simple_method() instead of using xhci_apple_card_reader_power(), and it powers on the device before xhci_gen_setup() halts and resets the controller.
Since ACPI_HANDLE() and acpi_has_method() already have !CONFIG_ACPI stubs, xhci_pci_quirks() could just set XHCI_APPLE_CARD_READER_POWER and let xhci_pci_setup() call xhci_apple_card_reader_power(pdev, 1) after xhci_gen_setup() succeeds, matching XHCI_PME_STUCK_QUIRK and xhci_pme_acpi_rtd3_enable().
drivers/usb/host/xhci-pci.c static int xhci_pci_suspend(struct usb_hcd *hcd, bool do_wakeup) · line 877
Quoted 9 lines
drivers/usb/host/xhci-pci.c +3
-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 ); +if (ret && (xhci->quirks &XHCI_SSIC_PORT_UNUSED ))xhci_ssic_port_unused_quirk (hcd, false);
[Severity: Medium]
Does calling xhci_apple_card_reader_power(pdev, 0) unconditionally in xhci_pci_suspend() break runtime PM and wake-on-disconnect on the card reader port?
hcd_pci_runtime_suspend() also calls suspend_common() -> xhci_pci_suspend() (and controllers with hci_version >= 0x120 enable runtime PM by default via XHCI_DEFAULT_PM_RUNTIME_ALLOW), so cutting power with SPWR(0) during runtime suspend prevents the card reader from detecting card insertion or signaling remote wakeup while the controller is idle.
Furthermore, when do_wakeup is true, xhci_bus_suspend() arms PORT_WKDISC_E on connected ports and xhci_suspend() -> xhci_disable_hub_port_wake() preserves PORT_WAKE_BITS; cutting power to an attached USB card reader after xhci_suspend() causes a disconnect on the port that can trigger a spurious wake event and drops the device's power session without notifying USB core via usb_root_hub_lost_power() on resume.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1