summaryrefslogtreecommitdiff
path: root/src/class
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
parentf11adb02ebc0c594a7114b08585b8af18c3ee6d6 (diff)
clear endpoint stream when open for cdc_host and midi_host
Diffstat (limited to 'src/class')
-rw-r--r--src/class/cdc/cdc_device.c15
-rw-r--r--src/class/cdc/cdc_host.c50
-rw-r--r--src/class/midi/midi_device.c14
-rw-r--r--src/class/midi/midi_host.c48
4 files changed, 59 insertions, 68 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,
diff --git a/src/class/midi/midi_device.c b/src/class/midi/midi_device.c
index 79e7dfa71..b20903d68 100644
--- a/src/class/midi/midi_device.c
+++ b/src/class/midi/midi_device.c
@@ -404,15 +404,17 @@ uint16_t midid_open(uint8_t rhport, const tusb_desc_interface_t *desc_itf, uint1
const uint8_t ep_addr = ((const tusb_desc_endpoint_t *)p_desc)->bEndpointAddress;
if (tu_edpt_dir(ep_addr) == TUSB_DIR_IN) {
- tu_edpt_stream_open(&p_midi->ep_stream.tx, desc_ep);
- tu_edpt_stream_clear(&p_midi->ep_stream.tx);
+ tu_edpt_stream_t *stream_tx = &p_midi->ep_stream.tx;
+ tu_edpt_stream_open(stream_tx, desc_ep);
+ tu_edpt_stream_clear(stream_tx);
} else {
- tu_edpt_stream_open(&p_midi->ep_stream.rx, desc_ep);
- tu_edpt_stream_clear(&p_midi->ep_stream.rx);
- TU_ASSERT(tu_edpt_stream_read_xfer(rhport, &p_midi->ep_stream.rx) > 0, 0); // prepare to receive data
+ tu_edpt_stream_t *stream_rx = &p_midi->ep_stream.rx;
+ tu_edpt_stream_open(stream_rx, desc_ep);
+ tu_edpt_stream_clear(stream_rx);
+ TU_ASSERT(tu_edpt_stream_read_xfer(rhport, stream_rx) > 0, 0); // prepare to receive data
}
- p_desc = tu_desc_next(p_desc); // skip CS Endpoint descriptor
+ p_desc = tu_desc_next(p_desc); // skip CS Endpoint descriptor
found_ep++;
}
diff --git a/src/class/midi/midi_host.c b/src/class/midi/midi_host.c
index 8b78fe945..07062875c 100644
--- a/src/class/midi/midi_host.c
+++ b/src/class/midi/midi_host.c
@@ -59,9 +59,6 @@ typedef struct {
uint8_t iInterface;
uint8_t itf_count; // number of interface including Audio Control + MIDI streaming
- uint8_t ep_in; // IN endpoint address
- uint8_t ep_out; // OUT endpoint address
-
uint8_t rx_cable_count; // IN endpoint CS descriptor bNumEmbMIDIJack value
uint8_t tx_cable_count; // OUT endpoint CS descriptor bNumEmbMIDIJack value
@@ -147,8 +144,6 @@ void midih_close(uint8_t daddr) {
TU_LOG_DRV(" MIDI close addr = %u index = %u\r\n", daddr, idx);
tuh_midi_umount_cb(idx);
- p_midi->ep_in = 0;
- p_midi->ep_out = 0;
p_midi->bInterfaceNumber = 0;
p_midi->rx_cable_count = 0;
p_midi->tx_cable_count = 0;
@@ -169,23 +164,25 @@ bool midih_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint
const uint8_t idx = get_idx_by_ep_addr(dev_addr, ep_addr);
TU_VERIFY(idx < CFG_TUH_MIDI);
midih_interface_t *p_midi = &_midi_host[idx];
+ tu_edpt_stream_t *ep_str_rx = &p_midi->ep_stream.rx;
+ tu_edpt_stream_t *ep_str_tx = &p_midi->ep_stream.tx;
- if (ep_addr == p_midi->ep_stream.rx.ep_addr) {
+ if (ep_addr == ep_str_rx->ep_addr) {
// receive new data, put it into FIFO and invoke callback if available
// Note: some devices send back all zero packets even if there is no data ready
- if (xferred_bytes && !tu_mem_is_zero(p_midi->ep_stream.rx.ep_buf, xferred_bytes)) {
- tu_edpt_stream_read_xfer_complete(&p_midi->ep_stream.rx, xferred_bytes);
+ if (xferred_bytes && !tu_mem_is_zero(ep_str_rx->ep_buf, xferred_bytes)) {
+ tu_edpt_stream_read_xfer_complete(ep_str_rx, xferred_bytes);
tuh_midi_rx_cb(idx, xferred_bytes);
}
- tu_edpt_stream_read_xfer(dev_addr, &p_midi->ep_stream.rx); // prepare for next transfer
- } else if (ep_addr == p_midi->ep_stream.tx.ep_addr) {
+ tu_edpt_stream_read_xfer(dev_addr, ep_str_rx); // prepare for next transfer
+ } else if (ep_addr == ep_str_tx->ep_addr) {
tuh_midi_tx_cb(idx, xferred_bytes);
- if (0 == tu_edpt_stream_write_xfer(dev_addr, &p_midi->ep_stream.tx)) {
+ if (0 == tu_edpt_stream_write_xfer(dev_addr, ep_str_tx)) {
// If there is no data left, a ZLP should be sent if
// xferred_bytes is multiple of EP size and not zero
- tu_edpt_stream_write_zlp_if_needed(dev_addr, &p_midi->ep_stream.tx, xferred_bytes);
+ tu_edpt_stream_write_zlp_if_needed(dev_addr, ep_str_tx, xferred_bytes);
}
}
@@ -295,21 +292,20 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d
const midi_desc_cs_endpoint_t *p_csep = (const midi_desc_cs_endpoint_t *) p_desc;
TU_LOG_DRV(" Endpoint and CS_Endpoint descriptor %02x\r\n", p_ep->bEndpointAddress);
+ tu_edpt_stream_t *ep_stream;
if (tu_edpt_dir(p_ep->bEndpointAddress) == TUSB_DIR_OUT) {
- p_midi->ep_out = p_ep->bEndpointAddress;
p_midi->tx_cable_count = p_csep->bNumEmbMIDIJack;
desc_cb.desc_epout = p_ep;
-
- TU_ASSERT(tuh_edpt_open(dev_addr, p_ep));
- tu_edpt_stream_open(&p_midi->ep_stream.tx, p_ep);
+ ep_stream = &p_midi->ep_stream.tx;
} else {
- p_midi->ep_in = p_ep->bEndpointAddress;
p_midi->rx_cable_count = p_csep->bNumEmbMIDIJack;
- desc_cb.desc_epin = p_ep;
-
- TU_ASSERT(tuh_edpt_open(dev_addr, p_ep));
- tu_edpt_stream_open(&p_midi->ep_stream.rx, p_ep);
+ desc_cb.desc_epin = p_ep;
+ ep_stream = &p_midi->ep_stream.rx;
}
+ TU_ASSERT(tuh_edpt_open(dev_addr, p_ep));
+ tu_edpt_stream_open(ep_stream, p_ep);
+ tu_edpt_stream_clear(ep_stream);
+
break;
}
@@ -379,8 +375,14 @@ bool tuh_midi_itf_get_info(uint8_t idx, tuh_itf_info_t* info) {
desc->bDescriptorType = TUSB_DESC_INTERFACE;
desc->bInterfaceNumber = p_midi->bInterfaceNumber;
- desc->bAlternateSetting = 0;
- desc->bNumEndpoints = (uint8_t)((p_midi->ep_in != 0 ? 1:0) + (p_midi->ep_out != 0 ? 1:0));
+ desc->bAlternateSetting = 0;
+ desc->bNumEndpoints = 0;
+ if (tu_edpt_stream_is_opened(&p_midi->ep_stream.tx)) {
+ desc->bNumEndpoints++;
+ }
+ if (tu_edpt_stream_is_opened(&p_midi->ep_stream.rx)) {
+ desc->bNumEndpoints++;
+ }
desc->bInterfaceClass = TUSB_CLASS_AUDIO;
desc->bInterfaceSubClass = AUDIO_SUBCLASS_MIDI_STREAMING;
desc->bInterfaceProtocol = 0;