Skip to content

Commit c46d292

Browse files
committed
feature(tusb): Added teardown API
1 parent b339c84 commit c46d292

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

src/tusb.c

+41
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,47 @@ void tusb_int_handler(uint8_t rhport, bool in_isr) {
136136
#endif
137137
}
138138

139+
bool tusb_rhport_teardown(uint8_t rhport) {
140+
bool ret = false;
141+
// backward compatible call with tusb_init(void)
142+
#if defined(TUD_OPT_RHPORT) || defined(TUH_OPT_RHPORT)
143+
#if CFG_TUD_ENABLED && defined(TUD_OPT_RHPORT)
144+
// deinit device stack, CFG_TUSB_RHPORTx_MODE must be defined
145+
ret = ret || tud_deinit(TUD_OPT_RHPORT);
146+
_tusb_rhport_role[TUD_OPT_RHPORT] = TUSB_ROLE_INVALID;
147+
#endif
148+
149+
#if CFG_TUH_ENABLED && defined(TUH_OPT_RHPORT)
150+
// deinit host stack CFG_TUSB_RHPORTx_MODE must be defined
151+
ret = ret || tuh_deinit(TUH_OPT_RHPORT);
152+
_tusb_rhport_role[TUH_OPT_RHPORT] = TUSB_ROLE_INVALID;
153+
#endif
154+
155+
return ret;
156+
#endif
157+
158+
// new API with explicit rhport and role
159+
TU_ASSERT(rhport < TUP_USBIP_CONTROLLER_NUM);
160+
161+
#if CFG_TUD_ENABLED
162+
if (tud_deinit(rhport)) {
163+
_tusb_rhport_role[rhport] = TUSB_ROLE_INVALID;
164+
} else {
165+
ret = ret || false;
166+
}
167+
#endif
168+
169+
#if CFG_TUH_ENABLED
170+
if (tuh_deinit(rhport)) {
171+
_tusb_rhport_role[rhport] = TUSB_ROLE_INVALID;
172+
} else {
173+
ret = ret || false;
174+
}
175+
#endif
176+
177+
return ret;
178+
}
179+
139180
//--------------------------------------------------------------------+
140181
// Descriptor helper
141182
//--------------------------------------------------------------------+

src/tusb.h

+12-2
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,24 @@ bool tusb_inited(void);
154154
// Called to handle usb interrupt/event. tusb_init(rhport, role) must be called before
155155
void tusb_int_handler(uint8_t rhport, bool in_isr);
156156

157-
// TODO
158-
// bool tusb_teardown(void);
157+
// Internal helper for backward compatibility with tusb_init(void)
158+
bool tusb_rhport_teardown(uint8_t rhport);
159+
160+
#if defined(TUD_OPT_RHPORT) || defined(TUH_OPT_RHPORT)
161+
#define _tusb_teardown_arg0() tusb_rhport_teardown(0)
162+
#else
163+
#define _tusb_teardown_arg0() TU_VERIFY_STATIC(false, "CFG_TUSB_RHPORT0_MODE/CFG_TUSB_RHPORT1_MODE must be defined")
164+
#endif
165+
166+
#define _tusb_teardown_arg1(_rhport) tusb_rhport_teardown(_rhport)
167+
#define tusb_teardown(...) TU_FUNC_OPTIONAL_ARG(_tusb_teardown, __VA_ARGS__)
159168

160169
#else
161170

162171
#define tusb_init(...) (false)
163172
#define tusb_int_handler(...) do {}while(0)
164173
#define tusb_inited() (false)
174+
#define tusb_teardown(...) (false)
165175

166176
#endif
167177

0 commit comments

Comments
 (0)