summaryrefslogtreecommitdiff
path: root/src/class/cdc
diff options
context:
space:
mode:
authorhathach <[email protected]>2025-11-13 12:02:02 +0700
committerhathach <[email protected]>2025-11-13 12:31:27 +0700
commit397a3af8afc4bc5460572da4a40629684dfa788c (patch)
tree576d3ca7128854d5181b2a1c38660ba1f973ecb7 /src/class/cdc
parentf11adb02ebc0c594a7114b08585b8af18c3ee6d6 (diff)
clear endpoint stream when open for cdc_host and midi_host
Diffstat (limited to 'src/class/cdc')
-rw-r--r--src/class/cdc/cdc_device.c15
-rw-r--r--src/class/cdc/cdc_host.c50
2 files changed, 26 insertions, 39 deletions
diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c
index 92139b14e..80fa81d99 100644
--- a/src/class/cdc/cdc_device.c
+++ b/src/class/cdc/cdc_device.c
@@ -141,7 +141,7 @@ bool tud_cdc_configure(const tud_cdc_configure_t* driver_cfg) {
bool tud_cdc_n_ready(uint8_t itf) {
TU_VERIFY(itf < CFG_TUD_CDC);
TU_VERIFY(tud_ready());
- cdcd_interface_t *p_cdc = &_cdcd_itf[itf];
+ const cdcd_interface_t *p_cdc = &_cdcd_itf[itf];
const bool in_opened = tu_edpt_stream_is_opened(&p_cdc->stream.tx);
const bool out_opened = tu_edpt_stream_is_opened(&p_cdc->stream.rx);
@@ -151,9 +151,8 @@ bool tud_cdc_n_ready(uint8_t itf) {
bool tud_cdc_n_connected(uint8_t itf) {
TU_VERIFY(itf < CFG_TUD_CDC);
TU_VERIFY(tud_ready());
- cdcd_interface_t *p_cdc = &_cdcd_itf[itf];
// DTR (bit 0) active is considered as connected
- return tu_bit_test(p_cdc->line_state, 0);
+ return tu_bit_test(_cdcd_itf[itf].line_state, 0);
}
uint8_t tud_cdc_n_get_line_state(uint8_t itf) {
@@ -214,8 +213,7 @@ void tud_cdc_n_set_wanted_char(uint8_t itf, char wanted) {
//--------------------------------------------------------------------+
uint32_t tud_cdc_n_available(uint8_t itf) {
TU_VERIFY(itf < CFG_TUD_CDC, 0);
- cdcd_interface_t *p_cdc = &_cdcd_itf[itf];
- return tu_edpt_stream_read_available(&p_cdc->stream.rx);
+ return tu_edpt_stream_read_available(&_cdcd_itf[itf].stream.rx);
}
uint32_t tud_cdc_n_read(uint8_t itf, void* buffer, uint32_t bufsize) {
@@ -226,8 +224,7 @@ uint32_t tud_cdc_n_read(uint8_t itf, void* buffer, uint32_t bufsize) {
bool tud_cdc_n_peek(uint8_t itf, uint8_t *chr) {
TU_VERIFY(itf < CFG_TUD_CDC);
- cdcd_interface_t *p_cdc = &_cdcd_itf[itf];
- return tu_edpt_stream_peek(&p_cdc->stream.rx, chr);
+ return tu_edpt_stream_peek(&_cdcd_itf[itf].stream.rx, chr);
}
void tud_cdc_n_read_flush(uint8_t itf) {
@@ -362,8 +359,8 @@ uint16_t cdcd_open(uint8_t rhport, const tusb_desc_interface_t* itf_desc, uint16
TU_ASSERT(usbd_edpt_open(rhport, desc_ep), 0);
if (tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN) {
tu_edpt_stream_t *stream_tx = &p_cdc->stream.tx;
- tu_edpt_stream_open(stream_tx, desc_ep);
+ tu_edpt_stream_open(stream_tx, desc_ep);
if (_cdcd_cfg.tx_persistent) {
tu_edpt_stream_write_xfer(rhport, stream_tx); // flush pending data
} else {
@@ -384,7 +381,7 @@ uint16_t cdcd_open(uint8_t rhport, const tusb_desc_interface_t* itf_desc, uint16
}
}
- return p_desc - (const uint8_t *)itf_desc;
+ return (uint16_t)(p_desc - (const uint8_t *)itf_desc);
}
// Invoked when a control transfer occurred on an interface of this class
diff --git a/src/class/cdc/cdc_host.c b/src/class/cdc/cdc_host.c
index 7fdf0a7b9..35717ddf6 100644
--- a/src/class/cdc/cdc_host.c
+++ b/src/class/cdc/cdc_host.c
@@ -298,6 +298,7 @@ TU_VERIFY_STATIC(TU_ARRAY_SIZE(serial_drivers) == SERIAL_DRIVER_COUNT, "Serial d
//--------------------------------------------------------------------+
// INTERNAL OBJECT & FUNCTION DECLARATION
//--------------------------------------------------------------------+
+static bool open_ep_stream_pair(cdch_interface_t *p_cdc, const tusb_desc_endpoint_t *desc_ep);
TU_ATTR_ALWAYS_INLINE static inline cdch_interface_t * get_itf(uint8_t idx) {
TU_ASSERT(idx < CFG_TUH_CDC, NULL);
@@ -364,7 +365,7 @@ static cdch_interface_t* get_itf_by_xfer(const tuh_xfer_t * xfer) {
#endif
default:
- break;
+ break; // unknown driver
}
}
}
@@ -389,8 +390,6 @@ static cdch_interface_t * make_new_itf(uint8_t daddr, tusb_desc_interface_t cons
return NULL;
}
-static bool open_ep_stream_pair(cdch_interface_t * p_cdc , tusb_desc_endpoint_t const *desc_ep);
-
//--------------------------------------------------------------------+
// Weak stubs: invoked if no strong implementation is available
//--------------------------------------------------------------------+
@@ -519,7 +518,7 @@ bool tuh_cdc_read_clear (uint8_t idx) {
TU_VERIFY(p_cdc);
bool ret = tu_edpt_stream_clear(&p_cdc->stream.rx);
- tu_edpt_stream_read_xfer(p_cdc->daddr, &p_cdc->stream.rx);
+ (void)tu_edpt_stream_read_xfer(p_cdc->daddr, &p_cdc->stream.rx);
return ret;
}
@@ -648,13 +647,10 @@ bool cdch_init(void) {
for (size_t i = 0; i < CFG_TUH_CDC; i++) {
cdch_interface_t *p_cdc = &cdch_data[i];
cdch_epbuf_t *epbuf = &cdch_epbuf[i];
- tu_edpt_stream_init(&p_cdc->stream.tx, true, true, false,
- p_cdc->stream.tx_ff_buf, CFG_TUH_CDC_TX_BUFSIZE,
- epbuf->tx, CFG_TUH_CDC_TX_EPSIZE);
-
- tu_edpt_stream_init(&p_cdc->stream.rx, true, false, false,
- p_cdc->stream.rx_ff_buf, CFG_TUH_CDC_RX_BUFSIZE,
- epbuf->rx, CFG_TUH_CDC_RX_EPSIZE);
+ TU_ASSERT(tu_edpt_stream_init(&p_cdc->stream.tx, true, true, false, p_cdc->stream.tx_ff_buf, CFG_TUH_CDC_TX_BUFSIZE,
+ epbuf->tx, CFG_TUH_CDC_TX_EPSIZE));
+ TU_ASSERT(tu_edpt_stream_init(&p_cdc->stream.rx, true, false, false, p_cdc->stream.rx_ff_buf,
+ CFG_TUH_CDC_RX_BUFSIZE, epbuf->rx, CFG_TUH_CDC_RX_EPSIZE));
}
return true;
@@ -663,8 +659,8 @@ bool cdch_init(void) {
bool cdch_deinit(void) {
for (size_t i = 0; i < CFG_TUH_CDC; i++) {
cdch_interface_t *p_cdc = &cdch_data[i];
- tu_edpt_stream_deinit(&p_cdc->stream.tx);
- tu_edpt_stream_deinit(&p_cdc->stream.rx);
+ (void)tu_edpt_stream_deinit(&p_cdc->stream.tx);
+ (void)tu_edpt_stream_deinit(&p_cdc->stream.rx);
}
return true;
}
@@ -674,11 +670,9 @@ void cdch_close(uint8_t daddr) {
cdch_interface_t *p_cdc = &cdch_data[idx];
if (p_cdc->daddr == daddr) {
TU_LOG_CDC(p_cdc, "close");
+ tuh_cdc_umount_cb(idx); // invoke callback
- // Invoke application callback
- tuh_cdc_umount_cb(idx);
-
- p_cdc->daddr = 0;
+ p_cdc->daddr = 0;
p_cdc->bInterfaceNumber = 0;
p_cdc->mounted = false;
tu_edpt_stream_close(&p_cdc->stream.tx);
@@ -696,13 +690,12 @@ bool cdch_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t event, uint32_t
TU_ASSERT(p_cdc);
if (ep_addr == p_cdc->stream.tx.ep_addr) {
- // invoke tx complete callback to possibly refill tx fifo
- tuh_cdc_tx_complete_cb(idx);
+ tuh_cdc_tx_complete_cb(idx); // invoke transmit complete callback
if (0 == tu_edpt_stream_write_xfer(daddr, &p_cdc->stream.tx)) {
// If there is no data left, a ZLP should be sent if:
// - xferred_bytes is multiple of EP Packet size and not zero
- tu_edpt_stream_write_zlp_if_needed(daddr, &p_cdc->stream.tx, xferred_bytes);
+ (void)tu_edpt_stream_write_zlp_if_needed(daddr, &p_cdc->stream.tx, xferred_bytes);
}
} else if (ep_addr == p_cdc->stream.rx.ep_addr) {
#if CFG_TUH_CDC_FTDI
@@ -718,7 +711,6 @@ bool cdch_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t event, uint32_t
#endif
{
tu_edpt_stream_read_xfer_complete(&p_cdc->stream.rx, xferred_bytes);
-
tuh_cdc_rx_cb(idx); // invoke receive callback
}
@@ -727,7 +719,7 @@ bool cdch_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t event, uint32_t
} else if (ep_addr == p_cdc->ep_notif) {
// TODO handle notification endpoint
} else {
- TU_ASSERT(false);
+ return false;
}
return true;
@@ -736,20 +728,17 @@ bool cdch_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t event, uint32_t
//--------------------------------------------------------------------+
// Enumeration
//--------------------------------------------------------------------+
-
static bool open_ep_stream_pair(cdch_interface_t *p_cdc, tusb_desc_endpoint_t const *desc_ep) {
for (size_t i = 0; i < 2; i++) {
TU_ASSERT(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType &&
TUSB_XFER_BULK == desc_ep->bmAttributes.xfer);
TU_ASSERT(tuh_edpt_open(p_cdc->daddr, desc_ep));
+ tu_edpt_stream_t *stream =
+ (tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN) ? &p_cdc->stream.rx : &p_cdc->stream.tx;
+ tu_edpt_stream_open(stream, desc_ep);
+ tu_edpt_stream_clear(stream);
- if (tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN) {
- tu_edpt_stream_open(&p_cdc->stream.rx, desc_ep);
- } else {
- tu_edpt_stream_open(&p_cdc->stream.tx, desc_ep);
- }
-
- desc_ep = (tusb_desc_endpoint_t const *) tu_desc_next(desc_ep);
+ desc_ep = (const tusb_desc_endpoint_t *)tu_desc_next(desc_ep);
}
return true;
@@ -832,6 +821,7 @@ static void cdch_process_set_config(tuh_xfer_t *xfer) {
}
}
+// return false if there is no active transfer
static bool set_line_state_on_enum(cdch_interface_t *p_cdc, tuh_xfer_t *xfer) {
enum {
ENUM_SET_LINE_CODING = 0,