Skip to content

Commit

Permalink
feature(tusb): Added teardown API
Browse files Browse the repository at this point in the history
  • Loading branch information
roma-jam committed Dec 11, 2024
1 parent b339c84 commit c46d292
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
41 changes: 41 additions & 0 deletions src/tusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,47 @@ void tusb_int_handler(uint8_t rhport, bool in_isr) {
#endif
}

bool tusb_rhport_teardown(uint8_t rhport) {
bool ret = false;
// backward compatible call with tusb_init(void)
#if defined(TUD_OPT_RHPORT) || defined(TUH_OPT_RHPORT)
#if CFG_TUD_ENABLED && defined(TUD_OPT_RHPORT)
// deinit device stack, CFG_TUSB_RHPORTx_MODE must be defined
ret = ret || tud_deinit(TUD_OPT_RHPORT);
_tusb_rhport_role[TUD_OPT_RHPORT] = TUSB_ROLE_INVALID;
#endif

#if CFG_TUH_ENABLED && defined(TUH_OPT_RHPORT)
// deinit host stack CFG_TUSB_RHPORTx_MODE must be defined
ret = ret || tuh_deinit(TUH_OPT_RHPORT);
_tusb_rhport_role[TUH_OPT_RHPORT] = TUSB_ROLE_INVALID;
#endif

return ret;
#endif

// new API with explicit rhport and role
TU_ASSERT(rhport < TUP_USBIP_CONTROLLER_NUM);

#if CFG_TUD_ENABLED
if (tud_deinit(rhport)) {
_tusb_rhport_role[rhport] = TUSB_ROLE_INVALID;
} else {
ret = ret || false;
}
#endif

#if CFG_TUH_ENABLED
if (tuh_deinit(rhport)) {
_tusb_rhport_role[rhport] = TUSB_ROLE_INVALID;
} else {
ret = ret || false;
}
#endif

return ret;
}

//--------------------------------------------------------------------+
// Descriptor helper
//--------------------------------------------------------------------+
Expand Down
14 changes: 12 additions & 2 deletions src/tusb.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,24 @@ bool tusb_inited(void);
// Called to handle usb interrupt/event. tusb_init(rhport, role) must be called before
void tusb_int_handler(uint8_t rhport, bool in_isr);

// TODO
// bool tusb_teardown(void);
// Internal helper for backward compatibility with tusb_init(void)
bool tusb_rhport_teardown(uint8_t rhport);

#if defined(TUD_OPT_RHPORT) || defined(TUH_OPT_RHPORT)
#define _tusb_teardown_arg0() tusb_rhport_teardown(0)
#else
#define _tusb_teardown_arg0() TU_VERIFY_STATIC(false, "CFG_TUSB_RHPORT0_MODE/CFG_TUSB_RHPORT1_MODE must be defined")
#endif

#define _tusb_teardown_arg1(_rhport) tusb_rhport_teardown(_rhport)
#define tusb_teardown(...) TU_FUNC_OPTIONAL_ARG(_tusb_teardown, __VA_ARGS__)

#else

#define tusb_init(...) (false)
#define tusb_int_handler(...) do {}while(0)
#define tusb_inited() (false)
#define tusb_teardown(...) (false)

#endif

Expand Down

0 comments on commit c46d292

Please sign in to comment.