Skip to contentSkip to conversation

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

PATCH Superseded Atharva Tiwari 3 participants 4 messages Nov 23

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 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 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 v2:
https://lore.kernel.org/all/[email protected]/

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
index e278db940..31961de97 100644
@@ -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
index ec4bfeb48..579ddea52 100644
@@ -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
#1 lore raw
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [Medium] usb: xhci-pci: xhci_pci_suspend() powers off card reader in runtime PM - [Medium] usb: xhci-pci…

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [Medium] usb: xhci-pci: xhci_pci_suspend() powers off 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()
- [Low] usb: xhci-pci: commit message does not explain why SPWR is needed
--

commit d1a936841fa988f7b77ee73f457b5d9b5e354c4c
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::setDevicePower in 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)

[Severity: Low]
This isn't a bug, but the commit message describes what the SPWR ACPI method does without explaining the problem that occurs without this change. Could the changelog mention that the internal card reader is powered off at boot and fails to enumerate on the USB bus unless SPWR(1) is evaluated?

Quoted 4 lines
drivers/usb/host/xhci-pci.c +0 -0
index e278db940a583..31961de9761f8 100644

[ ... ]

Quoted 5 lines
drivers/usb/host/xhci-pci.c +1 -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

drivers/usb/host/xhci-pci.c static void xhci_pci_quirks(struct device *dev, struct xhci_hcd *xhci) · line 531

Quoted 10 lines
drivers/usb/host/xhci-pci.c +10 -0
+	/* 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: Medium]
Does this leave the card reader powered on after probe failures, driver unbind, or system shutdown?

Evaluating SPWR(1) in xhci_pci_quirks() turns on the card reader during xhci_gen_setup(), before xhci_halt(), xhci_reset(), xhci_mem_init(), and shared USB3 HCD registration complete, and neither the probe error paths nor xhci_pci_remove(), xhci_pci_stop(), or xhci_pci_shutdown() call xhci_apple_card_reader_power(pdev, 0) to power it back off when the host controller 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() open-codes acpi_execute_simple_method(ACPI_HANDLE(&pdev->dev), "SPWR", 1) inside a separate #ifdef CONFIG_ACPI block instead of using xhci_apple_card_reader_power(pdev, 1), and powers on the device before xhci_gen_setup() halts and resets the host 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]
Won't calling xhci_apple_card_reader_power(pdev, 0) unconditionally in xhci_pci_suspend() cut power to the card reader during runtime suspend and while port wake bits are armed?

hcd_pci_runtime_suspend() calls suspend_common(dev, PMSG_AUTO_SUSPEND), which invokes xhci_pci_suspend() with do_wakeup = true. On controllers with hci_version >= 0x120 (such as iMac20,1), XHCI_DEFAULT_PM_RUNTIME_ALLOW enables runtime PM by default, so whenever the xHCI controller enters runtime suspend in S0, SPWR(0) powers off the card reader and prevents it from detecting SD card insertions or signaling remote wakeup.

In addition, when the card reader is connected and wakeup is enabled, xhci_bus_suspend() arms PORT_WKDISC_E on the root-hub port and xhci_suspend() preserves PORT_WAKE_BITS. Cutting power to the card reader after xhci_suspend() causes the device to drop off the bus while wake-on-disconnect is armed, which can trigger an immediate disconnect wake event or force an unexpected disconnect and re-enumeration on resume.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Atharva Tiwari
Quoted 39 lines · Atharva Tiwari

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 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 v2:
https://lore.kernel.org/all/[email protected]/

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 +0 -0
index e278db940..31961de97 100644

drivers/usb/host/xhci-pci.c line 18

Quoted 4 lines
drivers/usb/host/xhci-pci.c +1 -0
@@ -15,6 +15,7 @@
 #include <linux/reset.h>
 #include <linux/suspend.h>
 #include <linux/bitfield.h>
+#include <linux/platform_data/x86/apple.h>

That feels very wrong for all non-x86 systems to be forced to include.

drivers/usb/host/xhci-pci.c static void xhci_pci_quirks(struct device *dev, struct xhci_hcd *xhci) · line 525

Quoted 12 lines
drivers/usb/host/xhci-pci.c +5 -0
 #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.
+	 */

Wrong comment style :(

And why an #ifdef in this file?

drivers/usb/host/xhci-pci.c static void xhci_pci_quirks(struct device *dev, struct xhci_hcd *xhci) · line 526

Quoted 1 line
drivers/usb/host/xhci-pci.c +1 -0
+	if (x86_apple_machine && ACPI_HANDLE(&pdev->dev) &&

So what happens on arm64 systems with ACPI? Riscv systems? Does this just break the build?

Quoted 10 lines
drivers/usb/host/xhci-pci.c +6 -0
+			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");

drivers/usb/host/xhci-pci.c static void xhci_find_lpm_incapable_ports(struct usb_hcd *hcd, struct usb_device · line 581

Quoted 8 lines
drivers/usb/host/xhci-pci.c +4 -1
@@ -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);
+}

You removed the blank line?

and this still feels very platform specific, why must it be in generic xhci code?

and what about all of the sashiko review comments that were made on this? Are they all wrong?

thanks,

greg k-h

#3 lore raw
Quoted 1 line

That feels very wrong for all non-x86 systems to be forced to include.

I understand that it may feels wrong, but the file checks for CONFIG_X86 and if it is not true, then it sets x86_apple_machine to false automatically

#ifndef PLATFORM_DATA_X86_APPLE_H
#define PLATFORM_DATA_X86_APPLE_H

#ifdef CONFIG_X86
/**
* x86_apple_machine - whether the machine is an x86 Apple Macintosh */
extern bool x86_apple_machine;
#else
#define x86_apple_machine false
#endif

#endif

^ complete file

Quoted 2 lines

and this still feels very platform specific, why must it be in generic xhci code?

Apple macs use the generic driver, and i figured it would be best to do this as a quirk in xhci-pci.c If you have any other ideas, please provide them.

Thanks,

Atharva.