Skip to content

Commit 3be91bd

Browse files
committed
clear endpoint stream when open for cdc_host and midi_host
1 parent f11adb0 commit 3be91bd

File tree

3 files changed

+47
-55
lines changed

3 files changed

+47
-55
lines changed

src/class/cdc/cdc_device.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,8 @@ uint16_t cdcd_open(uint8_t rhport, const tusb_desc_interface_t* itf_desc, uint16
362362
TU_ASSERT(usbd_edpt_open(rhport, desc_ep), 0);
363363
if (tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN) {
364364
tu_edpt_stream_t *stream_tx = &p_cdc->stream.tx;
365-
tu_edpt_stream_open(stream_tx, desc_ep);
366365

366+
tu_edpt_stream_open(stream_tx, desc_ep);
367367
if (_cdcd_cfg.tx_persistent) {
368368
tu_edpt_stream_write_xfer(rhport, stream_tx); // flush pending data
369369
} else {
@@ -384,7 +384,7 @@ uint16_t cdcd_open(uint8_t rhport, const tusb_desc_interface_t* itf_desc, uint16
384384
}
385385
}
386386

387-
return p_desc - (const uint8_t *)itf_desc;
387+
return (uint16_t)(p_desc - (const uint8_t *)itf_desc);
388388
}
389389

390390
// Invoked when a control transfer occurred on an interface of this class

src/class/cdc/cdc_host.c

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ TU_VERIFY_STATIC(TU_ARRAY_SIZE(serial_drivers) == SERIAL_DRIVER_COUNT, "Serial d
298298
//--------------------------------------------------------------------+
299299
// INTERNAL OBJECT & FUNCTION DECLARATION
300300
//--------------------------------------------------------------------+
301+
static bool open_ep_stream_pair(cdch_interface_t *p_cdc, const tusb_desc_endpoint_t *desc_ep);
301302

302303
TU_ATTR_ALWAYS_INLINE static inline cdch_interface_t * get_itf(uint8_t idx) {
303304
TU_ASSERT(idx < CFG_TUH_CDC, NULL);
@@ -364,7 +365,7 @@ static cdch_interface_t* get_itf_by_xfer(const tuh_xfer_t * xfer) {
364365
#endif
365366

366367
default:
367-
break;
368+
break; // unknown driver
368369
}
369370
}
370371
}
@@ -389,8 +390,6 @@ static cdch_interface_t * make_new_itf(uint8_t daddr, tusb_desc_interface_t cons
389390
return NULL;
390391
}
391392

392-
static bool open_ep_stream_pair(cdch_interface_t * p_cdc , tusb_desc_endpoint_t const *desc_ep);
393-
394393
//--------------------------------------------------------------------+
395394
// Weak stubs: invoked if no strong implementation is available
396395
//--------------------------------------------------------------------+
@@ -519,7 +518,7 @@ bool tuh_cdc_read_clear (uint8_t idx) {
519518
TU_VERIFY(p_cdc);
520519

521520
bool ret = tu_edpt_stream_clear(&p_cdc->stream.rx);
522-
tu_edpt_stream_read_xfer(p_cdc->daddr, &p_cdc->stream.rx);
521+
(void)tu_edpt_stream_read_xfer(p_cdc->daddr, &p_cdc->stream.rx);
523522
return ret;
524523
}
525524

@@ -648,13 +647,10 @@ bool cdch_init(void) {
648647
for (size_t i = 0; i < CFG_TUH_CDC; i++) {
649648
cdch_interface_t *p_cdc = &cdch_data[i];
650649
cdch_epbuf_t *epbuf = &cdch_epbuf[i];
651-
tu_edpt_stream_init(&p_cdc->stream.tx, true, true, false,
652-
p_cdc->stream.tx_ff_buf, CFG_TUH_CDC_TX_BUFSIZE,
653-
epbuf->tx, CFG_TUH_CDC_TX_EPSIZE);
654-
655-
tu_edpt_stream_init(&p_cdc->stream.rx, true, false, false,
656-
p_cdc->stream.rx_ff_buf, CFG_TUH_CDC_RX_BUFSIZE,
657-
epbuf->rx, CFG_TUH_CDC_RX_EPSIZE);
650+
TU_ASSERT(tu_edpt_stream_init(&p_cdc->stream.tx, true, true, false, p_cdc->stream.tx_ff_buf, CFG_TUH_CDC_TX_BUFSIZE,
651+
epbuf->tx, CFG_TUH_CDC_TX_EPSIZE));
652+
TU_ASSERT(tu_edpt_stream_init(&p_cdc->stream.rx, true, false, false, p_cdc->stream.rx_ff_buf,
653+
CFG_TUH_CDC_RX_BUFSIZE, epbuf->rx, CFG_TUH_CDC_RX_EPSIZE));
658654
}
659655

660656
return true;
@@ -663,8 +659,8 @@ bool cdch_init(void) {
663659
bool cdch_deinit(void) {
664660
for (size_t i = 0; i < CFG_TUH_CDC; i++) {
665661
cdch_interface_t *p_cdc = &cdch_data[i];
666-
tu_edpt_stream_deinit(&p_cdc->stream.tx);
667-
tu_edpt_stream_deinit(&p_cdc->stream.rx);
662+
(void)tu_edpt_stream_deinit(&p_cdc->stream.tx);
663+
(void)tu_edpt_stream_deinit(&p_cdc->stream.rx);
668664
}
669665
return true;
670666
}
@@ -674,11 +670,9 @@ void cdch_close(uint8_t daddr) {
674670
cdch_interface_t *p_cdc = &cdch_data[idx];
675671
if (p_cdc->daddr == daddr) {
676672
TU_LOG_CDC(p_cdc, "close");
673+
tuh_cdc_umount_cb(idx); // invoke callback
677674

678-
// Invoke application callback
679-
tuh_cdc_umount_cb(idx);
680-
681-
p_cdc->daddr = 0;
675+
p_cdc->daddr = 0;
682676
p_cdc->bInterfaceNumber = 0;
683677
p_cdc->mounted = false;
684678
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
696690
TU_ASSERT(p_cdc);
697691

698692
if (ep_addr == p_cdc->stream.tx.ep_addr) {
699-
// invoke tx complete callback to possibly refill tx fifo
700-
tuh_cdc_tx_complete_cb(idx);
693+
tuh_cdc_tx_complete_cb(idx); // invoke transmit complete callback
701694

702695
if (0 == tu_edpt_stream_write_xfer(daddr, &p_cdc->stream.tx)) {
703696
// If there is no data left, a ZLP should be sent if:
704697
// - xferred_bytes is multiple of EP Packet size and not zero
705-
tu_edpt_stream_write_zlp_if_needed(daddr, &p_cdc->stream.tx, xferred_bytes);
698+
(void)tu_edpt_stream_write_zlp_if_needed(daddr, &p_cdc->stream.tx, xferred_bytes);
706699
}
707700
} else if (ep_addr == p_cdc->stream.rx.ep_addr) {
708701
#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
718711
#endif
719712
{
720713
tu_edpt_stream_read_xfer_complete(&p_cdc->stream.rx, xferred_bytes);
721-
722714
tuh_cdc_rx_cb(idx); // invoke receive callback
723715
}
724716

@@ -727,7 +719,7 @@ bool cdch_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t event, uint32_t
727719
} else if (ep_addr == p_cdc->ep_notif) {
728720
// TODO handle notification endpoint
729721
} else {
730-
TU_ASSERT(false);
722+
return false;
731723
}
732724

733725
return true;
@@ -736,20 +728,17 @@ bool cdch_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t event, uint32_t
736728
//--------------------------------------------------------------------+
737729
// Enumeration
738730
//--------------------------------------------------------------------+
739-
740731
static bool open_ep_stream_pair(cdch_interface_t *p_cdc, tusb_desc_endpoint_t const *desc_ep) {
741732
for (size_t i = 0; i < 2; i++) {
742733
TU_ASSERT(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType &&
743734
TUSB_XFER_BULK == desc_ep->bmAttributes.xfer);
744735
TU_ASSERT(tuh_edpt_open(p_cdc->daddr, desc_ep));
736+
tu_edpt_stream_t *stream =
737+
(tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN) ? &p_cdc->stream.rx : &p_cdc->stream.tx;
738+
tu_edpt_stream_open(stream, desc_ep);
739+
tu_edpt_stream_clear(stream);
745740

746-
if (tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN) {
747-
tu_edpt_stream_open(&p_cdc->stream.rx, desc_ep);
748-
} else {
749-
tu_edpt_stream_open(&p_cdc->stream.tx, desc_ep);
750-
}
751-
752-
desc_ep = (tusb_desc_endpoint_t const *) tu_desc_next(desc_ep);
741+
desc_ep = (const tusb_desc_endpoint_t *)tu_desc_next(desc_ep);
753742
}
754743

755744
return true;
@@ -832,6 +821,7 @@ static void cdch_process_set_config(tuh_xfer_t *xfer) {
832821
}
833822
}
834823

824+
// return false if there is no active transfer
835825
static bool set_line_state_on_enum(cdch_interface_t *p_cdc, tuh_xfer_t *xfer) {
836826
enum {
837827
ENUM_SET_LINE_CODING = 0,

src/class/midi/midi_host.c

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ typedef struct {
5959
uint8_t iInterface;
6060
uint8_t itf_count; // number of interface including Audio Control + MIDI streaming
6161

62-
uint8_t ep_in; // IN endpoint address
63-
uint8_t ep_out; // OUT endpoint address
64-
6562
uint8_t rx_cable_count; // IN endpoint CS descriptor bNumEmbMIDIJack value
6663
uint8_t tx_cable_count; // OUT endpoint CS descriptor bNumEmbMIDIJack value
6764

@@ -147,8 +144,6 @@ void midih_close(uint8_t daddr) {
147144
TU_LOG_DRV(" MIDI close addr = %u index = %u\r\n", daddr, idx);
148145
tuh_midi_umount_cb(idx);
149146

150-
p_midi->ep_in = 0;
151-
p_midi->ep_out = 0;
152147
p_midi->bInterfaceNumber = 0;
153148
p_midi->rx_cable_count = 0;
154149
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
169164
const uint8_t idx = get_idx_by_ep_addr(dev_addr, ep_addr);
170165
TU_VERIFY(idx < CFG_TUH_MIDI);
171166
midih_interface_t *p_midi = &_midi_host[idx];
167+
tu_edpt_stream_t *ep_str_rx = &p_midi->ep_stream.rx;
168+
tu_edpt_stream_t *ep_str_tx = &p_midi->ep_stream.tx;
172169

173-
if (ep_addr == p_midi->ep_stream.rx.ep_addr) {
170+
if (ep_addr == ep_str_rx->ep_addr) {
174171
// receive new data, put it into FIFO and invoke callback if available
175172
// Note: some devices send back all zero packets even if there is no data ready
176-
if (xferred_bytes && !tu_mem_is_zero(p_midi->ep_stream.rx.ep_buf, xferred_bytes)) {
177-
tu_edpt_stream_read_xfer_complete(&p_midi->ep_stream.rx, xferred_bytes);
173+
if (xferred_bytes && !tu_mem_is_zero(ep_str_rx->ep_buf, xferred_bytes)) {
174+
tu_edpt_stream_read_xfer_complete(ep_str_rx, xferred_bytes);
178175
tuh_midi_rx_cb(idx, xferred_bytes);
179176
}
180177

181-
tu_edpt_stream_read_xfer(dev_addr, &p_midi->ep_stream.rx); // prepare for next transfer
182-
} else if (ep_addr == p_midi->ep_stream.tx.ep_addr) {
178+
tu_edpt_stream_read_xfer(dev_addr, ep_str_rx); // prepare for next transfer
179+
} else if (ep_addr == ep_str_tx->ep_addr) {
183180
tuh_midi_tx_cb(idx, xferred_bytes);
184181

185-
if (0 == tu_edpt_stream_write_xfer(dev_addr, &p_midi->ep_stream.tx)) {
182+
if (0 == tu_edpt_stream_write_xfer(dev_addr, ep_str_tx)) {
186183
// If there is no data left, a ZLP should be sent if
187184
// xferred_bytes is multiple of EP size and not zero
188-
tu_edpt_stream_write_zlp_if_needed(dev_addr, &p_midi->ep_stream.tx, xferred_bytes);
185+
tu_edpt_stream_write_zlp_if_needed(dev_addr, ep_str_tx, xferred_bytes);
189186
}
190187
}
191188

@@ -295,21 +292,20 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d
295292
const midi_desc_cs_endpoint_t *p_csep = (const midi_desc_cs_endpoint_t *) p_desc;
296293

297294
TU_LOG_DRV(" Endpoint and CS_Endpoint descriptor %02x\r\n", p_ep->bEndpointAddress);
295+
tu_edpt_stream_t *ep_stream;
298296
if (tu_edpt_dir(p_ep->bEndpointAddress) == TUSB_DIR_OUT) {
299-
p_midi->ep_out = p_ep->bEndpointAddress;
300297
p_midi->tx_cable_count = p_csep->bNumEmbMIDIJack;
301298
desc_cb.desc_epout = p_ep;
302-
303-
TU_ASSERT(tuh_edpt_open(dev_addr, p_ep));
304-
tu_edpt_stream_open(&p_midi->ep_stream.tx, p_ep);
299+
ep_stream = &p_midi->ep_stream.tx;
305300
} else {
306-
p_midi->ep_in = p_ep->bEndpointAddress;
307301
p_midi->rx_cable_count = p_csep->bNumEmbMIDIJack;
308-
desc_cb.desc_epin = p_ep;
309-
310-
TU_ASSERT(tuh_edpt_open(dev_addr, p_ep));
311-
tu_edpt_stream_open(&p_midi->ep_stream.rx, p_ep);
302+
desc_cb.desc_epin = p_ep;
303+
ep_stream = &p_midi->ep_stream.rx;
312304
}
305+
TU_ASSERT(tuh_edpt_open(dev_addr, p_ep));
306+
tu_edpt_stream_open(ep_stream, p_ep);
307+
tu_edpt_stream_clear(ep_stream);
308+
313309
break;
314310
}
315311

@@ -379,8 +375,14 @@ bool tuh_midi_itf_get_info(uint8_t idx, tuh_itf_info_t* info) {
379375
desc->bDescriptorType = TUSB_DESC_INTERFACE;
380376

381377
desc->bInterfaceNumber = p_midi->bInterfaceNumber;
382-
desc->bAlternateSetting = 0;
383-
desc->bNumEndpoints = (uint8_t)((p_midi->ep_in != 0 ? 1:0) + (p_midi->ep_out != 0 ? 1:0));
378+
desc->bAlternateSetting = 0;
379+
desc->bNumEndpoints = 0;
380+
if (tu_edpt_stream_is_opened(&p_midi->ep_stream.tx)) {
381+
desc->bNumEndpoints++;
382+
}
383+
if (tu_edpt_stream_is_opened(&p_midi->ep_stream.rx)) {
384+
desc->bNumEndpoints++;
385+
}
384386
desc->bInterfaceClass = TUSB_CLASS_AUDIO;
385387
desc->bInterfaceSubClass = AUDIO_SUBCLASS_MIDI_STREAMING;
386388
desc->bInterfaceProtocol = 0;

0 commit comments

Comments
 (0)