usb: xhci-pci: Add Power handling for Card readers on Apple systems
Subsystems (2)
PATCH RESEND 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/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(-)
drivers/usb/core/usb-acpi.c +34 -0
@@ -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;
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 6 potential issues to consider: - [Medium] usb: acpi: unvalidated SSP package access in usb_acpi_apple_get_location() - [Medium] usb: acpi…
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 <[email protected]>
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.
Quoted 4 lines
drivers/usb/core/usb-acpi.c +0
-0
[ ... ]
Quoted 12 lines
drivers/usb/core/usb-acpi.c +8
-0@@ -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. */
drivers/usb/core/usb-acpi.c static int usb_acpi_add_usb4_devlink(struct usb_device *udev) · line 220
Quoted 10 lines
drivers/usb/core/usb-acpi.c +10
-0+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.
drivers/usb/core/usb-acpi.c static int usb_acpi_add_usb4_devlink(struct usb_device *udev) · line 229
Quoted 9 lines
drivers/usb/core/usb-acpi.c +9
-0+ + 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).
drivers/usb/core/usb-acpi.c usb_acpi_get_connect_type(struct usb_port *port_dev, acpi_handle *handle) · line 257
Quoted 13 lines
drivers/usb/core/usb-acpi.c +6
-0+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.
Quoted 4 lines
drivers/usb/host/xhci-pci.c +0
-0
[ ... ]
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: 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).
[ ... ]
drivers/usb/host/xhci-pci.c static int xhci_pci_suspend(struct usb_hcd *hcd, bool do_wakeup) · line 875
Quoted 7 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 ); +
[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().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Greg Kroah-Hartman
maintainerQuoted 11 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::setDevicePowerin AppleUSBXHCIPCI.kext.(This should fix https://bugzilla.kernel.org/show_bug.cgi?id=111201
But thats not confirmed)
Quoted 6 lines
(Tested on iMac20,1)
Changes in v2:
- Fixed grammatical errors.
- AddedCONFIG_ACPIcheck
- Checked forxhci_suspendsuccess before turning off power
changes go below the --- line.
thanks,
greg k-h