pass event data to usb_acknowledge

add second argument to usb_acknowledge.
it can be used for more appropriate connection tracking that does not
rely on timeout in the future.

Change-Id: I8a44366b7c7a1f944524c4ba8ecd6d9673746a65
This commit is contained in:
mojyack 2026-01-15 16:46:03 +09:00 committed by Solomon Peachy
parent d5506dfa22
commit 1951c17e0b
33 changed files with 54 additions and 46 deletions

View file

@ -101,7 +101,7 @@ static void NORETURN_ATTR audio_thread(void)
case SYS_USB_CONNECTED: case SYS_USB_CONNECTED:
LOGFQUEUE("audio < SYS_USB_CONNECTED"); LOGFQUEUE("audio < SYS_USB_CONNECTED");
voice_stop(); voice_stop();
usb_acknowledge(SYS_USB_CONNECTED_ACK); usb_acknowledge(SYS_USB_CONNECTED_ACK, ev.data);
usb_wait_for_disconnect(&audio_queue); usb_wait_for_disconnect(&audio_queue);
break; break;
} }

View file

@ -248,7 +248,7 @@ static void usb_screens_draw(struct usb_screen_vps_t *usb_screen_vps_ar)
} }
} }
void gui_usb_screen_run(bool early_usb) void gui_usb_screen_run(bool early_usb, intptr_t seqnum)
{ {
#ifdef SIMULATOR /* the sim allows toggling USB fast enough to overflow viewportmanagers stack */ #ifdef SIMULATOR /* the sim allows toggling USB fast enough to overflow viewportmanagers stack */
static bool in_usb_screen = false; static bool in_usb_screen = false;
@ -297,7 +297,7 @@ void gui_usb_screen_run(bool early_usb)
font_disable_all(); font_disable_all();
} }
usb_acknowledge(SYS_USB_CONNECTED_ACK); usb_acknowledge(SYS_USB_CONNECTED_ACK, seqnum);
while (1) while (1)
{ {

View file

@ -21,10 +21,12 @@
#ifndef _USB_SCREEN_H_ #ifndef _USB_SCREEN_H_
#define _USB_SCREEN_H_ #define _USB_SCREEN_H_
#include <stdint.h>
#ifdef USB_NONE #ifdef USB_NONE
#define gui_usb_screen_run(early_usb) do {} while(0) #define gui_usb_screen_run(early_usb, seqnum) do {} while(0)
#else #else
extern void gui_usb_screen_run(bool early_usb); extern void gui_usb_screen_run(bool early_usb, intptr_t seqnum);
#endif #endif
#endif #endif

View file

@ -404,7 +404,7 @@ static void iap_thread(void)
/* Ack USB thread */ /* Ack USB thread */
case SYS_USB_CONNECTED: case SYS_USB_CONNECTED:
{ {
usb_acknowledge(SYS_USB_CONNECTED_ACK); usb_acknowledge(SYS_USB_CONNECTED_ACK, ev.data);
break; break;
} }
} }

View file

@ -618,7 +618,7 @@ static void init(void)
(mmc_remove_request() == SYS_HOTSWAP_EXTRACTED)) (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
#endif #endif
{ {
gui_usb_screen_run(true); gui_usb_screen_run(true, button_get_data());
mounted = true; /* mounting done @ end of USB mode */ mounted = true; /* mounting done @ end of USB mode */
} }
#ifdef HAVE_USB_POWER #ifdef HAVE_USB_POWER
@ -675,7 +675,7 @@ static void init(void)
#ifndef USB_NONE #ifndef USB_NONE
usb_start_monitoring(); usb_start_monitoring();
while(button_get(true) != SYS_USB_CONNECTED) {}; while(button_get(true) != SYS_USB_CONNECTED) {};
gui_usb_screen_run(true); gui_usb_screen_run(true, button_get_data());
#elif !defined(DEBUG) && !(CONFIG_STORAGE & STORAGE_RAMDISK) #elif !defined(DEBUG) && !(CONFIG_STORAGE & STORAGE_RAMDISK)
sleep(HZ*5); sleep(HZ*5);
#endif #endif

View file

@ -656,24 +656,25 @@ long default_event_handler_ex(long event, void (*callback)(void *), void *parame
} }
break; break;
case SYS_USB_CONNECTED: case SYS_USB_CONNECTED:
{
intptr_t seqnum = button_get_data();
if (callback != NULL) if (callback != NULL)
callback(parameter); callback(parameter);
{ system_flush();
system_flush();
#ifdef BOOTFILE #ifdef BOOTFILE
#if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF) #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
check_bootfile(false); /* gets initial size */ check_bootfile(false); /* gets initial size */
#endif #endif
#endif #endif
gui_usb_screen_run(false); gui_usb_screen_run(false, seqnum);
#ifdef BOOTFILE #ifdef BOOTFILE
#if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF) #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
check_bootfile(true); check_bootfile(true);
#endif #endif
#endif #endif
system_restore(); system_restore();
}
return SYS_USB_CONNECTED; return SYS_USB_CONNECTED;
}
case SYS_POWEROFF: case SYS_POWEROFF:
case SYS_REBOOT: case SYS_REBOOT:

View file

@ -1882,7 +1882,7 @@ static void dc_thread_playlist(void)
} }
case SYS_USB_CONNECTED: case SYS_USB_CONNECTED:
usb_acknowledge(SYS_USB_CONNECTED_ACK); usb_acknowledge(SYS_USB_CONNECTED_ACK, ev.data);
usb_wait_for_disconnect(&playlist_queue); usb_wait_for_disconnect(&playlist_queue);
break; break;
} }

View file

@ -948,7 +948,7 @@ struct plugin_api {
/* usb */ /* usb */
bool (*usb_inserted)(void); bool (*usb_inserted)(void);
void (*usb_acknowledge)(long id); void (*usb_acknowledge)(long id, intptr_t seqnum);
#ifdef USB_ENABLE_HID #ifdef USB_ENABLE_HID
void (*usb_hid_send)(usage_page_t usage_page, int id); void (*usb_hid_send)(usage_page_t usage_page, int id);
#endif #endif

View file

@ -435,7 +435,7 @@ void thread(void)
switch (ev.id) switch (ev.id)
{ {
case SYS_USB_CONNECTED: case SYS_USB_CONNECTED:
rb->usb_acknowledge(SYS_USB_CONNECTED_ACK); rb->usb_acknowledge(SYS_USB_CONNECTED_ACK, ev.data);
in_usb = true; in_usb = true;
break; break;
case SYS_USB_DISCONNECTED: case SYS_USB_DISCONNECTED:

View file

@ -491,7 +491,7 @@ static void thread(void)
{ {
case SYS_USB_CONNECTED: case SYS_USB_CONNECTED:
in_usb_mode = true; in_usb_mode = true;
rb->usb_acknowledge(SYS_USB_CONNECTED_ACK); rb->usb_acknowledge(SYS_USB_CONNECTED_ACK, ev.data);
break; break;
case SYS_USB_DISCONNECTED: case SYS_USB_DISCONNECTED:
in_usb_mode = false; in_usb_mode = false;

View file

@ -979,7 +979,7 @@ static void thread(void)
switch (ev.id) switch (ev.id)
{ {
case SYS_USB_CONNECTED: case SYS_USB_CONNECTED:
rb->usb_acknowledge(SYS_USB_CONNECTED_ACK); rb->usb_acknowledge(SYS_USB_CONNECTED_ACK, ev.data);
logenabled = false; logenabled = false;
break; break;
case SYS_USB_DISCONNECTED: case SYS_USB_DISCONNECTED:

View file

@ -58,7 +58,7 @@ static void main_loop(void)
state = "connected"; state = "connected";
logf("test_usb: connect ack %ld", *rb->current_tick); logf("test_usb: connect ack %ld", *rb->current_tick);
DEBUGF("test_usb: connect ack %ld\n", *rb->current_tick); DEBUGF("test_usb: connect ack %ld\n", *rb->current_tick);
rb->usb_acknowledge(SYS_USB_CONNECTED_ACK); rb->usb_acknowledge(SYS_USB_CONNECTED_ACK, ev.data);
break; break;
case SYS_USB_DISCONNECTED: case SYS_USB_DISCONNECTED:

View file

@ -5357,7 +5357,7 @@ static void tagcache_thread(void)
case SYS_USB_CONNECTED: case SYS_USB_CONNECTED:
logf("USB: TagCache"); logf("USB: TagCache");
usb_acknowledge(SYS_USB_CONNECTED_ACK); usb_acknowledge(SYS_USB_CONNECTED_ACK, ev.data);
usb_wait_for_disconnect(&tagcache_queue); usb_wait_for_disconnect(&tagcache_queue);
break ; break ;
} }

View file

@ -470,7 +470,7 @@ void main(void)
case SYS_USB_CONNECTED: case SYS_USB_CONNECTED:
go_active(); go_active();
usb_acknowledge(SYS_USB_CONNECTED_ACK); usb_acknowledge(SYS_USB_CONNECTED_ACK, button_get_data());
break; break;
} }
} }

View file

@ -123,7 +123,7 @@ static void handle_usb(int connect_timeout)
/* Got the message - wait for disconnect */ /* Got the message - wait for disconnect */
printf("Bootloader USB mode"); printf("Bootloader USB mode");
usb_acknowledge(SYS_USB_CONNECTED_ACK); usb_acknowledge(SYS_USB_CONNECTED_ACK, button_get_data());
while (1) while (1)
{ {

View file

@ -96,7 +96,7 @@ static void usb_mode(int connect_timeout)
adc_init(); adc_init();
/* ack the SYS_USB_CONNECTED polled from the button queue */ /* ack the SYS_USB_CONNECTED polled from the button queue */
usb_acknowledge(SYS_USB_CONNECTED_ACK); usb_acknowledge(SYS_USB_CONNECTED_ACK, button_get_data());
while(1) while(1)
{ {

View file

@ -129,7 +129,7 @@ static void usb_mode(void)
printf("Bootloader USB mode"); printf("Bootloader USB mode");
/* Ack the SYS_USB_CONNECTED polled from the button queue */ /* Ack the SYS_USB_CONNECTED polled from the button queue */
usb_acknowledge(SYS_USB_CONNECTED_ACK); usb_acknowledge(SYS_USB_CONNECTED_ACK, button_get_data());
while(1) while(1)
{ {

View file

@ -230,7 +230,7 @@ static int handle_usb(int connect_timeout)
printf("Bootloader USB mode"); printf("Bootloader USB mode");
usb = USB_HANDLED; usb = USB_HANDLED;
usb_acknowledge(SYS_USB_CONNECTED_ACK); usb_acknowledge(SYS_USB_CONNECTED_ACK, ev.data);
#if defined(SANSA_E200) && defined(HAVE_BOOTLOADER_USB_MODE) #if defined(SANSA_E200) && defined(HAVE_BOOTLOADER_USB_MODE)
/* E200 misses unplug randomly /* E200 misses unplug randomly
probably fine for other targets too but needs tested */ probably fine for other targets too but needs tested */

View file

@ -75,7 +75,7 @@ static void usb_mode(void)
/* Got the message - wait for disconnect */ /* Got the message - wait for disconnect */
show_splash(0, "Bootloader USB mode"); show_splash(0, "Bootloader USB mode");
usb_acknowledge(SYS_USB_CONNECTED_ACK); usb_acknowledge(SYS_USB_CONNECTED_ACK, button_get_data());
while (1) while (1)
{ {

View file

@ -196,7 +196,7 @@ static void handle_usb(int connect_timeout)
if (button_get_w_tmo(HZ/2) == SYS_USB_CONNECTED) if (button_get_w_tmo(HZ/2) == SYS_USB_CONNECTED)
{ {
printf("Bootloader USB mode"); printf("Bootloader USB mode");
usb_acknowledge(SYS_USB_CONNECTED_ACK); usb_acknowledge(SYS_USB_CONNECTED_ACK, button_get_data());
while (button_get_w_tmo(HZ/2) != SYS_USB_DISCONNECTED) while (button_get_w_tmo(HZ/2) != SYS_USB_DISCONNECTED)
{ {
storage_spin(); storage_spin();

View file

@ -34,6 +34,7 @@
static bool lcd_inited = false; static bool lcd_inited = false;
extern bool is_usb_connected; extern bool is_usb_connected;
extern intptr_t usb_connection_seqnum;
void clearscreen(void) void clearscreen(void)
{ {
@ -134,6 +135,7 @@ int get_button(int timeout)
case SYS_USB_CONNECTED: case SYS_USB_CONNECTED:
case SYS_USB_DISCONNECTED: case SYS_USB_DISCONNECTED:
is_usb_connected = (btn == SYS_USB_CONNECTED); is_usb_connected = (btn == SYS_USB_CONNECTED);
usb_connection_seqnum = button_get_data();
break; break;
#ifdef HAVE_SCREENDUMP #ifdef HAVE_SCREENDUMP
case BL_SCREENSHOT: case BL_SCREENSHOT:

View file

@ -38,6 +38,7 @@
* Handled by the gui code since that's how events are delivered * Handled by the gui code since that's how events are delivered
* TODO: this is an ugly kludge */ * TODO: this is an ugly kludge */
bool is_usb_connected = false; bool is_usb_connected = false;
intptr_t usb_connection_seqnum = 0;
static bool screenshot_enabled = false; static bool screenshot_enabled = false;
@ -71,7 +72,7 @@ void usb_mode(void)
return; return;
splashf(0, "USB mode"); splashf(0, "USB mode");
usb_acknowledge(SYS_USB_CONNECTED_ACK); usb_acknowledge(SYS_USB_CONNECTED_ACK, usb_connection_seqnum);
while(is_usb_connected) while(is_usb_connected)
get_button(TIMEOUT_BLOCK); get_button(TIMEOUT_BLOCK);

View file

@ -105,7 +105,7 @@ static void usb_mode(void)
/* Got the message - wait for disconnect */ /* Got the message - wait for disconnect */
show_splash(0, "Bootloader USB mode"); show_splash(0, "Bootloader USB mode");
usb_acknowledge(SYS_USB_CONNECTED_ACK); usb_acknowledge(SYS_USB_CONNECTED_ACK, button_get_data());
while(1) { while(1) {
button = button_get_w_tmo(HZ/2); button = button_get_w_tmo(HZ/2);

View file

@ -2996,9 +2996,10 @@ void unregister_storage_idle_func(void (*function)(void), bool run)
\param run \param run
\description \description
void usb_acknowledge(long id) void usb_acknowledge(long id, intptr_t seqnum)
\group usb \group usb
\param id \param id
\param seqnum
\description \description
void usb_hid_send(usage_page_t usage_page, int id) void usb_hid_send(usage_page_t usage_page, int id)

View file

@ -605,7 +605,7 @@ void backlight_thread(void)
#endif /* HAVE_REMOTE_LCD/ HAVE_REMOTE_LCD_AS_MAIN */ #endif /* HAVE_REMOTE_LCD/ HAVE_REMOTE_LCD_AS_MAIN */
#endif /* !SIMULATOR */ #endif /* !SIMULATOR */
case SYS_USB_CONNECTED: case SYS_USB_CONNECTED:
usb_acknowledge(SYS_USB_CONNECTED_ACK); usb_acknowledge(SYS_USB_CONNECTED_ACK, ev.data);
break; break;
#ifdef BACKLIGHT_DRIVER_CLOSE #ifdef BACKLIGHT_DRIVER_CLOSE

View file

@ -70,8 +70,8 @@
* mass storage mode, it will require exclusive access to the disk and ask all * mass storage mode, it will require exclusive access to the disk and ask all
* threads to release any file handle and stop using the disks. It does so by * threads to release any file handle and stop using the disks. It does so by
* broadcasting a SYS_USB_CONNECTED message, which threads must acknowledge using * broadcasting a SYS_USB_CONNECTED message, which threads must acknowledge using
* usb_acknowledge(SYS_USB_CONNECTED_ACK). They must not access the disk until * usb_acknowledge(SYS_USB_CONNECTED_ACK, ev.data). They must not access the disk
* SYS_USB_DISCONNECTED is broadcast. To ease waiting, threads can call * until SYS_USB_DISCONNECTED is broadcast. To ease waiting, threads can call
* usb_wait_for_disconnect() or usb_wait_for_disconnect_w_tmo() on their waiting * usb_wait_for_disconnect() or usb_wait_for_disconnect_w_tmo() on their waiting
* queue. * queue.
* *
@ -198,7 +198,7 @@ void usb_attach(void);
void usb_start_monitoring(void) INIT_ATTR; void usb_start_monitoring(void) INIT_ATTR;
void usb_close(void); void usb_close(void);
/* acknowledge usb connection, typically with SYS_USB_CONNECTED_ACK */ /* acknowledge usb connection, typically with SYS_USB_CONNECTED_ACK */
void usb_acknowledge(long id); void usb_acknowledge(long id, intptr_t seqnum);
/* block the current thread until SYS_USB_DISCONNECTED has been broadcast */ /* block the current thread until SYS_USB_DISCONNECTED has been broadcast */
void usb_wait_for_disconnect(struct event_queue *q); void usb_wait_for_disconnect(struct event_queue *q);
/* same as usb_wait_for_disconnect() but with a timeout, returns 1 on timeout */ /* same as usb_wait_for_disconnect() but with a timeout, returns 1 on timeout */

View file

@ -89,7 +89,7 @@ static bool scroll_process_message(int delay)
case SYS_TIMEOUT: case SYS_TIMEOUT:
return false; return false;
case SYS_USB_CONNECTED: case SYS_USB_CONNECTED:
usb_acknowledge(SYS_USB_CONNECTED_ACK); usb_acknowledge(SYS_USB_CONNECTED_ACK, ev.data);
usb_wait_for_disconnect(&scroll_queue); usb_wait_for_disconnect(&scroll_queue);
sync_display_ticks(); sync_display_ticks();
return true; return true;

View file

@ -236,7 +236,7 @@ static void NORETURN_ATTR storage_thread(void)
storage_event_send(CONFIG_STORAGE, ev.id, (intptr_t)&bdcast); storage_event_send(CONFIG_STORAGE, ev.id, (intptr_t)&bdcast);
usb_mode = ev.id == SYS_USB_CONNECTED; usb_mode = ev.id == SYS_USB_CONNECTED;
if (usb_mode) { if (usb_mode) {
usb_acknowledge(SYS_USB_CONNECTED_ACK); usb_acknowledge(SYS_USB_CONNECTED_ACK, ev.data);
} }
else { else {
bdcast = CONFIG_STORAGE; bdcast = CONFIG_STORAGE;

View file

@ -108,7 +108,7 @@ static void piezo_thread(void)
/*logf("USB: Piezo core");*/ /*logf("USB: Piezo core");*/
piezo_hw_stop(); piezo_hw_stop();
queue_clear(&piezo_queue); queue_clear(&piezo_queue);
usb_acknowledge(SYS_USB_CONNECTED_ACK); usb_acknowledge(SYS_USB_CONNECTED_ACK, ev.data);
usb_wait_for_disconnect(&piezo_queue); usb_wait_for_disconnect(&piezo_queue);
break ; break ;
#endif #endif

View file

@ -838,7 +838,7 @@ void avr_thread(void)
if (ev.id == SYS_USB_CONNECTED) if (ev.id == SYS_USB_CONNECTED)
{ {
/* Allow USB to gain exclusive storage access */ /* Allow USB to gain exclusive storage access */
usb_acknowledge(SYS_USB_CONNECTED_ACK); usb_acknowledge(SYS_USB_CONNECTED_ACK, ev.data);
disk_access_available = false; disk_access_available = false;
} }
else if (ev.id == SYS_USB_DISCONNECTED) else if (ev.id == SYS_USB_DISCONNECTED)

View file

@ -747,7 +747,7 @@ void usb_test(void)
usb_init(); usb_init();
usb_start_monitoring(); usb_start_monitoring();
usb_acknowledge(SYS_USB_CONNECTED_ACK); usb_acknowledge(SYS_USB_CONNECTED_ACK, 0);
while (1) { while (1) {
sleep(HZ); sleep(HZ);

View file

@ -765,9 +765,9 @@ void usb_start_monitoring(void)
#endif /* USB_STATUS_BY_EVENT */ #endif /* USB_STATUS_BY_EVENT */
#endif /* USB_FULL_INIT */ #endif /* USB_FULL_INIT */
void usb_acknowledge(long id) void usb_acknowledge(long id, intptr_t seqnum)
{ {
queue_post(&usb_queue, id, 0); queue_post(&usb_queue, id, seqnum);
} }
void usb_init(void) void usb_init(void)
@ -901,9 +901,10 @@ bool usb_inserted(void)
return false; return false;
} }
void usb_acknowledge(long id) void usb_acknowledge(long id, intptr_t seqnum)
{ {
(void)id; (void)id;
(void)seqnum;
} }
void usb_init(void) void usb_init(void)

View file

@ -203,9 +203,9 @@ void usb_start_monitoring(void)
{ {
} }
void usb_acknowledge(long id) void usb_acknowledge(long id, intptr_t seqnum)
{ {
queue_post(&sim_queue, id, 0); queue_post(&sim_queue, id, seqnum);
} }
void usb_wait_for_disconnect(struct event_queue *q) void usb_wait_for_disconnect(struct event_queue *q)