rtsx_usb: fix tray-reader false card detect and autosuspend
Subsystems (2)
v2 cover rtsx_usb: fix tray-reader false card detect and autosuspend
This series fixes the empty-tray detect loop seen on a Realtek RTS5129 USB SD reader, while also avoiding USB register accesses from the USB runtime autosuspend callback.
Compared with the previous RFC, this no longer qualifies media by issuing SD/MMC commands from ->get_cd(). The rtsx_usb_sdmmc child still reports the raw card-detect state initially, then lets the normal MMC rescan path probe the media. If an initialization command times out before a card is attached, the SD child suppresses the raw SD_CD signal until the tray/card detect line drops again.
The parent rtsx_usb change removes USB I/O from runtime autosuspend and keeps only the existing Memory Stick autosuspend deferral based on cached card-status bits. Raw SD_CD is not used as a parent autosuspend blocker, because this reader can assert it for an empty tray.
Runtime-tested on a StarLite Mk V with Realtek RTS5129 (0bda:0129), using these changes as a matched rtsx_usb/rtsx_usb_sdmmc/rtsx_usb_ms module set on Linux 7.0.0-27-generic.
Device enumeration:
Bus 001 Device 003: ID 0bda:0129 Realtek Semiconductor Corp. RTS5129 Card Reader Controller /: Bus 001.Port 001: Dev 001, Class=root_hub, Driver=xhci_hcd/12p, 480M
|__ Port 004: Dev 003, If 0, Class=Vendor Specific Class, Driver=rtsx_usb, 480M
Card inserted:
mmc0: new UHS-I speed SDR50 SDXC card at address 544c
mmcblk0: mmc0:544c LX2TB 1.82 TiB
mmcblk0: p1
Deep S3 with the card inserted:
PM: suspend entry (deep)
ACPI: PM: Waking up from system sleep state S3
PM: suspend exit
After resume, the card remained visible:
NAME SIZE TYPE FSTYPE MOUNTPOINTS mmcblk0 1.8T disk `-mmcblk0p1 1.8T part exfat
Empty tray:
mmc0: card 544c removed /sys/bus/usb/devices/1-4/power/control=auto /sys/bus/usb/devices/1-4/power/runtime_status=suspended /sys/bus/usb/devices/1-4/power/runtime_usage=0
With the empty tray left inserted for about three minutes, no repeated MMC detect attempts, command timeouts, or USB reset churn were observed.
Sean Rhodes (2):
misc: rtsx_usb: avoid USB I/O in runtime autosuspend
mmc: rtsx_usb_sdmmc: suppress false CD after init timeout
drivers/misc/cardreader/rtsx_usb.c | 26 ++++++++++++++++++++------ drivers/mmc/host/rtsx_usb_sdmmc.c | 13 ++++++++++++- include/linux/rtsx_usb.h | 3 +++ 3 files changed, 35 insertions(+), 7 deletions(-)
PATCH v2 1/2 misc: rtsx_usb: avoid USB I/O in runtime autosuspend
The runtime autosuspend callback currently queries card status and clears OCP by issuing USB register accesses. This can run from the USB runtime-PM path itself, which is the wrong place to start more device I/O.
Keep a cached copy of the card-status bits from normal status reads instead. During runtime autosuspend, use that cached value only to preserve the existing Memory Stick autosuspend deferral.
Do not treat raw SD_CD as an autosuspend blocker, because tray-based SD readers can assert SD_CD with an empty tray. A real SD card is protected by the SD/MMC child runtime-PM usage once powered.
Also stop clearing OCP from the runtime autosuspend callback, so the callback does not issue USB commands.
--- drivers/misc/cardreader/rtsx_usb.c | 26 ++++++++++++++++++++------ include/linux/rtsx_usb.h | 3 +++ 2 files changed, 23 insertions(+), 6 deletions(-)
drivers/misc/cardreader/rtsx_usb.c +20 -6
@@ -312,6 +312,9 @@ int rtsx_usb_get_card_status(struct rtsx_ucr *ucr, u16 *status)if (ret <0 )return ret; + ucr->card_status_cache = *status; + ucr->card_status_valid = true; +return 0 ; }EXPORT_SYMBOL_GPL (rtsx_usb_get_card_status);
@@ -623,6 +626,7 @@ static int rtsx_usb_probe(struct usb_interface *intf, {struct usb_device *usb_dev =interface_to_usbdev (intf);struct rtsx_ucr *ucr; +u16 status;int ret; dev_dbg(&intf->dev,
@@ -659,6 +663,9 @@ static int rtsx_usb_probe(struct usb_interface *intf,if (ret) goto out_init_fail; +/* Prime cached status for runtime autosuspend decisions. */ +rtsx_usb_get_card_status (ucr, &status); +/* initialize USB SG transfer timer */ timer_setup (&ucr->sg_timer, rtsx_usb_sg_timed_out,0 );
@@ -713,22 +720,29 @@ static int rtsx_usb_suspend(struct usb_interface *intf, pm_message_t message)struct rtsx_ucr *ucr = (struct rtsx_ucr *)usb_get_intfdata (intf);u16 val =0 ; +bool valid = false;dev_dbg (&intf->dev,"%s called with pm message 0x%04x\n" , __func__, message.event);if (PMSG_IS_AUTO (message)) {if (mutex_trylock (&ucr->dev_mutex)) { -rtsx_usb_get_card_status (ucr, &val); + valid = ucr->card_status_valid; +if (valid) + val = ucr->card_status_cache;mutex_unlock (&ucr->dev_mutex); -/* Defer the autosuspend if card exists */ -if (val & (SD_CD |MS_CD )) { +/* +* Do not issue USB commands from runtime autosuspend. +* Raw SD_CD is not authoritative on tray-based readers, +* while a real SD card is protected by the SD/MMC child +* runtime-PM reference once the card is powered. Keep +* the historical Memory Stick autosuspend deferral when +* the cached status says MS media is present. +*/ +if (valid && (val &MS_CD )) {device_for_each_child (&intf->dev,NULL , rtsx_usb_resume_child);return -EAGAIN ; - }else { -/* if the card does not exists, clear OCP status */ -rtsx_usb_write_register (ucr,OCPCTL ,MS_OCP_CLEAR ,MS_OCP_CLEAR ); } }else {/* There is an ongoing operation*/
include/linux/rtsx_usb.h +3 -0
@@ -61,6 +61,9 @@ struct rtsx_ucr {struct timer_list sg_timer;struct mutex dev_mutex; + +u16 card_status_cache; +bool card_status_valid; };/* buffer size */
PATCH v2 2/2 mmc: rtsx_usb_sdmmc: suppress false CD after init timeout
In reply to Sean RhodesSome Realtek USB SD readers with a tray keep raw SD_CD asserted when an empty tray is inserted. The MMC core then repeatedly tries to initialize non-existent media, sees command timeouts, calls ->get_cd() again, and starts the same detect cycle over.
Do not qualify media by open-coding MMC commands in ->get_cd(). Instead, let the normal MMC rescan path probe the card. If an initialization command times out before a card has been attached, suppress the raw SD_CD signal so the host can settle and the USB parent can autosuspend.
Clear the suppression only when raw SD_CD drops. On the affected tray reader, changing media requires removing and reinserting the tray, so a low CD transition is the signal that a new insertion attempt can be trusted again.
--- drivers/mmc/host/rtsx_usb_sdmmc.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
drivers/mmc/host/rtsx_usb_sdmmc.c +12 -1
@@ -44,6 +44,7 @@ struct rtsx_usb_sdmmc {bool double_clk;bool host_removal;bool card_exist; +bool suppress_cd;bool initial_mode;bool ddr_mode;
@@ -774,6 +775,7 @@ static int sdmmc_get_cd(struct mmc_host *mmc)struct rtsx_ucr *ucr = host->ucr;int err;u16 val; +bool cd;if (host->host_removal)return -ENOMEDIUM ;
@@ -791,8 +793,14 @@ static int sdmmc_get_cd(struct mmc_host *mmc)/* get OCP status */ host->ocp_stat = (val >>4 ) &0x03 ; + cd = val &SD_CD ; -if (val &SD_CD ) { +if (!cd) { +WRITE_ONCE (host->suppress_cd, false); + goto no_card; + } + +if (!READ_ONCE (host->suppress_cd)) { host->card_exist = true;return 1 ; }
@@ -874,6 +882,8 @@ static void sdmmc_request(struct mmc_host *mmc, struct mmc_request *mrq)* detect card when fail to update card existence state and * speed up card removal when retry */ +if (!mmc->card && cmd->error == -ETIMEDOUT ) +WRITE_ONCE (host->suppress_cd, true);sdmmc_get_cd (mmc);dev_dbg (sdmmc_dev (host),"cmd->error = %d\n" , cmd->error); }
@@ -1359,6 +1369,7 @@ static void rtsx_usb_init_host(struct rtsx_usb_sdmmc *host) host->power_mode =MMC_POWER_OFF ; host->ocp_stat =0 ; + host->suppress_cd = false; }static int rtsx_usb_sdmmc_drv_probe (struct platform_device *pdev)
Quoted 50 lines · Sean Rhodes
This series fixes the empty-tray detect loop seen on a Realtek RTS5129 USB SD reader, while also avoiding USB register accesses from the USB runtime autosuspend callback.
Compared with the previous RFC, this no longer qualifies media by issuing SD/MMC commands from ->get_cd(). The
rtsx_usb_sdmmcchild still reports the raw card-detect state initially, then lets the normal MMC rescan path probe the media. If an initialization command times out before a card is attached, the SD child suppresses the rawSD_CDsignal until the tray/card detect line drops again.The parent
rtsx_usbchange removes USB I/O from runtime autosuspend and keeps only the existing Memory Stick autosuspend deferral based on cached card-status bits. RawSD_CDis not used as a parent autosuspend blocker, because this reader can assert it for an empty tray.Runtime-tested on a StarLite Mk V with Realtek RTS5129 (0bda:0129), using these changes as a matched rtsx_usb/rtsx_usb_sdmmc/rtsx_usb_ms module set on Linux 7.0.0-27-generic.
Device enumeration:
Bus 001 Device 003: ID 0bda:0129 Realtek Semiconductor Corp. RTS5129 Card Reader Controller /: Bus 001.Port 001: Dev 001, Class=root_hub, Driver=xhci_hcd/12p, 480M|__ Port 004: Dev 003, If 0, Class=Vendor Specific Class, Driver=rtsx_usb, 480MCard inserted:
mmc0: new UHS-I speed SDR50 SDXC card at address 544c
mmcblk0: mmc0:544c LX2TB 1.82 TiB
mmcblk0: p1Deep S3 with the card inserted:
PM: suspend entry (deep)
ACPI: PM: Waking up from system sleep state S3
PM: suspend exitAfter resume, the card remained visible:
NAME SIZE TYPE FSTYPE MOUNTPOINTS mmcblk0 1.8T disk `-mmcblk0p1 1.8T part exfatEmpty tray:
mmc0: card 544c removed /sys/bus/usb/devices/1-4/power/control=auto /sys/bus/usb/devices/1-4/power/runtime_status=suspended /sys/bus/usb/devices/1-4/power/runtime_usage=0With the empty tray left inserted for about three minutes, no repeated MMC detect attempts, command timeouts, or USB reset churn were observed.
Quoted 9 lines
Sean Rhodes (2):
misc:rtsx_usb: avoid USB I/O in runtime autosuspend
mmc:rtsx_usb_sdmmc: suppress false CD after init timeoutdrivers/misc/cardreader/rtsx_usb.c | 26 ++++++++++++++++++++------ drivers/mmc/host/rtsx_usb_sdmmc.c | 13 ++++++++++++- include/linux/rtsx_usb.h | 3 +++ 3 files changed, 35 insertions(+), 7 deletions(-)
I decided to queue the series for next to get it thoroughly tested. Potentially we should also tag these for stable kernels, but let's get it tested a bit more in next first.
Greg, please let me know if you have any objections that I pick this via my mmc tree, or please provide your ack.
Kind regards
Uffe
Quoted 61 lines · Ulf Hansson
Quoted 50 lines · Sean Rhodes
This series fixes the empty-tray detect loop seen on a Realtek RTS5129 USB SD reader, while also avoiding USB register accesses from the USB runtime autosuspend callback.
Compared with the previous RFC, this no longer qualifies media by issuing SD/MMC commands from ->get_cd(). The
rtsx_usb_sdmmcchild still reports the raw card-detect state initially, then lets the normal MMC rescan path probe the media. If an initialization command times out before a card is attached, the SD child suppresses the rawSD_CDsignal until the tray/card detect line drops again.The parent
rtsx_usbchange removes USB I/O from runtime autosuspend and keeps only the existing Memory Stick autosuspend deferral based on cached card-status bits. RawSD_CDis not used as a parent autosuspend blocker, because this reader can assert it for an empty tray.Runtime-tested on a StarLite Mk V with Realtek RTS5129 (0bda:0129), using these changes as a matched rtsx_usb/rtsx_usb_sdmmc/rtsx_usb_ms module set on Linux 7.0.0-27-generic.
Device enumeration:
Bus 001 Device 003: ID 0bda:0129 Realtek Semiconductor Corp. RTS5129 Card Reader Controller /: Bus 001.Port 001: Dev 001, Class=root_hub, Driver=xhci_hcd/12p, 480M|__ Port 004: Dev 003, If 0, Class=Vendor Specific Class, Driver=rtsx_usb, 480MCard inserted:
mmc0: new UHS-I speed SDR50 SDXC card at address 544c
mmcblk0: mmc0:544c LX2TB 1.82 TiB
mmcblk0: p1Deep S3 with the card inserted:
PM: suspend entry (deep)
ACPI: PM: Waking up from system sleep state S3
PM: suspend exitAfter resume, the card remained visible:
NAME SIZE TYPE FSTYPE MOUNTPOINTS mmcblk0 1.8T disk `-mmcblk0p1 1.8T part exfatEmpty tray:
mmc0: card 544c removed /sys/bus/usb/devices/1-4/power/control=auto /sys/bus/usb/devices/1-4/power/runtime_status=suspended /sys/bus/usb/devices/1-4/power/runtime_usage=0With the empty tray left inserted for about three minutes, no repeated MMC detect attempts, command timeouts, or USB reset churn were observed.
Quoted 9 lines
Sean Rhodes (2):
misc:rtsx_usb: avoid USB I/O in runtime autosuspend
mmc:rtsx_usb_sdmmc: suppress false CD after init timeoutdrivers/misc/cardreader/rtsx_usb.c | 26 ++++++++++++++++++++------ drivers/mmc/host/rtsx_usb_sdmmc.c | 13 ++++++++++++- include/linux/rtsx_usb.h | 3 +++ 3 files changed, 35 insertions(+), 7 deletions(-)
Quoted 6 lines
I decided to queue the series for next to get it thoroughly tested. Potentially we should also tag these for stable kernels, but let's get it tested a bit more in next first.
Greg, please let me know if you have any objections that I pick this via my mmc tree, or please provide your ack.
No objection from me:
Re: [PATCH v2 1/2] misc: rtsx_usb: avoid USB I/O in runtime autosuspend
In reply to Sean RhodesQuoted 8 lines · Sean Rhodes
The runtime autosuspend callback currently queries card status and clears OCP by issuing USB register accesses. This can run from the USB runtime-PM path itself, which is the wrong place to start more device I/O.
Keep a cached copy of the card-status bits from normal status reads instead. During runtime autosuspend, use that cached value only to preserve the existing Memory Stick autosuspend deferral.
How is the driver supposed to detect card insertion after the last poll but before the reader goes to suspend and can issue remote wakeup?
This is exactly what happens now: if I insert a new card within two seconds of the "card removed" message showing up (which itself happens up to a second after removal) the insertion is never detected and the reader enters suspend with the card in.
It seems that not only was "starting more I/O in the runtime-PM path" actually a good idea, but polling should only stop when the parent USB device suspends, not the child MMC host, because there is a two second delay between them. And even with this patch reverted, insertion never works if USB autosuspend is simply disabled altogether.
Also, does anyone know if there is any way to utilize the interrupt endpoint instead of polling? I tried submitting a URB to it and got regular responses, but only zeros. Can this be changed?
Quoted 3 lines
Do not treat raw
SD_CDas an autosuspend blocker, because tray-based SD readers can assertSD_CDwith an empty tray. A real SD card is protected by the SD/MMC child runtime-PM usage once powered.
Sounds like the proper condition is to ignore the card if and only if it failed to initialize and has not been swapped for a new one.
Hence the question about interrupts: this would vastly reduce the risk that a swap for an actual working card remains undetected.
Regards,
Michal
Re: [PATCH v2 1/2] misc: rtsx_usb: avoid USB I/O in runtime autosuspend
Quoted 1 line
How is the driver supposed to detect card insertion after the last poll
but before the reader goes to suspend and can issue remote wakeup?
The reader uses remote wake; what hardware are you testing on?
Quoted 41 lines · Michal Pecio
Quoted 8 lines · Sean Rhodes
The runtime autosuspend callback currently queries card status and clears OCP by issuing USB register accesses. This can run from the USB runtime-PM path itself, which is the wrong place to start more device I/O.
Keep a cached copy of the card-status bits from normal status reads instead. During runtime autosuspend, use that cached value only to preserve the existing Memory Stick autosuspend deferral.
How is the driver supposed to detect card insertion after the last poll but before the reader goes to suspend and can issue remote wakeup?
This is exactly what happens now: if I insert a new card within two seconds of the "card removed" message showing up (which itself happens up to a second after removal) the insertion is never detected and the reader enters suspend with the card in.
It seems that not only was "starting more I/O in the runtime-PM path" actually a good idea, but polling should only stop when the parent USB device suspends, not the child MMC host, because there is a two second delay between them. And even with this patch reverted, insertion never works if USB autosuspend is simply disabled altogether.
Also, does anyone know if there is any way to utilize the interrupt endpoint instead of polling? I tried submitting a URB to it and got regular responses, but only zeros. Can this be changed?
Quoted 3 lines
Do not treat raw
SD_CDas an autosuspend blocker, because tray-based SD readers can assertSD_CDwith an empty tray. A real SD card is protected by the SD/MMC child runtime-PM usage once powered.Sounds like the proper condition is to ignore the card if and only if it failed to initialize and has not been swapped for a new one.
Hence the question about interrupts: this would vastly reduce the risk that a swap for an actual working card remains undetected.
Regards,
Michal
Re: [PATCH v2 1/2] misc: rtsx_usb: avoid USB I/O in runtime autosuspend
Quoted 5 lines · Sean Rhodes
Quoted 3 lines
How is the driver supposed to detect card insertion after the last poll but before the reader goes to suspend and can issue remote wakeup?
The reader uses remote wake; what hardware are you testing on?
I have a card reader with RTS5178 and a simple SD slot, no tray.
Remote wake doesn't work when the reader is not suspended, so before suspending we used to check one last time that no new card has been inserted. This made sense.
Any delay between the last time the reader is polled (by anyone, for any reason) and actually placed in suspend is a time when a new card can be inserted and completely undetected, forever.
Regards,
Michal
Re: [PATCH v2 1/2] misc: rtsx_usb: avoid USB I/O in runtime autosuspend
Quoted 1 line
I have a card reader with RTS5178 and a simple SD slot, no tray.
Thanks, but what is that attached to? i.e. Arrow Lake
Quoted 20 lines · Michal Pecio
Quoted 5 lines · Sean Rhodes
Quoted 3 lines
How is the driver supposed to detect card insertion after the last poll but before the reader goes to suspend and can issue remote wakeup?
The reader uses remote wake; what hardware are you testing on?
I have a card reader with RTS5178 and a simple SD slot, no tray.
Remote wake doesn't work when the reader is not suspended, so before suspending we used to check one last time that no new card has been inserted. This made sense.
Any delay between the last time the reader is polled (by anyone, for any reason) and actually placed in suspend is a time when a new card can be inserted and completely undetected, forever.
Regards,
Michal
Re: [PATCH v2 1/2] misc: rtsx_usb: avoid USB I/O in runtime autosuspend
Quoted 3 lines · Sean Rhodes
Quoted 1 line
I have a card reader with RTS5178 and a simple SD slot, no tray.
Thanks, but what is that attached to? i.e. Arrow Lake
A whole bunch of USB ports on various xHCI and one EHCI controller, none of it makes a difference. For the record, I have no issues with wakeup on insertion when the reader is suspend. The problem is when it goes to suspend with a card already inserted and doesn't wake.
Re: [PATCH v2 1/2] misc: rtsx_usb: avoid USB I/O in runtime autosuspend
Quoted 1 line
For the record, I have no issues with wakeup on insertion when the reader is suspend.
I do, but only on Meteor Lake
Quoted 1 line
The problem is when it goes to suspend with a card already inserted and doesn't wake.
I can't replicate this at all; my "bunch" are all fine (glk through arl, and cezanne through phoenix)
Quoted 1 line
Sounds like the proper condition is to ignore the card if and only if it failed to initialize and has not been swapped for a new one.
I agree; but that approach wasn't accepted.
Quoted 10 lines · Michal Pecio
Quoted 3 lines · Sean Rhodes
Quoted 1 line
I have a card reader with RTS5178 and a simple SD slot, no tray.
Thanks, but what is that attached to? i.e. Arrow Lake
A whole bunch of USB ports on various xHCI and one EHCI controller, none of it makes a difference. For the record, I have no issues with wakeup on insertion when the reader is suspend. The problem is when it goes to suspend with a card already inserted and doesn't wake.
Re: [PATCH v2 1/2] misc: rtsx_usb: avoid USB I/O in runtime autosuspend
Quoted 4 lines · Sean Rhodes
Quoted 2 lines
For the record, I have no issues with wakeup on insertion when the reader is suspend.
I do, but only on Meteor Lake
Sounds like an unrelated USB bug.
Quoted 5 lines
Quoted 2 lines
The problem is when it goes to suspend with a card already
inserted and doesn't wake.I can't replicate this at all; my "bunch" are all fine (glk through arl, and cezanne through phoenix)
Host shouldn't matter in principle. Reproduction is simple:
echo 'module usbcore +p' >/proc/dynamic_debug/control
# remove the card # in less than one second, this appears
[38244.954611] usb 10-1: kworker/0:0 timed out or killed on ep2in len=0/8 [38244.956547] mmc0: card 0260 removed
# MMC host is suspended and polling stops
# for two seconds, card insertion will go unnoticed # the reader enters suspend with a card inserted
[38247.021374] usb 10-1: usb auto-suspend, wakeup 1
# removing the card wakes it up
[38250.163400] hub 10-0:1.0: state 7 ports 4 chg 0000 evt 0002 [38250.174416] usb 10-1: usb wakeup-resume
Regards,
Michal