From f16ebd512bbaed3971a6e5a28168413279abf4c4 Mon Sep 17 00:00:00 2001
From: hathach <thach@tinyusb.org>
Date: Thu, 4 Jul 2019 18:45:10 +0700
Subject: [PATCH 1/2] indent clean up

---
 src/portable/microchip/samd21/dcd_samd21.c | 99 +++++++++++-----------
 src/portable/microchip/samd51/dcd_samd51.c | 88 +++++++++----------
 2 files changed, 95 insertions(+), 92 deletions(-)

diff --git a/src/portable/microchip/samd21/dcd_samd21.c b/src/portable/microchip/samd21/dcd_samd21.c
index 7f3c34959..231abc7d0 100644
--- a/src/portable/microchip/samd21/dcd_samd21.c
+++ b/src/portable/microchip/samd21/dcd_samd21.c
@@ -232,60 +232,63 @@ void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr)
 /*------------------------------------------------------------------*/
 
 static bool maybe_handle_setup_packet(void) {
-    if (USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.bit.RXSTP)
-    {
-        USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_RXSTP;
+  if (USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.bit.RXSTP)
+  {
+    USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_RXSTP;
 
-        // This copies the data elsewhere so we can reuse the buffer.
-        dcd_event_setup_received(0, (uint8_t*) sram_registers[0][0].ADDR.reg, true);
-        return true;
-    }
-    return false;
+    // This copies the data elsewhere so we can reuse the buffer.
+    dcd_event_setup_received(0, (uint8_t*) sram_registers[0][0].ADDR.reg, true);
+    return true;
+  }
+  return false;
 }
 
 void maybe_transfer_complete(void) {
-    uint32_t epints = USB->DEVICE.EPINTSMRY.reg;
-    for (uint8_t epnum = 0; epnum < USB_EPT_NUM; epnum++) {
-        if ((epints & (1 << epnum)) == 0) {
-            continue;
-        }
+  uint32_t epints = USB->DEVICE.EPINTSMRY.reg;
 
-        if (maybe_handle_setup_packet()) {
-            continue;
-        }
-        UsbDeviceEndpoint* ep = &USB->DEVICE.DeviceEndpoint[epnum];
-
-        uint32_t epintflag = ep->EPINTFLAG.reg;
-
-        uint16_t total_transfer_size = 0;
-
-        // Handle IN completions
-        if ((epintflag & USB_DEVICE_EPINTFLAG_TRCPT1) != 0) {
-            ep->EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT1;
-
-            UsbDeviceDescBank* bank = &sram_registers[epnum][TUSB_DIR_IN];
-            total_transfer_size = bank->PCKSIZE.bit.BYTE_COUNT;
-
-            uint8_t ep_addr = epnum | TUSB_DIR_IN_MASK;
-            dcd_event_xfer_complete(0, ep_addr, total_transfer_size, XFER_RESULT_SUCCESS, true);
-        }
-
-        // Handle OUT completions
-        if ((epintflag & USB_DEVICE_EPINTFLAG_TRCPT0) != 0) {
-            ep->EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT0;
-
-            UsbDeviceDescBank* bank = &sram_registers[epnum][TUSB_DIR_OUT];
-            total_transfer_size = bank->PCKSIZE.bit.BYTE_COUNT;
-
-            uint8_t ep_addr = epnum;
-            dcd_event_xfer_complete(0, ep_addr, total_transfer_size, XFER_RESULT_SUCCESS, true);
-        }
-
-        // just finished status stage (total size = 0), prepare for next setup packet
-        if (epnum == 0 && total_transfer_size == 0) {
-            dcd_edpt_xfer(0, 0, _setup_packet, sizeof(_setup_packet));
-        }
+  for (uint8_t epnum = 0; epnum < USB_EPT_NUM; epnum++) {
+    if ((epints & (1 << epnum)) == 0) {
+      continue;
     }
+
+    if (maybe_handle_setup_packet()) {
+      continue;
+    }
+
+    UsbDeviceEndpoint* ep = &USB->DEVICE.DeviceEndpoint[epnum];
+
+    uint32_t epintflag = ep->EPINTFLAG.reg;
+
+    uint16_t total_transfer_size = 0;
+
+    // Handle IN completions
+    if ((epintflag & USB_DEVICE_EPINTFLAG_TRCPT1) != 0) {
+      ep->EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT1;
+
+      UsbDeviceDescBank* bank = &sram_registers[epnum][TUSB_DIR_IN];
+      total_transfer_size = bank->PCKSIZE.bit.BYTE_COUNT;
+
+      uint8_t ep_addr = epnum | TUSB_DIR_IN_MASK;
+      dcd_event_xfer_complete(0, ep_addr, total_transfer_size, XFER_RESULT_SUCCESS, true);
+    }
+
+    // Handle OUT completions
+    if ((epintflag & USB_DEVICE_EPINTFLAG_TRCPT0) != 0) {
+      ep->EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT0;
+
+      UsbDeviceDescBank* bank = &sram_registers[epnum][TUSB_DIR_OUT];
+      total_transfer_size = bank->PCKSIZE.bit.BYTE_COUNT;
+
+      uint8_t ep_addr = epnum;
+      dcd_event_xfer_complete(0, ep_addr, total_transfer_size, XFER_RESULT_SUCCESS, true);
+    }
+
+    // Just finished status stage (total size = 0), prepare for next setup packet
+    // TODO could cause issue with actual zero length data used by class such as DFU
+    if (epnum == 0 && total_transfer_size == 0) {
+      dcd_edpt_xfer(0, 0, _setup_packet, sizeof(_setup_packet));
+    }
+  }
 }
 
 void USB_Handler(void)
diff --git a/src/portable/microchip/samd51/dcd_samd51.c b/src/portable/microchip/samd51/dcd_samd51.c
index f2aff857c..f58e7cbc7 100644
--- a/src/portable/microchip/samd51/dcd_samd51.c
+++ b/src/portable/microchip/samd51/dcd_samd51.c
@@ -213,9 +213,9 @@ void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr)
   UsbDeviceEndpoint* ep = &USB->DEVICE.DeviceEndpoint[epnum];
 
   if (tu_edpt_dir(ep_addr) == TUSB_DIR_IN) {
-      ep->EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_STALLRQ1;
+    ep->EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_STALLRQ1;
   } else {
-      ep->EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_STALLRQ0;
+    ep->EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_STALLRQ0;
   }
 }
 
@@ -236,15 +236,15 @@ void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr)
 /*------------------------------------------------------------------*/
 
 static bool maybe_handle_setup_packet(void) {
-    if (USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.bit.RXSTP)
-    {
-        USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_RXSTP;
+  if (USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.bit.RXSTP)
+  {
+    USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_RXSTP;
 
-        // This copies the data elsewhere so we can reuse the buffer.
-        dcd_event_setup_received(0, (uint8_t*) sram_registers[0][0].ADDR.reg, true);
-        return true;
-    }
-    return false;
+    // This copies the data elsewhere so we can reuse the buffer.
+    dcd_event_setup_received(0, (uint8_t*) sram_registers[0][0].ADDR.reg, true);
+    return true;
+  }
+  return false;
 }
 /*
  *------------------------------------------------------------------*/
@@ -309,42 +309,42 @@ void USB_0_Handler(void) {
 
 /* USB_SOF_HSOF */
 void USB_1_Handler(void) {
-    USB->DEVICE.INTFLAG.reg = USB_DEVICE_INTFLAG_SOF;
-    dcd_event_bus_signal(0, DCD_EVENT_SOF, true);
+  USB->DEVICE.INTFLAG.reg = USB_DEVICE_INTFLAG_SOF;
+  dcd_event_bus_signal(0, DCD_EVENT_SOF, true);
 }
 
 void transfer_complete(uint8_t direction) {
-    uint32_t epints = USB->DEVICE.EPINTSMRY.reg;
-    for (uint8_t epnum = 0; epnum < USB_EPT_NUM; epnum++) {
-        if ((epints & (1 << epnum)) == 0) {
-            continue;
-        }
-
-        if (direction == TUSB_DIR_OUT && maybe_handle_setup_packet()) {
-            continue;
-        }
-        UsbDeviceEndpoint* ep = &USB->DEVICE.DeviceEndpoint[epnum];
-
-        UsbDeviceDescBank* bank = &sram_registers[epnum][direction];
-        uint16_t total_transfer_size = bank->PCKSIZE.bit.BYTE_COUNT;
-
-        uint8_t ep_addr = epnum;
-        if (direction == TUSB_DIR_IN) {
-            ep_addr |= TUSB_DIR_IN_MASK;
-        }
-        dcd_event_xfer_complete(0, ep_addr, total_transfer_size, XFER_RESULT_SUCCESS, true);
-
-        // just finished status stage (total size = 0), prepare for next setup packet
-        if (epnum == 0 && total_transfer_size == 0) {
-            dcd_edpt_xfer(0, 0, _setup_packet, sizeof(_setup_packet));
-        }
-
-        if (direction == TUSB_DIR_IN) {
-            ep->EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT1;
-        } else {
-            ep->EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT0;
-        }
+  uint32_t epints = USB->DEVICE.EPINTSMRY.reg;
+  for (uint8_t epnum = 0; epnum < USB_EPT_NUM; epnum++) {
+    if ((epints & (1 << epnum)) == 0) {
+      continue;
     }
+
+    if (direction == TUSB_DIR_OUT && maybe_handle_setup_packet()) {
+      continue;
+    }
+    UsbDeviceEndpoint* ep = &USB->DEVICE.DeviceEndpoint[epnum];
+
+    UsbDeviceDescBank* bank = &sram_registers[epnum][direction];
+    uint16_t total_transfer_size = bank->PCKSIZE.bit.BYTE_COUNT;
+
+    uint8_t ep_addr = epnum;
+    if (direction == TUSB_DIR_IN) {
+      ep_addr |= TUSB_DIR_IN_MASK;
+    }
+    dcd_event_xfer_complete(0, ep_addr, total_transfer_size, XFER_RESULT_SUCCESS, true);
+
+    // just finished status stage (total size = 0), prepare for next setup packet
+    if (epnum == 0 && total_transfer_size == 0) {
+      dcd_edpt_xfer(0, 0, _setup_packet, sizeof(_setup_packet));
+    }
+
+    if (direction == TUSB_DIR_IN) {
+      ep->EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT1;
+    } else {
+      ep->EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT0;
+    }
+  }
 }
 
 // Bank zero is for OUT and SETUP transactions.
@@ -352,7 +352,7 @@ void transfer_complete(uint8_t direction) {
 USB_TRCPT0_3, USB_TRCPT0_4, USB_TRCPT0_5,
 USB_TRCPT0_6, USB_TRCPT0_7 */
 void USB_2_Handler(void) {
-    transfer_complete(TUSB_DIR_OUT);
+  transfer_complete(TUSB_DIR_OUT);
 }
 
 // Bank one is used for IN transactions.
@@ -360,7 +360,7 @@ void USB_2_Handler(void) {
 USB_TRCPT1_3, USB_TRCPT1_4, USB_TRCPT1_5,
 USB_TRCPT1_6, USB_TRCPT1_7 */
 void USB_3_Handler(void) {
-    transfer_complete(TUSB_DIR_IN);
+  transfer_complete(TUSB_DIR_IN);
 }
 
 #endif

From 216ccc6e93fe294e80bb17bfb873bfb00f5ac038 Mon Sep 17 00:00:00 2001
From: hathach <thach@tinyusb.org>
Date: Fri, 5 Jul 2019 16:46:11 +0700
Subject: [PATCH 2/2] midi only use audio 1.0 without IAD

---
 src/class/midi/midi_device.c | 8 +++-----
 src/device/usbd.h            | 4 +---
 2 files changed, 4 insertions(+), 8 deletions(-)

diff --git a/src/class/midi/midi_device.c b/src/class/midi/midi_device.c
index 76776d71c..267167720 100644
--- a/src/class/midi/midi_device.c
+++ b/src/class/midi/midi_device.c
@@ -250,15 +250,13 @@ bool midid_open(uint8_t rhport, tusb_desc_interface_t const * p_interface_desc,
     (*p_length) = sizeof(tusb_desc_interface_t);
 
     // Skip over the class specific descriptor.
-    (*p_length) += p_desc[DESC_OFFSET_LEN];
+    (*p_length) += tu_desc_len(p_desc);
     p_desc = tu_desc_next(p_desc);
     return true;
   }
 
-  if ( AUDIO_SUBCLASS_MIDI_STREAMING != p_interface_desc->bInterfaceSubClass ||
-       p_interface_desc->bInterfaceProtocol != AUDIO_PROTOCOL_V1 ) {
-    return false;
-  }
+  TU_VERIFY(AUDIO_SUBCLASS_MIDI_STREAMING == p_interface_desc->bInterfaceSubClass &&
+            AUDIO_PROTOCOL_V1 == p_interface_desc->bInterfaceProtocol );
 
   // Find available interface
   midid_interface_t * p_midi = NULL;
diff --git a/src/device/usbd.h b/src/device/usbd.h
index d3a5dce66..bb9c62f2f 100644
--- a/src/device/usbd.h
+++ b/src/device/usbd.h
@@ -174,14 +174,12 @@ TU_ATTR_WEAK void tud_resume_cb(void);
 //------------- MIDI -------------//
 
 // Length of template descriptor (96 bytes)
-#define TUD_MIDI_DESC_LEN (8 + 9 + 9 + 9 + 7 + 6 + 6 + 9 + 9 + 7 + 5 + 7 + 5)
+#define TUD_MIDI_DESC_LEN (9 + 9 + 9 + 7 + 6 + 6 + 9 + 9 + 7 + 5 + 7 + 5)
 
 // MIDI simple descriptor
 // - 1 Embedded Jack In connected to 1 External Jack Out
 // - 1 Embedded Jack out connected to 1 External Jack In
 #define TUD_MIDI_DESCRIPTOR(_itfnum, _stridx, _epin, _epout, _epsize) \
-  /* Interface Associate */\
-  8, TUSB_DESC_INTERFACE_ASSOCIATION, _itfnum, 2, TUSB_CLASS_AUDIO, 0x00, AUDIO_PROTOCOL_V1, 0,\
   /* Audio Control (AC) Interface */\
   9, TUSB_DESC_INTERFACE, _itfnum, 0, 0, TUSB_CLASS_AUDIO, AUDIO_SUBCLASS_CONTROL, AUDIO_PROTOCOL_V1, _stridx,\
   /* AC Header */\