usb: keep the USB Serial setting across connects

rockbox-gerrit-noreply--- via rockbox-cvs <[email protected]>
Newsgroups gmane.comp.systems.archos.rockbox.cvs
Message-ID <[email protected]>
commit ef20bc4c7877d2397150dbbeecb6576ed90ba408
Author: Michael McAllister <[email protected]>
Date:   Sun Aug 9 23:22:25 2026 +0100

    usb: keep the USB Serial setting across connects
    
    Debug -> USB Serial calls usb_core_enable_driver(USB_DRIVER_SERIAL, ...), which sets a flag inside usb_core. usb_core_init() clears every driver's enabled flag on each connect and runs after the toggle, so the choice is gone before any descriptor is built: the menu reports the new state and nothing changes.
    
    Hold the setting in firmware/usb.c and reapply it from usb_configure_drivers() on each connect, which is where the other drivers are already configured. usb_set_serial()/usb_get_serial() sit alongside usb_set_hid(), which works this way for the same reason.
    
    toggle_usb_core_driver() goes with it: toggle_usb_serial() was its only
    caller, and a generic wrapper around a call that cannot persist is not worth keeping.
    
    Affects any target building USB_ENABLE_SERIAL.
    
    Change-Id: I171a532d23cc9cf0efe0a01c2836b69846333a4d
    Co-Authored-By: Claude Opus 4.8

diff --git a/apps/debug_menu.c b/apps/debug_menu.c
index 2a0f182ed2..031845f32f 100644
--- a/apps/debug_menu.c
+++ b/apps/debug_menu.c
@@ -2537,21 +2537,16 @@ static bool dbg_talk(void)
 
 #ifdef HAVE_USBSTACK
 #if (defined(ROCKBOX_HAS_LOGF) && defined(USB_ENABLE_SERIAL))
-static bool toggle_usb_core_driver(int driver, char *msg)
+#ifdef USB_ENABLE_SERIAL
+static bool toggle_usb_serial(void)
 {
-    bool enabled = !usb_core_driver_enabled(driver);
+    bool enabled = !usb_get_serial();
 
-    usb_core_enable_driver(driver,enabled);
-    splashf(HZ, "%s %s", msg, enabled ? "enabled" : "disabled");
+    usb_set_serial(enabled);
+    splashf(HZ, "USB Serial %s", enabled ? "enabled" : "disabled");
 
     return false;
 }
-
-#ifdef USB_ENABLE_SERIAL
-static bool toggle_usb_serial(void)
-{
-    return toggle_usb_core_driver(USB_DRIVER_SERIAL, "USB Serial");
-}
 #endif /* USB_ENABLE_SERIAL */
 #endif
 
diff --git a/firmware/export/usb.h b/firmware/export/usb.h
index aa1e25c93b..de48c890f9 100644
--- a/firmware/export/usb.h
+++ b/firmware/export/usb.h
@@ -272,6 +272,11 @@ void usb_firewire_connect_event(void);
 void usb_set_hid(bool enable);
 #endif
 
+#ifdef USB_ENABLE_SERIAL
+void usb_set_serial(bool enable);
+bool usb_get_serial(void);
+#endif
+
 #if defined(USB_ENABLE_AUDIO) || defined(HAVE_HOST_USB_AUDIO)
 /* Select when the USB Audio (DAC) function is active. Values follow the
  * usb_audio setting: 0 never, 1 always, 2 while charge-only, 3 while
diff --git a/firmware/usb.c b/firmware/usb.c
index 1e91efd96b..c44a7df7fc 100644
--- a/firmware/usb.c
+++ b/firmware/usb.c
@@ -117,6 +117,9 @@ static struct event_queue usb_queue SHAREDBSS_ATTR;
 #ifdef USB_ENABLE_HID
 static bool usb_hid = true;
 #endif
+#ifdef USB_ENABLE_SERIAL
+static bool usb_serial = false;
+#endif
 #ifdef USB_ENABLE_AUDIO
 static int usb_audio = 0;
 #endif
@@ -215,6 +218,9 @@ static inline void usb_configure_drivers(int for_state)
         usb_core_enable_driver(USB_DRIVER_HID, true);
 #endif /* USB_ENABLE_CHARGING_ONLY */
 #endif /* USB_ENABLE_HID */
+#ifdef USB_ENABLE_SERIAL
+        usb_core_enable_driver(USB_DRIVER_SERIAL, usb_serial);
+#endif
 #ifdef USB_ENABLE_AUDIO
         usb_core_enable_driver(USB_DRIVER_AUDIO, (usb_audio == 1) || (usb_audio == 2)); // while "always" or "only in charge-only mode"
 #endif /* USB_ENABLE_AUDIO */
@@ -237,6 +243,9 @@ static inline void usb_configure_drivers(int for_state)
 #ifdef USB_ENABLE_HID
         usb_core_enable_driver(USB_DRIVER_HID, usb_hid);
 #endif
+#ifdef USB_ENABLE_SERIAL
+        usb_core_enable_driver(USB_DRIVER_SERIAL, usb_serial);
+#endif
 #ifdef USB_ENABLE_AUDIO
         usb_core_enable_driver(USB_DRIVER_AUDIO, (usb_audio == 1) || (usb_audio == 3)); // while "always" or "only in mass-storage mode"
 #endif /* USB_ENABLE_AUDIO */
@@ -890,6 +899,19 @@ void usb_set_hid(bool enable)
 }
 #endif /* USB_ENABLE_HID */
 
+#ifdef USB_ENABLE_SERIAL
+void usb_set_serial(bool enable)
+{
+    usb_serial = enable;
+    usb_core_enable_driver(USB_DRIVER_SERIAL, usb_serial);
+}
+
+bool usb_get_serial(void)
+{
+    return usb_serial;
+}
+#endif /* USB_ENABLE_SERIAL */
+
 #ifdef USB_ENABLE_AUDIO
 void usb_set_audio(int value)
 {
-- 
rockbox-cvs mailing list
[email protected]
https://lists.haxx.se/mailman/listinfo/rockbox-cvs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.