Skip to content

Commit c92471b

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

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

src/tusb.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,50 @@ 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+
142+
TU_LOG1("\t teardown\r\n");
143+
144+
// backward compatible called with tusb_init(void)
145+
#if defined(TUD_OPT_RHPORT) || defined(TUH_OPT_RHPORT)
146+
#if CFG_TUD_ENABLED && defined(TUD_OPT_RHPORT)
147+
// deinit device stack, CFG_TUSB_RHPORTx_MODE must be defined
148+
ret = ret || tud_deinit(TUD_OPT_RHPORT);
149+
_tusb_rhport_role[TUD_OPT_RHPORT] = TUSB_ROLE_INVALID;
150+
#endif
151+
152+
#if CFG_TUH_ENABLED && defined(TUH_OPT_RHPORT)
153+
// deinit host stack CFG_TUSB_RHPORTx_MODE must be defined
154+
ret = ret || tuh_deinit(TUH_OPT_RHPORT);
155+
_tusb_rhport_role[TUD_OPT_RHPORT] = TUSB_ROLE_INVALID;
156+
#endif
157+
158+
return ret;
159+
#endif
160+
161+
// new API with explicit rhport and role
162+
TU_ASSERT(rhport < TUP_USBIP_CONTROLLER_NUM);
163+
164+
#if CFG_TUD_ENABLED
165+
if (tud_deinit(rhport)) {
166+
_tusb_rhport_role[rhport] = TUSB_ROLE_INVALID;
167+
} else {
168+
ret = ret || false;
169+
}
170+
#endif
171+
172+
#if CFG_TUH_ENABLED
173+
if (tuh_deinit(rhport)) {
174+
_tusb_rhport_role[rhport] = TUSB_ROLE_INVALID;
175+
} else {
176+
ret = ret || false;
177+
}
178+
#endif
179+
180+
return ret;
181+
}
182+
139183
//--------------------------------------------------------------------+
140184
// Descriptor helper
141185
//--------------------------------------------------------------------+

src/tusb.h

Lines changed: 12 additions & 2 deletions
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 compatible 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)