diff options
| author | YP3 Audiobook <noreply@example.invalid> | 2026-11-27 00:51:12 +0000 |
|---|---|---|
| committer | YP3 Audiobook <noreply@example.invalid> | 2026-11-27 00:51:12 +0000 |
| commit | 7a06b5d2d7ac329dc5cba794b019ba185b9b09e2 (patch) | |
| tree | fac537bb029a8532aadec97816292c3074b9e608 /patches/0005-yp3-survive-pulling-the-SD-card-while-running.patch | |
| parent | 4b670f7ac2635de6d6583d603d97f0bcfbec6733 (diff) | |
| download | yp3-audiobook-firmware-7a06b5d2d7ac329dc5cba794b019ba185b9b09e2.tar.gz | |
Release 1.1
- Hold Up/Down to keep changing the volume
- Pulling the SD card powers the player off instead of crashing; with no
card at power-on it says so and turns off
- Long M4B books load without freezing the controls, with a
"Loading book N%" screen
- The Center press that powers the player on no longer pauses the book
as it resumes (which also made it load twice)
- No "Bookmark saved" pause at power-off
- USB disk shows up right away while a book loads (flasher's
"NO USB DISK")
- tests/m4a: host test of the M4B index loader
Diffstat (limited to 'patches/0005-yp3-survive-pulling-the-SD-card-while-running.patch')
| -rw-r--r-- | patches/0005-yp3-survive-pulling-the-SD-card-while-running.patch | 389 |
1 files changed, 389 insertions, 0 deletions
diff --git a/patches/0005-yp3-survive-pulling-the-SD-card-while-running.patch b/patches/0005-yp3-survive-pulling-the-SD-card-while-running.patch new file mode 100644 index 0000000..3d27689 --- /dev/null +++ b/patches/0005-yp3-survive-pulling-the-SD-card-while-running.patch @@ -0,0 +1,389 @@ +From 27005a02eebd02cf82f357917019f72db9045af7 Mon Sep 17 00:00:00 2001 +From: YP3 Audiobook <noreply@example.invalid> +Date: Thu, 26 Nov 2026 13:52:50 -0500 +Subject: [PATCH] yp3: survive pulling the SD card while running + +The driver gave up on the first failed transfer, so a card pulled and +put back stayed dead, and the next write (at power-off at the latest) +panicked in fat.c ("Could not write sector"). + +Now a failed transfer sets the card up again and retries if it is the +same card (CID). The power thread checks an idle card every half second. +A card that is gone pauses playback with a message, writes are dropped +instead of panicking, and the player powers off 4 s later. Turned on +again with the card in, it resumes the book from its last saved place. + +(Restarting with system_reboot() once the card was back left a player +dark and unresponsive; power_off() is the tested shutdown path.) + +--- + apps/misc.c | 10 ++ + firmware/common/fat.c | 9 ++ + firmware/export/config/yp3.h | 4 + + firmware/export/sd.h | 5 + + firmware/powermgmt.c | 7 + + firmware/target/arm/sl6801/yp3/sd-yp3.c | 165 ++++++++++++++++++++++-- + 6 files changed, 186 insertions(+), 14 deletions(-) + +diff --git a/apps/misc.c b/apps/misc.c +index a5762bb3..a487c050 100644 +--- a/apps/misc.c ++++ b/apps/misc.c +@@ -719,6 +719,16 @@ long default_event_handler_ex(long event, void (*callback)(void *), void *parame + unpause_action(true); + return SYS_CAR_ADAPTER_RESUME; + #endif ++#ifdef HAVE_SD_CARD_SWAP ++ case SYS_HOTSWAP_EXTRACTED: ++ /* The card was pulled (sd-yp3.c): hold the book where it is. ++ Nothing can be read or saved; the player powers off a few ++ seconds later. */ ++ if (audio_status() & AUDIO_STATUS_PLAY) ++ audio_pause(); ++ splash(3*HZ, "SD card removed. Turning off."); ++ return SYS_HOTSWAP_EXTRACTED; ++#endif + #ifdef HAVE_DEVLOG + case SYS_DEV_RUN: /* 0 for a UI ping: nothing ran */ + return dev_ui_run() ? SYS_DEV_RUN : 0; +diff --git a/firmware/common/fat.c b/firmware/common/fat.c +index 3953f46b..4523c327 100644 +--- a/firmware/common/fat.c ++++ b/firmware/common/fat.c +@@ -38,6 +38,9 @@ + #include "kernel.h" + /*#define LOGF_ENABLE*/ + #include "logf.h" ++#ifdef HAVE_SD_CARD_SWAP ++#include <errno.h> ++#endif + + #define BYTES2INT32(array, pos) \ + (((uint32_t)array[pos+0] << 0) | \ +@@ -394,6 +397,12 @@ void dc_writeback_callback(IF_MV(int volume,) sector_t sector, void *buf) + while (1) + { + int rc = storage_write_sectors(IF_MD(fat_bpb->drive,) sector, 1, buf); ++#ifdef HAVE_SD_CARD_SWAP ++ /* The card is out: nothing to write to, and the player powers off ++ before it is used again (sd-yp3.c) */ ++ if (rc == -ENODEV) ++ return; ++#endif + if (rc < 0) + { + panicf("%s() - Could not write sector %llu" +diff --git a/firmware/export/config/yp3.h b/firmware/export/config/yp3.h +index d3e5742d..ec0ccdab 100644 +--- a/firmware/export/config/yp3.h ++++ b/firmware/export/config/yp3.h +@@ -22,6 +22,10 @@ + #endif + /* Keep FAT sector aggregation within the YP3 adapter's 128-sector cap. */ + #define FAT_MAX_TRANSFER_SIZE 128 ++/* Pulling the card while running (sd-yp3.c): pushed straight back it ++ * carries on, otherwise playback pauses, writes are dropped instead of ++ * panicking, and the player powers off a few seconds later. */ ++#define HAVE_SD_CARD_SWAP + #endif + #define CONFIG_CPU SL6801 + #define CPU_FREQ 192000000 +diff --git a/firmware/export/sd.h b/firmware/export/sd.h +index 61cc513c..5cf229a2 100644 +--- a/firmware/export/sd.h ++++ b/firmware/export/sd.h +@@ -55,6 +55,11 @@ bool sd_present(IF_MD_NONVOID(int drive)); + + long sd_last_disk_activity(void); + ++#ifdef HAVE_SD_CARD_SWAP ++/* From the power thread: notice a pulled card, then power off */ ++void sd_card_poll(void); ++#endif ++ + #ifdef CONFIG_STORAGE_MULTI + int sd_num_drives(int first_drive); + #endif +diff --git a/firmware/powermgmt.c b/firmware/powermgmt.c +index 1441fa12..869de8bd 100644 +--- a/firmware/powermgmt.c ++++ b/firmware/powermgmt.c +@@ -32,6 +32,9 @@ + #include "audio.h" + #include "usb.h" + #include "powermgmt.h" ++#ifdef HAVE_SD_CARD_SWAP ++#include "sd.h" ++#endif + #include "backlight.h" + #include "lcd.h" + #include "rtc.h" +@@ -840,6 +843,10 @@ static void power_thread(void) + /* Perform target tasks */ + charging_algorithm_step(); + ++#ifdef HAVE_SD_CARD_SWAP ++ sd_card_poll(); ++#endif ++ + /* check if some idle or sleep timer wears off */ + handle_auto_poweroff(); + +diff --git a/firmware/target/arm/sl6801/yp3/sd-yp3.c b/firmware/target/arm/sl6801/yp3/sd-yp3.c +index 1ecc5844..5187ef76 100644 +--- a/firmware/target/arm/sl6801/yp3/sd-yp3.c ++++ b/firmware/target/arm/sl6801/yp3/sd-yp3.c +@@ -18,6 +18,9 @@ + #include "sd.h" + #include "sdmmc.h" + #include "storage.h" ++#ifdef HAVE_SD_CARD_SWAP ++#include "power.h" ++#endif + #include "gpio-yp3.h" + #include "mmio-sl6801.h" + +@@ -219,7 +222,10 @@ enum { + SD_E_R1 = -5, SD_E_FIFO = -6, SD_E_STATE = -7, SD_E_UNSUPPORTED = -8, + }; + +-enum sd_state { STATE_UNINIT, STATE_READY, STATE_FAILED }; ++/* REMOVED (HAVE_SD_CARD_SWAP): the card stopped answering and a fresh ++ * setup found no card, or a different one; sd_card_poll() powers the ++ * player off shortly after. */ ++enum sd_state { STATE_UNINIT, STATE_READY, STATE_FAILED, STATE_REMOVED }; + + static struct mutex sd_mutex; + static tCardInfo card_info; +@@ -228,6 +234,13 @@ static bool sd_busy; + static long last_activity; + static uint32_t card_sectors, card_rca; + static bool card_sector_addressed; ++#ifdef HAVE_SD_CARD_SWAP ++/* CID (manufacturer, product, serial) of the card set up at boot, and of ++ * the one the last cold_prepare() found */ ++static uint32_t card_cid[4], probe_cid[4]; ++/* When the card was found gone (STATE_REMOVED) */ ++static long removed_tick; ++#endif + + /* ------------------------------------------------------------------ */ + /* Bounded polling of the data-path controller state. */ +@@ -761,6 +774,11 @@ static int cold_prepare(void) + ccs = (response >> OCR_CCS_SHIFT) & 1u; + status = cold_command(2, 0, true, &response); + if (status) return status; ++#ifdef HAVE_SD_CARD_SWAP ++ /* R2, highest word at the highest response address, as the CSD */ ++ for (i = 0; i < 4u; i++) ++ probe_cid[i] = REG(SD_RESP0 + (3u - i) * 4u); ++#endif + status = cold_command(3, 0, true, &response); + if (status) return status; + rca = response >> RCA_SHIFT; /* R6: new RCA in bits 31:16 */ +@@ -830,6 +848,113 @@ static int bind_card(void) + return controller_idle() ? 0 : SD_E_STATE; + } + ++#ifdef HAVE_SD_CARD_SWAP ++/* ------------------------------------------------------------------ */ ++/* Card pulled while running. The card holds everything but the core and ++ * codecs, and Rockbox cannot unmount its system volume, so: a card that ++ * is pulled and pushed straight back during a transfer carries on (same ++ * CID, set up again); otherwise I/O stops (writes are dropped, fat.c), ++ * playback pauses with a message, and the player powers off a few seconds ++ * later, so nothing runs on with stale filesystem state. Turned on again ++ * with the card in, it resumes the book from its last saved place. ++ * Upstream gave up on the first failed transfer, and the next write ++ * (at power-off at the latest) panicked in fat.c. ++ * ++ * A first version restarted the player with system_reboot() (PMU software ++ * reset) once a card was back; on a player that left it dark and ++ * unresponsive, needing a reflash (2026-11-26). power_off() is the path ++ * every normal shutdown takes. */ ++ ++/* Idle time before the poll checks the card is still there */ ++#define CARD_POLL_IDLE_TICKS HZ ++/* Card gone to power-off: long enough to read the message (misc.c) */ ++#define CARD_REMOVED_OFF_TICKS (4*HZ) ++ ++/* Set up the card from scratch (mutex held). True if the card we booted ++ * with is ready again. */ ++static bool card_restart(void) ++{ ++ int rc = cold_prepare(); ++ if (rc == 0) ++ rc = bind_card(); ++#ifdef YP3_DEV ++ dev_log("sd restart: rc %d", rc); ++#endif ++ return rc == 0 && !memcmp(probe_cid, card_cid, sizeof(card_cid)); ++} ++ ++/* Mutex held, sd_state READY: the card stopped answering. True if it is ++ * back (retry the transfer); else it is out (or another card is in) and ++ * everyone is told. */ ++static bool card_recover(void) ++{ ++ if (card_restart()) ++ return true; ++ removed_tick = current_tick; ++ sd_state = STATE_REMOVED; ++ card_info.initialized = 0; ++ queue_broadcast(SYS_HOTSWAP_EXTRACTED, 0); ++ return false; ++} ++ ++/* Is the card still selected and answering? (mutex held) */ ++static bool card_answers(void) ++{ ++ struct op op = { 0u, POLL_LIMIT }; ++ uint32_t response = 0u; ++ if (!controller_idle()) ++ return false; ++ return send_command(&op, CMD13_PACKED, card_rca << RCA_SHIFT, 13u, true, ++ &response) == 0; ++} ++ ++/* Power thread, every POWER_THREAD_STEP_TICKS: notice a pulled card while ++ * nothing reads or writes, and power off a few seconds after. */ ++void sd_card_poll(void) ++{ ++ if (sd_state == STATE_READY) { ++ if (sd_busy || ++ !TIME_AFTER(current_tick, last_activity + CARD_POLL_IDLE_TICKS)) ++ return; ++ mutex_lock(&sd_mutex); ++ if (sd_state == STATE_READY && !card_answers()) ++ card_recover(); ++ mutex_unlock(&sd_mutex); ++ } else if (sd_state == STATE_REMOVED && ++ TIME_AFTER(current_tick, removed_tick + CARD_REMOVED_OFF_TICKS)) { ++ /* Straight to the hardware off: the normal shutdown would save ++ * settings and bookmarks to a card that is not there. */ ++ power_off(); ++ } ++} ++#endif /* HAVE_SD_CARD_SWAP */ ++ ++/* A transfer failed (mutex held): true to retry it once, the card having ++ * been set up again. */ ++static bool transfer_failed(bool retried) ++{ ++#ifdef HAVE_SD_CARD_SWAP ++ if (!retried) ++ return card_recover(); ++#else ++ (void)retried; ++#endif ++ return false; ++} ++ ++/* After the transfers (mutex held): a failure that left the card in place ++ * leaves the controller in an unknown state. */ ++static int transfer_result(int rc) ++{ ++ if (rc == 0) ++ return 0; ++ if (sd_state == STATE_READY) { ++ sd_state = STATE_FAILED; ++ card_info.initialized = 0; ++ } ++ return sd_state == STATE_FAILED ? -EIO : -ENODEV; ++} ++ + /* ------------------------------------------------------------------ */ + /* Rockbox storage API. */ + +@@ -850,6 +975,9 @@ int sd_init(void) + sd_state = STATE_FAILED; + return -EIO; + } ++#ifdef HAVE_SD_CARD_SWAP ++ memcpy(card_cid, probe_cid, sizeof(card_cid)); ++#endif + card_info.initialized = 1; + card_info.blocksize = SD_BLOCK_SIZE; + card_info.current_bank = 0; +@@ -885,7 +1013,8 @@ int sd_read_sectors(IF_MD(int drive,) sector_t start, int count, void *buffer) + #ifdef YP3_DEV + uint32_t t0 = sl6801_us(); + #endif +- for (int i = 0; i < count && rc == 0; ) { ++ bool retried = false; ++ for (int i = 0; i < count; ) { + unsigned n; + if ((uintptr_t)out & 3u) { + n = MIN((unsigned)(count - i), BOUNCE_BLOCKS); +@@ -905,6 +1034,13 @@ int sd_read_sectors(IF_MD(int drive,) sector_t start, int count, void *buffer) + (unsigned long)REG(SD_DMA_CTRL), + (unsigned long)REG(SD_DMA_STATUS)); + #endif ++ if (rc) { ++ if (!transfer_failed(retried)) ++ break; ++ retried = true; ++ rc = 0; ++ continue; ++ } + i += n; + out += n * SD_BLOCK_SIZE; + } +@@ -916,13 +1052,9 @@ int sd_read_sectors(IF_MD(int drive,) sector_t start, int count, void *buffer) + if (rc == 0) + last_activity = current_tick; + sd_busy = false; +- if (rc) { +- /* A failed transfer leaves the controller in an unknown state. */ +- sd_state = STATE_FAILED; +- card_info.initialized = 0; +- } ++ rc = transfer_result(rc); + mutex_unlock(&sd_mutex); +- return rc ? -EIO : 0; ++ return rc; + } + + int sd_write_sectors(IF_MD(int drive,) sector_t start, int count, +@@ -946,7 +1078,8 @@ int sd_write_sectors(IF_MD(int drive,) sector_t start, int count, + #ifdef YP3_DEV + uint32_t t0 = sl6801_us(); + #endif +- for (int i = 0; i < count && rc == 0; ) { ++ bool retried = false; ++ for (int i = 0; i < count; ) { + unsigned n; + if ((uintptr_t)in & 3u) { + n = MIN((unsigned)(count - i), BOUNCE_BLOCKS); +@@ -963,6 +1096,13 @@ int sd_write_sectors(IF_MD(int drive,) sector_t start, int count, + (unsigned long)REG(SD_EVENT), + (unsigned long)REG(SD_STATUS)); + #endif ++ if (rc) { ++ if (!transfer_failed(retried)) ++ break; ++ retried = true; ++ rc = 0; ++ continue; ++ } + i += n; + in += n * SD_BLOCK_SIZE; + } +@@ -973,12 +1113,9 @@ int sd_write_sectors(IF_MD(int drive,) sector_t start, int count, + if (rc == 0) + last_activity = current_tick; + sd_busy = false; +- if (rc) { +- sd_state = STATE_FAILED; +- card_info.initialized = 0; +- } ++ rc = transfer_result(rc); + mutex_unlock(&sd_mutex); +- return rc ? -EIO : 0; ++ return rc; + } + + bool sd_disk_is_active(void) +-- +2.56.1 + |
