clear endpoint stream when open for cdc_host and midi_host
diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c
index 92139b1..80fa81d 100644
--- a/src/class/cdc/cdc_device.c
+++ b/src/class/cdc/cdc_device.c
@@ -141,7 +141,7 @@
 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_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 @@
 //--------------------------------------------------------------------+
 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 @@
 
 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 @@
         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 @@
     }
   }
 
-  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 7fdf0a7..35717dd 100644
--- a/src/class/cdc/cdc_host.c
+++ b/src/class/cdc/cdc_host.c
@@ -298,6 +298,7 @@
 //--------------------------------------------------------------------+
 // 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 @@
         #endif
 
         default:
-          break;
+          break; // unknown driver
       }
     }
   }
@@ -389,8 +390,6 @@
   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 @@
   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 @@
   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_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 @@
     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 @@
   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 @@
     #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 @@
   } else if (ep_addr == p_cdc->ep_notif) {
     // TODO handle notification endpoint
   } else {
-    TU_ASSERT(false);
+    return false;
   }
 
   return true;
@@ -736,20 +728,17 @@
 //--------------------------------------------------------------------+
 // 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 @@
   }
 }
 
+// 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 79e7dfa..b20903d 100644
--- a/src/class/midi/midi_device.c
+++ b/src/class/midi/midi_device.c
@@ -404,15 +404,17 @@
       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 8b78fe9..0706287 100644
--- a/src/class/midi/midi_host.c
+++ b/src/class/midi/midi_host.c
@@ -59,9 +59,6 @@
   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 @@
       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 @@
   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 @@
         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 @@
   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;
diff --git a/src/common/tusb_common.h b/src/common/tusb_common.h
index 7aa42a2..f377d52 100644
--- a/src/common/tusb_common.h
+++ b/src/common/tusb_common.h
@@ -354,7 +354,7 @@
   return ((uint8_t const*) desc)[DESC_OFFSET_SUBTYPE];
 }
 
-TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_desc_in_bounds(uint8_t const* p_desc, uint8_t const* desc_end) {
+TU_ATTR_ALWAYS_INLINE static inline bool tu_desc_in_bounds(const uint8_t *p_desc, const uint8_t *desc_end) {
   if (p_desc >= desc_end) {
     return false;
   }
diff --git a/src/common/tusb_private.h b/src/common/tusb_private.h
index f82f87b..be1264a 100644
--- a/src/common/tusb_private.h
+++ b/src/common/tusb_private.h
@@ -120,7 +120,6 @@
   return tu_fifo_empty(&s->ff);
 }
 
-
 //--------------------------------------------------------------------+
 // Stream Write
 //--------------------------------------------------------------------+
diff --git a/src/tusb.c b/src/tusb.c
index df33f26..6fb5309 100644
--- a/src/tusb.c
+++ b/src/tusb.c
@@ -517,7 +517,7 @@
 }
 
 uint32_t tu_edpt_stream_read(uint8_t hwid, tu_edpt_stream_t* s, void* buffer, uint32_t bufsize) {
-  uint32_t num_read = tu_fifo_read_n(&s->ff, buffer, (uint16_t) bufsize);
+  const uint32_t num_read = tu_fifo_read_n(&s->ff, buffer, (uint16_t)bufsize);
   tu_edpt_stream_read_xfer(hwid, s);
   return num_read;
 }