Sfoglia il codice sorgente

Merge remote-tracking branch 'official/master'

Jeremiah McCarthy 4 anni fa
parent
commit
ce59d69520
46 ha cambiato i file con 1293 aggiunte e 887 eliminazioni
  1. 1 1
      .github/workflows/build.yml
  2. 1 1
      examples/device/hid_composite/src/usb_descriptors.c
  3. 1 1
      examples/device/hid_composite_freertos/src/usb_descriptors.c
  4. 1 1
      examples/device/hid_generic_inout/src/usb_descriptors.c
  5. 2 2
      examples/device/hid_multiple_interface/src/usb_descriptors.c
  6. 1 1
      examples/device/uac2_headset/src/main.c
  7. 1 0
      examples/host/cdc_msc_hid/CMakeLists.txt
  8. 237 0
      examples/host/cdc_msc_hid/src/hid_app.c
  9. 0 59
      examples/host/cdc_msc_hid/src/keyboard_helper.h
  10. 14 195
      examples/host/cdc_msc_hid/src/main.c
  11. 1 1
      examples/host/cdc_msc_hid/src/msc_app.c
  12. 8 3
      examples/host/cdc_msc_hid/src/tusb_config.h
  13. 1 1
      src/class/audio/audio_device.c
  14. 103 50
      src/class/hid/hid.h
  15. 34 29
      src/class/hid/hid_device.c
  16. 38 22
      src/class/hid/hid_device.h
  17. 454 176
      src/class/hid/hid_host.c
  18. 62 134
      src/class/hid/hid_host.h
  19. 12 10
      src/class/msc/msc_host.c
  20. 3 3
      src/class/msc/msc_host.h
  21. 2 0
      src/common/tusb_types.h
  22. 0 6
      src/device/dcd.h
  23. 20 8
      src/device/usbd.c
  24. 5 1
      src/device/usbd.h
  25. 1 1
      src/device/usbd_pvt.h
  26. 30 19
      src/host/hcd.h
  27. 146 111
      src/host/usbh.c
  28. 15 4
      src/host/usbh.h
  29. 2 1
      src/host/usbh_control.c
  30. 9 11
      src/portable/ehci/ehci.c
  31. 53 0
      src/portable/ehci/hcd_ehci.h
  32. 1 1
      src/portable/espressif/esp32sx/dcd_esp32sx.c
  33. 1 1
      src/portable/microchip/samg/dcd_samg.c
  34. 5 2
      src/portable/nordic/nrf5x/dcd_nrf5x.c
  35. 1 1
      src/portable/nuvoton/nuc120/dcd_nuc120.c
  36. 1 1
      src/portable/nuvoton/nuc121/dcd_nuc121.c
  37. 1 1
      src/portable/nuvoton/nuc505/dcd_nuc505.c
  38. 1 3
      src/portable/nxp/transdimension/hcd_transdimension.c
  39. 2 3
      src/portable/raspberrypi/rp2040/hcd_rp2040.c
  40. 1 1
      src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c
  41. 1 1
      src/portable/st/synopsys/dcd_synopsys.c
  42. 1 1
      src/portable/template/dcd_template.c
  43. 1 1
      src/portable/ti/msp430x5xx/dcd_msp430x5xx.c
  44. 15 12
      src/tusb.c
  45. 1 1
      src/tusb.h
  46. 2 5
      src/tusb_option.h

+ 1 - 1
.github/workflows/build.yml

@@ -199,7 +199,7 @@ jobs:
         - 'espressif_kaluga_1'
         - 'espressif_saola_1'
         # ESP32-S3
-        # - 'espressif_addax_1' # temporarily remove the board from test while espressif MR is not merged
+        - 'espressif_addax_1'
 
     steps:
     - name: Setup Python

+ 1 - 1
examples/device/hid_composite/src/usb_descriptors.c

@@ -108,7 +108,7 @@ uint8_t const desc_configuration[] =
   TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
 
   // Interface number, string index, protocol, report descriptor len, EP In address, size & polling interval
-  TUD_HID_DESCRIPTOR(ITF_NUM_HID, 0, HID_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 5)
+  TUD_HID_DESCRIPTOR(ITF_NUM_HID, 0, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 5)
 };
 
 // Invoked when received GET CONFIGURATION DESCRIPTOR

+ 1 - 1
examples/device/hid_composite_freertos/src/usb_descriptors.c

@@ -108,7 +108,7 @@ uint8_t const desc_configuration[] =
   TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
 
   // Interface number, string index, protocol, report descriptor len, EP In address, size & polling interval
-  TUD_HID_DESCRIPTOR(ITF_NUM_HID, 0, HID_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 5)
+  TUD_HID_DESCRIPTOR(ITF_NUM_HID, 0, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 5)
 };
 
 // Invoked when received GET CONFIGURATION DESCRIPTOR

+ 1 - 1
examples/device/hid_generic_inout/src/usb_descriptors.c

@@ -104,7 +104,7 @@ uint8_t const desc_configuration[] =
   TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
 
   // Interface number, string index, protocol, report descriptor len, EP In & Out address, size & polling interval
-  TUD_HID_INOUT_DESCRIPTOR(ITF_NUM_HID, 0, HID_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, 0x80 | EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 10)
+  TUD_HID_INOUT_DESCRIPTOR(ITF_NUM_HID, 0, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, 0x80 | EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 10)
 };
 
 // Invoked when received GET CONFIGURATION DESCRIPTOR

+ 2 - 2
examples/device/hid_multiple_interface/src/usb_descriptors.c

@@ -119,8 +119,8 @@ uint8_t const desc_configuration[] =
   TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
 
   // Interface number, string index, protocol, report descriptor len, EP In address, size & polling interval
-  TUD_HID_DESCRIPTOR(ITF_NUM_HID1, 4, HID_PROTOCOL_NONE, sizeof(desc_hid_report1), EPNUM_HID1, CFG_TUD_HID_EP_BUFSIZE, 10),
-  TUD_HID_DESCRIPTOR(ITF_NUM_HID2, 5, HID_PROTOCOL_NONE, sizeof(desc_hid_report2), EPNUM_HID2, CFG_TUD_HID_EP_BUFSIZE, 10)
+  TUD_HID_DESCRIPTOR(ITF_NUM_HID1, 4, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report1), EPNUM_HID1, CFG_TUD_HID_EP_BUFSIZE, 10),
+  TUD_HID_DESCRIPTOR(ITF_NUM_HID2, 5, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report2), EPNUM_HID2, CFG_TUD_HID_EP_BUFSIZE, 10)
 };
 
 // Invoked when received GET CONFIGURATION DESCRIPTOR

+ 1 - 1
examples/device/uac2_headset/src/main.c

@@ -149,7 +149,7 @@ typedef struct TU_ATTR_PACKED
     uint8_t bmRequestType;
   };
 
-  audio_cs_req_t bRequest;
+  uint8_t bRequest;  ///< Request type audio_cs_req_t
   uint8_t bChannelNumber;
   uint8_t bControlSelector;
   union

+ 1 - 0
examples/host/cdc_msc_hid/CMakeLists.txt

@@ -19,6 +19,7 @@ if(FAMILY STREQUAL "rp2040")
   # Example source
   target_sources(${PROJECT} PUBLIC
     ${CMAKE_CURRENT_SOURCE_DIR}/src/main.c
+    ${CMAKE_CURRENT_SOURCE_DIR}/src/hid_app.c
     ${CMAKE_CURRENT_SOURCE_DIR}/src/msc_app.c
   )
 

+ 237 - 0
examples/host/cdc_msc_hid/src/hid_app.c

@@ -0,0 +1,237 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2021, Ha Thach (tinyusb.org)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#include "bsp/board.h"
+#include "tusb.h"
+
+//--------------------------------------------------------------------+
+// MACRO TYPEDEF CONSTANT ENUM DECLARATION
+//--------------------------------------------------------------------+
+
+// If your host terminal support ansi escape code, it can be use to simulate mouse cursor
+#define USE_ANSI_ESCAPE   0
+
+#define MAX_REPORT  4
+
+static uint8_t const keycode2ascii[128][2] =  { HID_KEYCODE_TO_ASCII };
+
+// Each HID instance can has multiple reports
+static uint8_t _report_count[CFG_TUH_HID];
+static tuh_hid_report_info_t _report_info_arr[CFG_TUH_HID][MAX_REPORT];
+
+static void process_kbd_report(hid_keyboard_report_t const *report);
+static void process_mouse_report(hid_mouse_report_t const * report);
+
+void hid_app_task(void)
+{
+  // nothing to do
+}
+
+//--------------------------------------------------------------------+
+// TinyUSB Callbacks
+//--------------------------------------------------------------------+
+
+// Invoked when device with hid interface is mounted
+// Report descriptor is also available for use. tuh_hid_parse_report_descriptor()
+// can be used to parse common/simple enough descriptor.
+void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_report, uint16_t desc_len)
+{
+  printf("HID device address = %d, instance = %d is mounted\r\n", dev_addr, instance);
+
+  // Interface protocol
+  const char* protocol_str[] = { "None", "Keyboard", "Mouse" }; // hid_protocol_type_t
+  uint8_t const interface_protocol = tuh_hid_interface_protocol(dev_addr, instance);
+
+  // Parse report descriptor with built-in parser
+  _report_count[instance] = tuh_hid_parse_report_descriptor(_report_info_arr[instance], MAX_REPORT, desc_report, desc_len);
+  printf("HID has %u reports and interface protocol = %s\r\n", _report_count[instance], protocol_str[interface_protocol]);
+}
+
+// Invoked when device with hid interface is un-mounted
+void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance)
+{
+  printf("HID device address = %d, instance = %d is unmounted\r\n", dev_addr, instance);
+}
+
+// Invoked when received report from device via interrupt endpoint
+void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len)
+{
+  (void) dev_addr;
+
+  uint8_t const rpt_count = _report_count[instance];
+  tuh_hid_report_info_t* rpt_info_arr = _report_info_arr[instance];
+  tuh_hid_report_info_t* rpt_info = NULL;
+
+  if ( rpt_count == 1 && rpt_info_arr[0].report_id == 0)
+  {
+    // Simple report without report ID as 1st byte
+    rpt_info = &rpt_info_arr[0];
+  }else
+  {
+    // Composite report, 1st byte is report ID, data starts from 2nd byte
+    uint8_t const rpt_id = report[0];
+
+    // Find report id in the arrray
+    for(uint8_t i=0; i<rpt_count; i++)
+    {
+      if (rpt_id == rpt_info_arr[i].report_id )
+      {
+        rpt_info = &rpt_info_arr[i];
+        break;
+      }
+    }
+
+    report++;
+    len--;
+  }
+
+  if (!rpt_info)
+  {
+    printf("Couldn't find the report info for this report !\r\n");
+    return;
+  }
+
+  if ( rpt_info->usage_page == HID_USAGE_PAGE_DESKTOP )
+  {
+    switch (rpt_info->usage)
+    {
+      case HID_USAGE_DESKTOP_KEYBOARD:
+        TU_LOG1("HID receive keyboard report\r\n");
+        // Assume keyboard follow boot report layout
+        process_kbd_report( (hid_keyboard_report_t const*) report );
+      break;
+
+      case HID_USAGE_DESKTOP_MOUSE:
+        TU_LOG1("HID receive mouse report\r\n");
+        // Assume mouse follow boot report layout
+        process_mouse_report( (hid_mouse_report_t const*) report );
+      break;
+
+      default: break;
+    }
+  }
+}
+
+//--------------------------------------------------------------------+
+// Keyboard
+//--------------------------------------------------------------------+
+
+// look up new key in previous keys
+static inline bool find_key_in_report(hid_keyboard_report_t const *report, uint8_t keycode)
+{
+  for(uint8_t i=0; i<6; i++)
+  {
+    if (report->keycode[i] == keycode)  return true;
+  }
+
+  return false;
+}
+
+static void process_kbd_report(hid_keyboard_report_t const *report)
+{
+  static hid_keyboard_report_t prev_report = { 0, 0, {0} }; // previous report to check key released
+
+  //------------- example code ignore control (non-printable) key affects -------------//
+  for(uint8_t i=0; i<6; i++)
+  {
+    if ( report->keycode[i] )
+    {
+      if ( find_key_in_report(&prev_report, report->keycode[i]) )
+      {
+        // exist in previous report means the current key is holding
+      }else
+      {
+        // not existed in previous report means the current key is pressed
+        bool const is_shift =  report->modifier & (KEYBOARD_MODIFIER_LEFTSHIFT | KEYBOARD_MODIFIER_RIGHTSHIFT);
+        uint8_t ch = keycode2ascii[report->keycode[i]][is_shift ? 1 : 0];
+        putchar(ch);
+        if ( ch == '\r' ) putchar('\n'); // added new line for enter key
+
+        fflush(stdout); // flush right away, else nanolib will wait for newline
+      }
+    }
+    // TODO example skips key released
+  }
+
+  prev_report = *report;
+}
+
+//--------------------------------------------------------------------+
+// Mouse
+//--------------------------------------------------------------------+
+
+void cursor_movement(int8_t x, int8_t y, int8_t wheel)
+{
+#if USE_ANSI_ESCAPE
+  // Move X using ansi escape
+  if ( x < 0)
+  {
+    printf(ANSI_CURSOR_BACKWARD(%d), (-x)); // move left
+  }else if ( x > 0)
+  {
+    printf(ANSI_CURSOR_FORWARD(%d), x); // move right
+  }
+
+  // Move Y using ansi escape
+  if ( y < 0)
+  {
+    printf(ANSI_CURSOR_UP(%d), (-y)); // move up
+  }else if ( y > 0)
+  {
+    printf(ANSI_CURSOR_DOWN(%d), y); // move down
+  }
+
+  // Scroll using ansi escape
+  if (wheel < 0)
+  {
+    printf(ANSI_SCROLL_UP(%d), (-wheel)); // scroll up
+  }else if (wheel > 0)
+  {
+    printf(ANSI_SCROLL_DOWN(%d), wheel); // scroll down
+  }
+
+  printf("\r\n");
+#else
+  printf("(%d %d %d)\r\n", x, y, wheel);
+#endif
+}
+
+static void process_mouse_report(hid_mouse_report_t const * report)
+{
+  static hid_mouse_report_t prev_report = { 0 };
+
+  //------------- button state  -------------//
+  uint8_t button_changed_mask = report->buttons ^ prev_report.buttons;
+  if ( button_changed_mask & report->buttons)
+  {
+    printf(" %c%c%c ",
+       report->buttons & MOUSE_BUTTON_LEFT   ? 'L' : '-',
+       report->buttons & MOUSE_BUTTON_MIDDLE ? 'M' : '-',
+       report->buttons & MOUSE_BUTTON_RIGHT  ? 'R' : '-');
+  }
+
+  //------------- cursor movement -------------//
+  cursor_movement(report->x, report->y, report->wheel);
+}

+ 0 - 59
examples/host/cdc_msc_hid/src/keyboard_helper.h

@@ -1,59 +0,0 @@
-#ifndef KEYBOARD_HELPER_H
-#define KEYBAORD_HELPER_H
-
-#include <stdbool.h>
-#include <stdint.h>
-
-#include "tusb.h"
-
-// look up new key in previous keys
-inline bool find_key_in_report(hid_keyboard_report_t const *p_report, uint8_t keycode)
-{
-  for(uint8_t i = 0; i < 6; i++)
-  {
-    if (p_report->keycode[i] == keycode)  return true;
-  }
-
-  return false;
-}
-
-inline uint8_t keycode_to_ascii(uint8_t modifier, uint8_t keycode)
-{
-  return keycode > 128 ? 0 :
-    hid_keycode_to_ascii_tbl [keycode][modifier & (KEYBOARD_MODIFIER_LEFTSHIFT | KEYBOARD_MODIFIER_RIGHTSHIFT) ? 1 : 0];
-}
-
-void print_kbd_report(hid_keyboard_report_t *prev_report, hid_keyboard_report_t const *new_report)
-{
-
-  printf("Report: ");
-  uint8_t c;
-
-  // I assume it's possible to have up to 6 keypress events per report?
-  for (uint8_t i = 0; i < 6; i++)
-  {
-    // Check for key presses
-    if (new_report->keycode[i])
-    {
-      // If not in prev report then it is newly pressed
-      if ( !find_key_in_report(prev_report, new_report->keycode[i]) )
-        c = keycode_to_ascii(new_report->modifier, new_report->keycode[i]);
-        printf("press %c ", c);
-    }
-
-    // Check for key depresses (i.e. was present in prev report but not here)
-    if (prev_report->keycode[i])
-    {
-      // If not present in the current report then depressed
-      if (!find_key_in_report(new_report, prev_report->keycode[i]))
-      {
-        c = keycode_to_ascii(prev_report->modifier, prev_report->keycode[i]);
-        printf("depress %c ", c);
-      }
-    }
-  }
-
-  printf("\n");
-}
-
-#endif

+ 14 - 195
examples/host/cdc_msc_hid/src/main.c

@@ -37,7 +37,7 @@ void print_greeting(void);
 void led_blinking_task(void);
 
 extern void cdc_task(void);
-extern void hid_task(void);
+extern void hid_app_task(void);
 
 /*------------- MAIN -------------*/
 int main(void)
@@ -51,15 +51,14 @@ int main(void)
   {
     // tinyusb host task
     tuh_task();
-
     led_blinking_task();
 
 #if CFG_TUH_CDC
     cdc_task();
 #endif
 
-#if CFG_TUH_HID_KEYBOARD || CFG_TUH_HID_MOUSE
-    hid_task();
+#if CFG_TUH_HID
+    hid_app_task();
 #endif
   }
 
@@ -107,184 +106,11 @@ void cdc_task(void)
 #endif
 
 //--------------------------------------------------------------------+
-// USB HID
+// TinyUSB Callbacks
 //--------------------------------------------------------------------+
-#if CFG_TUH_HID_KEYBOARD
-
-CFG_TUSB_MEM_SECTION static hid_keyboard_report_t usb_keyboard_report;
-uint8_t const keycode2ascii[128][2] =  { HID_KEYCODE_TO_ASCII };
-
-// look up new key in previous keys
-static inline bool find_key_in_report(hid_keyboard_report_t const *p_report, uint8_t keycode)
-{
-  for(uint8_t i=0; i<6; i++)
-  {
-    if (p_report->keycode[i] == keycode)  return true;
-  }
-
-  return false;
-}
-
-static inline void process_kbd_report(hid_keyboard_report_t const *p_new_report)
-{
-  static hid_keyboard_report_t prev_report = { 0, 0, {0} }; // previous report to check key released
-
-  //------------- example code ignore control (non-printable) key affects -------------//
-  for(uint8_t i=0; i<6; i++)
-  {
-    if ( p_new_report->keycode[i] )
-    {
-      if ( find_key_in_report(&prev_report, p_new_report->keycode[i]) )
-      {
-        // exist in previous report means the current key is holding
-      }else
-      {
-        // not existed in previous report means the current key is pressed
-        bool const is_shift =  p_new_report->modifier & (KEYBOARD_MODIFIER_LEFTSHIFT | KEYBOARD_MODIFIER_RIGHTSHIFT);
-        uint8_t ch = keycode2ascii[p_new_report->keycode[i]][is_shift ? 1 : 0];
-        putchar(ch);
-        if ( ch == '\r' ) putchar('\n'); // added new line for enter key
-
-        fflush(stdout); // flush right away, else nanolib will wait for newline
-      }
-    }
-    // TODO example skips key released
-  }
-
-  prev_report = *p_new_report;
-}
-
-void tuh_hid_keyboard_mounted_cb(uint8_t dev_addr)
-{
-  // application set-up
-  printf("A Keyboard device (address %d) is mounted\r\n", dev_addr);
-
-  tuh_hid_keyboard_get_report(dev_addr, &usb_keyboard_report);
-}
-
-void tuh_hid_keyboard_unmounted_cb(uint8_t dev_addr)
-{
-  // application tear-down
-  printf("A Keyboard device (address %d) is unmounted\r\n", dev_addr);
-}
-
-// invoked ISR context
-void tuh_hid_keyboard_isr(uint8_t dev_addr, xfer_result_t event)
-{
-  (void) dev_addr;
-  (void) event;
-}
-
-#endif
-
-#if CFG_TUH_HID_MOUSE
-
-CFG_TUSB_MEM_SECTION static hid_mouse_report_t usb_mouse_report;
-
-void cursor_movement(int8_t x, int8_t y, int8_t wheel)
-{
-  //------------- X -------------//
-  if ( x < 0)
-  {
-    printf(ANSI_CURSOR_BACKWARD(%d), (-x)); // move left
-  }else if ( x > 0)
-  {
-    printf(ANSI_CURSOR_FORWARD(%d), x); // move right
-  }else { }
-
-  //------------- Y -------------//
-  if ( y < 0)
-  {
-    printf(ANSI_CURSOR_UP(%d), (-y)); // move up
-  }else if ( y > 0)
-  {
-    printf(ANSI_CURSOR_DOWN(%d), y); // move down
-  }else { }
-
-  //------------- wheel -------------//
-  if (wheel < 0)
-  {
-    printf(ANSI_SCROLL_UP(%d), (-wheel)); // scroll up
-  }else if (wheel > 0)
-  {
-    printf(ANSI_SCROLL_DOWN(%d), wheel); // scroll down
-  }else { }
-}
 
-static inline void process_mouse_report(hid_mouse_report_t const * p_report)
-{
-  static hid_mouse_report_t prev_report = { 0 };
-
-  //------------- button state  -------------//
-  uint8_t button_changed_mask = p_report->buttons ^ prev_report.buttons;
-  if ( button_changed_mask & p_report->buttons)
-  {
-    printf(" %c%c%c ",
-       p_report->buttons & MOUSE_BUTTON_LEFT   ? 'L' : '-',
-       p_report->buttons & MOUSE_BUTTON_MIDDLE ? 'M' : '-',
-       p_report->buttons & MOUSE_BUTTON_RIGHT  ? 'R' : '-');
-  }
-
-  //------------- cursor movement -------------//
-  cursor_movement(p_report->x, p_report->y, p_report->wheel);
-}
-
-
-void tuh_hid_mouse_mounted_cb(uint8_t dev_addr)
-{
-  // application set-up
-  printf("A Mouse device (address %d) is mounted\r\n", dev_addr);
-}
-
-void tuh_hid_mouse_unmounted_cb(uint8_t dev_addr)
-{
-  // application tear-down
-  printf("A Mouse device (address %d) is unmounted\r\n", dev_addr);
-}
-
-// invoked ISR context
-void tuh_hid_mouse_isr(uint8_t dev_addr, xfer_result_t event)
-{
-  (void) dev_addr;
-  (void) event;
-}
-#endif
-
-
-
-void hid_task(void)
-{
-  uint8_t const addr = 1;
-
-#if CFG_TUH_HID_KEYBOARD
-  if ( tuh_hid_keyboard_is_mounted(addr) )
-  {
-    if ( !tuh_hid_keyboard_is_busy(addr) )
-    {
-      process_kbd_report(&usb_keyboard_report);
-      tuh_hid_keyboard_get_report(addr, &usb_mouse_report);
-    }
-  }
-#endif
-
-#if CFG_TUH_HID_MOUSE
-  if ( tuh_hid_mouse_is_mounted(addr) )
-  {
-    if ( !tuh_hid_mouse_is_busy(addr) )
-    {
-      process_mouse_report(&usb_mouse_report);
-      tuh_hid_mouse_get_report(addr, &usb_mouse_report);
-    }
-  }
-#endif
-}
-
-//--------------------------------------------------------------------+
-// tinyusb callbacks
 //--------------------------------------------------------------------+
-
-//--------------------------------------------------------------------+
-// BLINKING TASK
+// Blinking Task
 //--------------------------------------------------------------------+
 void led_blinking_task(void)
 {
@@ -310,25 +136,18 @@ void print_greeting(void)
   {
       [OPT_OS_NONE]      = "None",
       [OPT_OS_FREERTOS]  = "FreeRTOS",
+      [OPT_OS_MYNEWT]    = "Mynewt OS",
+      [OPT_OS_CUSTOM]    = "Custom OS implemnted by application",
+      [OPT_OS_PICO]      = "Raspberry Pi Pico SDK",
+      [OPT_OS_RTTHREAD]  = "RT-Thread"
   };
 
-  printf("--------------------------------------------------------------------\r\n");
-  printf("- Host example\r\n");
-  printf("- if you find any bugs or get any questions, feel free to file an\r\n");
-  printf("- issue at https://github.com/hathach/tinyusb\r\n");
-  printf("--------------------------------------------------------------------\r\n\r\n");
+  printf("----------------------------------------------------\r\n");
+  printf("TinyUSB Host Example\r\n");
+  printf("If you find any bugs or problems, feel free to open\r\n");
+  printf("an issue at https://github.com/hathach/tinyusb\r\n");
+  printf("----------------------------------------------------\r\n\r\n");
 
   printf("This Host demo is configured to support:\r\n");
   printf("  - RTOS = %s\r\n", rtos_name[CFG_TUSB_OS]);
-
-#if CFG_TUH_CDC
-  printf("  - Communication Device Class\r\n");
-#endif
-
-#if CFG_TUH_MSC
-  printf("  - Mass Storage\r\n");
-#endif
-
-//  if (CFG_TUH_HID_KEYBOARD ) puts("  - HID Keyboard");
-//  if (CFG_TUH_HID_MOUSE    ) puts("  - HID Mouse");
 }

+ 1 - 1
examples/host/cdc_msc_hid/src/msc_app.c

@@ -80,7 +80,7 @@ void tuh_msc_mount_cb(uint8_t dev_addr)
 //  }
 }
 
-void tuh_msc_unmount_cb(uint8_t dev_addr)
+void tuh_msc_umount_cb(uint8_t dev_addr)
 {
   (void) dev_addr;
   printf("A MassStorage device is unmounted\r\n");

+ 8 - 3
examples/host/cdc_msc_hid/src/tusb_config.h

@@ -71,16 +71,21 @@
 // CONFIGURATION
 //--------------------------------------------------------------------
 
+// Size of buffer to hold descriptors and other data used for enumeration
+#define CFG_TUH_ENUMERATION_BUFSZIE 256
+
 #define CFG_TUH_HUB                 1
 #define CFG_TUH_CDC                 1
-#define CFG_TUH_HID_KEYBOARD        1
-#define CFG_TUH_HID_MOUSE           1
-#define CFG_TUSB_HOST_HID_GENERIC   0 // (not yet supported)
+#define CFG_TUH_HID                 2
 #define CFG_TUH_MSC                 1
 #define CFG_TUH_VENDOR              0
 
 #define CFG_TUSB_HOST_DEVICE_MAX    (CFG_TUH_HUB ? 5 : 1) // normal hub has 4 ports
 
+//------------- HID -------------//
+
+#define CFG_TUH_HID_EP_BUFSIZE      64
+
 #ifdef __cplusplus
  }
 #endif

+ 1 - 1
src/class/audio/audio_device.c

@@ -496,7 +496,7 @@ static bool audiod_rx_done_cb(uint8_t rhport, audiod_function_t* audio, uint16_t
   if (tud_audio_rx_done_pre_read_cb || tud_audio_rx_done_post_read_cb)
   {
     idx_audio_fct = audiod_get_audio_fct_idx(audio);
-    TU_VERIFY(audiod_get_AS_interface_index(audio->ep_in_as_intf_num, audio, &idxItf, &dummy2));
+    TU_VERIFY(audiod_get_AS_interface_index(audio->ep_out_as_intf_num, audio, &idxItf, &dummy2));
   }
 
   // Call a weak callback here - a possibility for user to get informed an audio packet was received and data gets now loaded into EP FIFO (or decoded into support RX software FIFO)

+ 103 - 50
src/class/hid/hid.h

@@ -62,15 +62,15 @@ typedef enum
 {
   HID_SUBCLASS_NONE = 0, ///< No Subclass
   HID_SUBCLASS_BOOT = 1  ///< Boot Interface Subclass
-}hid_subclass_type_t;
+}hid_subclass_enum_t;
 
-/// HID Protocol
+/// HID Interface Protocol
 typedef enum
 {
-  HID_PROTOCOL_NONE     = 0, ///< None
-  HID_PROTOCOL_KEYBOARD = 1, ///< Keyboard
-  HID_PROTOCOL_MOUSE    = 2  ///< Mouse
-}hid_protocol_type_t;
+  HID_ITF_PROTOCOL_NONE     = 0, ///< None
+  HID_ITF_PROTOCOL_KEYBOARD = 1, ///< Keyboard
+  HID_ITF_PROTOCOL_MOUSE    = 2  ///< Mouse
+}hid_interface_protocol_enum_t;
 
 /// HID Descriptor Type
 typedef enum
@@ -78,7 +78,7 @@ typedef enum
   HID_DESC_TYPE_HID      = 0x21, ///< HID Descriptor
   HID_DESC_TYPE_REPORT   = 0x22, ///< Report Descriptor
   HID_DESC_TYPE_PHYSICAL = 0x23  ///< Physical Descriptor
-}hid_descriptor_type_t;
+}hid_descriptor_enum_t;
 
 /// HID Request Report Type
 typedef enum
@@ -98,9 +98,9 @@ typedef enum
   HID_REQ_CONTROL_SET_REPORT   = 0x09, ///< Set Report
   HID_REQ_CONTROL_SET_IDLE     = 0x0a, ///< Set Idle
   HID_REQ_CONTROL_SET_PROTOCOL = 0x0b  ///< Set Protocol
-}hid_request_type_t;
+}hid_request_enum_t;
 
-/// HID Country Code
+/// HID Local Code
 typedef enum
 {
   HID_LOCAL_NotSupported = 0   , ///< NotSupported
@@ -139,7 +139,14 @@ typedef enum
   HID_LOCAL_US                 , ///< US
   HID_LOCAL_Yugoslavia         , ///< Yugoslavia
   HID_LOCAL_Turkish_F            ///< Turkish-F
-} hid_country_code_t;
+} hid_local_enum_t;
+
+// HID protocol value used by GetProtocol / SetProtocol
+typedef enum
+{
+  HID_PROTOCOL_BOOT = 0,
+  HID_PROTOCOL_REPORT = 1
+} hid_protocol_mode_enum_t;
 
 /** @} */
 
@@ -479,6 +486,7 @@ typedef enum
 //--------------------------------------------------------------------+
 // REPORT DESCRIPTOR
 //--------------------------------------------------------------------+
+
 //------------- ITEM & TAG -------------//
 #define HID_REPORT_DATA_0(data)
 #define HID_REPORT_DATA_1(data) , data
@@ -488,18 +496,31 @@ typedef enum
 #define HID_REPORT_ITEM(data, tag, type, size) \
   (((tag) << 4) | ((type) << 2) | (size)) HID_REPORT_DATA_##size(data)
 
-#define RI_TYPE_MAIN   0
-#define RI_TYPE_GLOBAL 1
-#define RI_TYPE_LOCAL  2
+// Report Item Types
+enum {
+  RI_TYPE_MAIN   = 0,
+  RI_TYPE_GLOBAL = 1,
+  RI_TYPE_LOCAL  = 2
+};
+
+//------------- Main Items - HID 1.11 section 6.2.2.4 -------------//
+
+// Report Item Main group
+enum {
+  RI_MAIN_INPUT          = 8,
+  RI_MAIN_OUTPUT         = 9,
+  RI_MAIN_COLLECTION     = 10,
+  RI_MAIN_FEATURE        = 11,
+  RI_MAIN_COLLECTION_END = 12
+};
 
-//------------- MAIN ITEMS 6.2.2.4 -------------//
-#define HID_INPUT(x)           HID_REPORT_ITEM(x,  8, RI_TYPE_MAIN, 1)
-#define HID_OUTPUT(x)          HID_REPORT_ITEM(x,  9, RI_TYPE_MAIN, 1)
-#define HID_COLLECTION(x)      HID_REPORT_ITEM(x, 10, RI_TYPE_MAIN, 1)
-#define HID_FEATURE(x)         HID_REPORT_ITEM(x, 11, RI_TYPE_MAIN, 1)
-#define HID_COLLECTION_END     HID_REPORT_ITEM(x, 12, RI_TYPE_MAIN, 0)
+#define HID_INPUT(x)           HID_REPORT_ITEM(x, RI_MAIN_INPUT         , RI_TYPE_MAIN, 1)
+#define HID_OUTPUT(x)          HID_REPORT_ITEM(x, RI_MAIN_OUTPUT        , RI_TYPE_MAIN, 1)
+#define HID_COLLECTION(x)      HID_REPORT_ITEM(x, RI_MAIN_COLLECTION    , RI_TYPE_MAIN, 1)
+#define HID_FEATURE(x)         HID_REPORT_ITEM(x, RI_MAIN_FEATURE       , RI_TYPE_MAIN, 1)
+#define HID_COLLECTION_END     HID_REPORT_ITEM(x, RI_MAIN_COLLECTION_END, RI_TYPE_MAIN, 0)
 
-//------------- INPUT, OUTPUT, FEATURE 6.2.2.5 -------------//
+//------------- Input, Output, Feature - HID 1.11 section 6.2.2.5 -------------//
 #define HID_DATA             (0<<0)
 #define HID_CONSTANT         (1<<0)
 
@@ -527,7 +548,7 @@ typedef enum
 #define HID_BITFIELD         (0<<8)
 #define HID_BUFFERED_BYTES   (1<<8)
 
-//------------- COLLECTION ITEM 6.2.2.6 -------------//
+//------------- Collection Item - HID 1.11 section 6.2.2.6 -------------//
 enum {
   HID_COLLECTION_PHYSICAL = 0,
   HID_COLLECTION_APPLICATION,
@@ -538,49 +559,81 @@ enum {
   HID_COLLECTION_USAGE_MODIFIER
 };
 
-//------------- GLOBAL ITEMS 6.2.2.7 -------------//
-#define HID_USAGE_PAGE(x)         HID_REPORT_ITEM(x, 0, RI_TYPE_GLOBAL, 1)
-#define HID_USAGE_PAGE_N(x, n)    HID_REPORT_ITEM(x, 0, RI_TYPE_GLOBAL, n)
+//------------- Global Items - HID 1.11 section 6.2.2.7 -------------//
+
+// Report Item Global group
+enum {
+  RI_GLOBAL_USAGE_PAGE    = 0,
+  RI_GLOBAL_LOGICAL_MIN   = 1,
+  RI_GLOBAL_LOGICAL_MAX   = 2,
+  RI_GLOBAL_PHYSICAL_MIN  = 3,
+  RI_GLOBAL_PHYSICAL_MAX  = 4,
+  RI_GLOBAL_UNIT_EXPONENT = 5,
+  RI_GLOBAL_UNIT          = 6,
+  RI_GLOBAL_REPORT_SIZE   = 7,
+  RI_GLOBAL_REPORT_ID     = 8,
+  RI_GLOBAL_REPORT_COUNT  = 9,
+  RI_GLOBAL_PUSH          = 10,
+  RI_GLOBAL_POP           = 11
+};
+
+#define HID_USAGE_PAGE(x)         HID_REPORT_ITEM(x, RI_GLOBAL_USAGE_PAGE, RI_TYPE_GLOBAL, 1)
+#define HID_USAGE_PAGE_N(x, n)    HID_REPORT_ITEM(x, RI_GLOBAL_USAGE_PAGE, RI_TYPE_GLOBAL, n)
 
-#define HID_LOGICAL_MIN(x)        HID_REPORT_ITEM(x, 1, RI_TYPE_GLOBAL, 1)
-#define HID_LOGICAL_MIN_N(x, n)   HID_REPORT_ITEM(x, 1, RI_TYPE_GLOBAL, n)
+#define HID_LOGICAL_MIN(x)        HID_REPORT_ITEM(x, RI_GLOBAL_LOGICAL_MIN, RI_TYPE_GLOBAL, 1)
+#define HID_LOGICAL_MIN_N(x, n)   HID_REPORT_ITEM(x, RI_GLOBAL_LOGICAL_MIN, RI_TYPE_GLOBAL, n)
 
-#define HID_LOGICAL_MAX(x)        HID_REPORT_ITEM(x, 2, RI_TYPE_GLOBAL, 1)
-#define HID_LOGICAL_MAX_N(x, n)   HID_REPORT_ITEM(x, 2, RI_TYPE_GLOBAL, n)
+#define HID_LOGICAL_MAX(x)        HID_REPORT_ITEM(x, RI_GLOBAL_LOGICAL_MAX, RI_TYPE_GLOBAL, 1)
+#define HID_LOGICAL_MAX_N(x, n)   HID_REPORT_ITEM(x, RI_GLOBAL_LOGICAL_MAX, RI_TYPE_GLOBAL, n)
 
-#define HID_PHYSICAL_MIN(x)       HID_REPORT_ITEM(x, 3, RI_TYPE_GLOBAL, 1)
-#define HID_PHYSICAL_MIN_N(x, n)  HID_REPORT_ITEM(x, 3, RI_TYPE_GLOBAL, n)
+#define HID_PHYSICAL_MIN(x)       HID_REPORT_ITEM(x, RI_GLOBAL_PHYSICAL_MIN, RI_TYPE_GLOBAL, 1)
+#define HID_PHYSICAL_MIN_N(x, n)  HID_REPORT_ITEM(x, RI_GLOBAL_PHYSICAL_MIN, RI_TYPE_GLOBAL, n)
 
-#define HID_PHYSICAL_MAX(x)       HID_REPORT_ITEM(x, 4, RI_TYPE_GLOBAL, 1)
-#define HID_PHYSICAL_MAX_N(x, n)  HID_REPORT_ITEM(x, 4, RI_TYPE_GLOBAL, n)
+#define HID_PHYSICAL_MAX(x)       HID_REPORT_ITEM(x, RI_GLOBAL_PHYSICAL_MAX, RI_TYPE_GLOBAL, 1)
+#define HID_PHYSICAL_MAX_N(x, n)  HID_REPORT_ITEM(x, RI_GLOBAL_PHYSICAL_MAX, RI_TYPE_GLOBAL, n)
 
-#define HID_UNIT_EXPONENT(x)      HID_REPORT_ITEM(x, 5, RI_TYPE_GLOBAL, 1)
-#define HID_UNIT_EXPONENT_N(x, n) HID_REPORT_ITEM(x, 5, RI_TYPE_GLOBAL, n)
+#define HID_UNIT_EXPONENT(x)      HID_REPORT_ITEM(x, RI_GLOBAL_UNIT_EXPONENT, RI_TYPE_GLOBAL, 1)
+#define HID_UNIT_EXPONENT_N(x, n) HID_REPORT_ITEM(x, RI_GLOBAL_UNIT_EXPONENT, RI_TYPE_GLOBAL, n)
 
-#define HID_UNIT(x)               HID_REPORT_ITEM(x, 6, RI_TYPE_GLOBAL, 1)
-#define HID_UNIT_N(x, n)          HID_REPORT_ITEM(x, 6, RI_TYPE_GLOBAL, n)
+#define HID_UNIT(x)               HID_REPORT_ITEM(x, RI_GLOBAL_UNIT, RI_TYPE_GLOBAL, 1)
+#define HID_UNIT_N(x, n)          HID_REPORT_ITEM(x, RI_GLOBAL_UNIT, RI_TYPE_GLOBAL, n)
 
-#define HID_REPORT_SIZE(x)        HID_REPORT_ITEM(x, 7, RI_TYPE_GLOBAL, 1)
-#define HID_REPORT_SIZE_N(x, n)   HID_REPORT_ITEM(x, 7, RI_TYPE_GLOBAL, n)
+#define HID_REPORT_SIZE(x)        HID_REPORT_ITEM(x, RI_GLOBAL_REPORT_SIZE, RI_TYPE_GLOBAL, 1)
+#define HID_REPORT_SIZE_N(x, n)   HID_REPORT_ITEM(x, RI_GLOBAL_REPORT_SIZE, RI_TYPE_GLOBAL, n)
 
-#define HID_REPORT_ID(x)          HID_REPORT_ITEM(x, 8, RI_TYPE_GLOBAL, 1),
-#define HID_REPORT_ID_N(x)        HID_REPORT_ITEM(x, 8, RI_TYPE_GLOBAL, n),
+#define HID_REPORT_ID(x)          HID_REPORT_ITEM(x, RI_GLOBAL_REPORT_ID, RI_TYPE_GLOBAL, 1),
+#define HID_REPORT_ID_N(x)        HID_REPORT_ITEM(x, RI_GLOBAL_REPORT_ID, RI_TYPE_GLOBAL, n),
 
-#define HID_REPORT_COUNT(x)       HID_REPORT_ITEM(x, 9, RI_TYPE_GLOBAL, 1)
-#define HID_REPORT_COUNT_N(x, n)  HID_REPORT_ITEM(x, 9, RI_TYPE_GLOBAL, n)
+#define HID_REPORT_COUNT(x)       HID_REPORT_ITEM(x, RI_GLOBAL_REPORT_COUNT, RI_TYPE_GLOBAL, 1)
+#define HID_REPORT_COUNT_N(x, n)  HID_REPORT_ITEM(x, RI_GLOBAL_REPORT_COUNT, RI_TYPE_GLOBAL, n)
 
-#define HID_PUSH                  HID_REPORT_ITEM(x, 10, RI_TYPE_GLOBAL, 0)
-#define HID_POP                   HID_REPORT_ITEM(x, 11, RI_TYPE_GLOBAL, 0)
+#define HID_PUSH                  HID_REPORT_ITEM(x, RI_GLOBAL_PUSH, RI_TYPE_GLOBAL, 0)
+#define HID_POP                   HID_REPORT_ITEM(x, RI_GLOBAL_POP, RI_TYPE_GLOBAL, 0)
 
 //------------- LOCAL ITEMS 6.2.2.8 -------------//
-#define HID_USAGE(x)              HID_REPORT_ITEM(x, 0, RI_TYPE_LOCAL, 1)
-#define HID_USAGE_N(x, n)         HID_REPORT_ITEM(x, 0, RI_TYPE_LOCAL, n)
 
-#define HID_USAGE_MIN(x)          HID_REPORT_ITEM(x, 1, RI_TYPE_LOCAL, 1)
-#define HID_USAGE_MIN_N(x, n)     HID_REPORT_ITEM(x, 1, RI_TYPE_LOCAL, n)
+enum {
+  RI_LOCAL_USAGE            = 0,
+  RI_LOCAL_USAGE_MIN        = 1,
+  RI_LOCAL_USAGE_MAX        = 2,
+  RI_LOCAL_DESIGNATOR_INDEX = 3,
+  RI_LOCAL_DESIGNATOR_MIN   = 4,
+  RI_LOCAL_DESIGNATOR_MAX   = 5,
+  // 6 is reserved
+  RI_LOCAL_STRING_INDEX     = 7,
+  RI_LOCAL_STRING_MIN       = 8,
+  RI_LOCAL_STRING_MAX       = 9,
+  RI_LOCAL_DELIMITER        = 10,
+};
+
+#define HID_USAGE(x)              HID_REPORT_ITEM(x, RI_LOCAL_USAGE, RI_TYPE_LOCAL, 1)
+#define HID_USAGE_N(x, n)         HID_REPORT_ITEM(x, RI_LOCAL_USAGE, RI_TYPE_LOCAL, n)
+
+#define HID_USAGE_MIN(x)          HID_REPORT_ITEM(x, RI_LOCAL_USAGE_MIN, RI_TYPE_LOCAL, 1)
+#define HID_USAGE_MIN_N(x, n)     HID_REPORT_ITEM(x, RI_LOCAL_USAGE_MIN, RI_TYPE_LOCAL, n)
 
-#define HID_USAGE_MAX(x)          HID_REPORT_ITEM(x, 2, RI_TYPE_LOCAL, 1)
-#define HID_USAGE_MAX_N(x, n)     HID_REPORT_ITEM(x, 2, RI_TYPE_LOCAL, n)
+#define HID_USAGE_MAX(x)          HID_REPORT_ITEM(x, RI_LOCAL_USAGE_MAX, RI_TYPE_LOCAL, 1)
+#define HID_USAGE_MAX_N(x, n)     HID_REPORT_ITEM(x, RI_LOCAL_USAGE_MAX, RI_TYPE_LOCAL, n)
 
 //--------------------------------------------------------------------+
 // Usage Table

+ 34 - 29
src/class/hid/hid_device.c

@@ -43,14 +43,17 @@ typedef struct
   uint8_t itf_num;
   uint8_t ep_in;
   uint8_t ep_out;        // optional Out endpoint
-  uint8_t boot_protocol; // Boot mouse or keyboard
-  bool    boot_mode;     // default = false (Report)
+  uint8_t itf_protocol;  // Boot mouse or keyboard
+
+  uint8_t protocol_mode; // Boot (0) or Report protocol (1)
   uint8_t idle_rate;     // up to application to handle idle rate
   uint16_t report_desc_len;
 
   CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUD_HID_EP_BUFSIZE];
   CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_HID_EP_BUFSIZE];
 
+  // TODO save hid descriptor since host can specifically request this after enumeration
+  // Note: HID descriptor may be not available from application after enumeration
   tusb_hid_descriptor_hid_t const * hid_descriptor;
 } hidd_interface_t;
 
@@ -70,16 +73,16 @@ static inline uint8_t get_index_by_itfnum(uint8_t itf_num)
 //--------------------------------------------------------------------+
 // APPLICATION API
 //--------------------------------------------------------------------+
-bool tud_hid_n_ready(uint8_t itf)
+bool tud_hid_n_ready(uint8_t instance)
 {
-  uint8_t const ep_in = _hidd_itf[itf].ep_in;
+  uint8_t const ep_in = _hidd_itf[instance].ep_in;
   return tud_ready() && (ep_in != 0) && !usbd_edpt_busy(TUD_OPT_RHPORT, ep_in);
 }
 
-bool tud_hid_n_report(uint8_t itf, uint8_t report_id, void const* report, uint8_t len)
+bool tud_hid_n_report(uint8_t instance, uint8_t report_id, void const* report, uint8_t len)
 {
   uint8_t const rhport = 0;
-  hidd_interface_t * p_hid = &_hidd_itf[itf];
+  hidd_interface_t * p_hid = &_hidd_itf[instance];
 
   // claim endpoint
   TU_VERIFY( usbd_edpt_claim(rhport, p_hid->ep_in) );
@@ -102,12 +105,17 @@ bool tud_hid_n_report(uint8_t itf, uint8_t report_id, void const* report, uint8_
   return usbd_edpt_xfer(TUD_OPT_RHPORT, p_hid->ep_in, p_hid->epin_buf, len);
 }
 
-bool tud_hid_n_boot_mode(uint8_t itf)
+uint8_t tud_hid_n_interface_protocol(uint8_t instance)
+{
+  return _hidd_itf[instance].itf_protocol;
+}
+
+uint8_t tud_hid_n_get_protocol(uint8_t instance)
 {
-  return _hidd_itf[itf].boot_mode;
+  return _hidd_itf[instance].protocol_mode;
 }
 
-bool tud_hid_n_keyboard_report(uint8_t itf, uint8_t report_id, uint8_t modifier, uint8_t keycode[6])
+bool tud_hid_n_keyboard_report(uint8_t instance, uint8_t report_id, uint8_t modifier, uint8_t keycode[6])
 {
   hid_keyboard_report_t report;
 
@@ -121,10 +129,10 @@ bool tud_hid_n_keyboard_report(uint8_t itf, uint8_t report_id, uint8_t modifier,
     tu_memclr(report.keycode, 6);
   }
 
-  return tud_hid_n_report(itf, report_id, &report, sizeof(report));
+  return tud_hid_n_report(instance, report_id, &report, sizeof(report));
 }
 
-bool tud_hid_n_mouse_report(uint8_t itf, uint8_t report_id,
+bool tud_hid_n_mouse_report(uint8_t instance, uint8_t report_id,
                             uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal)
 {
   hid_mouse_report_t report =
@@ -136,10 +144,10 @@ bool tud_hid_n_mouse_report(uint8_t itf, uint8_t report_id,
     .pan     = horizontal
   };
 
-  return tud_hid_n_report(itf, report_id, &report, sizeof(report));
+  return tud_hid_n_report(instance, report_id, &report, sizeof(report));
 }
 
-bool tud_hid_n_gamepad_report(uint8_t itf, uint8_t report_id,
+bool tud_hid_n_gamepad_report(uint8_t instance, uint8_t report_id,
                               int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint16_t buttons)
 {
   hid_gamepad_report_t report =
@@ -154,7 +162,7 @@ bool tud_hid_n_gamepad_report(uint8_t itf, uint8_t report_id,
     .buttons = buttons,
   };
 
-  return tud_hid_n_report(itf, report_id, &report, sizeof(report));
+  return tud_hid_n_report(instance, report_id, &report, sizeof(report));
 }
 
 //--------------------------------------------------------------------+
@@ -203,9 +211,9 @@ uint16_t hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint1
   p_desc = tu_desc_next(p_desc);
   TU_ASSERT(usbd_open_edpt_pair(rhport, p_desc, desc_itf->bNumEndpoints, TUSB_XFER_INTERRUPT, &p_hid->ep_out, &p_hid->ep_in), 0);
 
-  if ( desc_itf->bInterfaceSubClass == HID_SUBCLASS_BOOT ) p_hid->boot_protocol = desc_itf->bInterfaceProtocol;
+  if ( desc_itf->bInterfaceSubClass == HID_SUBCLASS_BOOT ) p_hid->itf_protocol = desc_itf->bInterfaceProtocol;
 
-  p_hid->boot_mode = false; // default mode is REPORT
+  p_hid->protocol_mode = HID_PROTOCOL_REPORT; // Per Specs: default is report mode
   p_hid->itf_num   = desc_itf->bInterfaceNumber;
 
   // Use offsetof to avoid pointer to the odd/misaligned address
@@ -318,24 +326,21 @@ bool hidd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t
       case HID_REQ_CONTROL_GET_PROTOCOL:
         if ( stage == CONTROL_STAGE_SETUP )
         {
-          // 0 is Boot, 1 is Report protocol
-          uint8_t protocol = (uint8_t)(1-p_hid->boot_mode);
-          tud_control_xfer(rhport, request, &protocol, 1);
+          tud_control_xfer(rhport, request, &p_hid->protocol_mode, 1);
         }
       break;
 
       case HID_REQ_CONTROL_SET_PROTOCOL:
         if ( stage == CONTROL_STAGE_SETUP )
         {
-          // 0 is Boot, 1 is Report protocol
-          p_hid->boot_mode = 1 - request->wValue;
           tud_control_status(rhport, request);
         }
         else if ( stage == CONTROL_STAGE_ACK )
         {
-          if (tud_hid_boot_mode_cb)
+          p_hid->protocol_mode = (uint8_t) request->wValue;
+          if (tud_hid_set_protocol_cb)
           {
-            tud_hid_boot_mode_cb(hid_itf, p_hid->boot_mode);
+            tud_hid_set_protocol_cb(hid_itf, p_hid->protocol_mode);
           }
         }
       break;
@@ -354,29 +359,29 @@ bool hidd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_
 {
   (void) result;
 
-  uint8_t itf = 0;
+  uint8_t instance = 0;
   hidd_interface_t * p_hid = _hidd_itf;
 
   // Identify which interface to use
-  for (itf = 0; itf < CFG_TUD_HID; itf++)
+  for (instance = 0; instance < CFG_TUD_HID; instance++)
   {
-    p_hid = &_hidd_itf[itf];
+    p_hid = &_hidd_itf[instance];
     if ( (ep_addr == p_hid->ep_out) || (ep_addr == p_hid->ep_in) ) break;
   }
-  TU_ASSERT(itf < CFG_TUD_HID);
+  TU_ASSERT(instance < CFG_TUD_HID);
 
   // Sent report successfully
   if (ep_addr == p_hid->ep_in)
   {
     if (tud_hid_report_complete_cb)
     {
-      tud_hid_report_complete_cb(itf, p_hid->epin_buf, (uint8_t) xferred_bytes);
+      tud_hid_report_complete_cb(instance, p_hid->epin_buf, (uint8_t) xferred_bytes);
     }
   }
   // Received report
   else if (ep_addr == p_hid->ep_out)
   {
-    tud_hid_set_report_cb(itf, 0, HID_REPORT_TYPE_INVALID, p_hid->epout_buf, xferred_bytes);
+    tud_hid_set_report_cb(instance, 0, HID_REPORT_TYPE_INVALID, p_hid->epout_buf, xferred_bytes);
     TU_ASSERT(usbd_edpt_xfer(rhport, p_hid->ep_out, p_hid->epout_buf, sizeof(p_hid->epout_buf)));
   }
 

+ 38 - 22
src/class/hid/hid_device.h

@@ -50,39 +50,44 @@
 #endif
 
 //--------------------------------------------------------------------+
-// Application API (Multiple Ports)
+// Application API (Multiple Instances)
 // CFG_TUD_HID > 1
 //--------------------------------------------------------------------+
 
 // Check if the interface is ready to use
-bool tud_hid_n_ready(uint8_t itf);
+bool tud_hid_n_ready(uint8_t instance);
 
-// Check if current mode is Boot (true) or Report (false)
-bool tud_hid_n_boot_mode(uint8_t itf);
+// Get interface supported protocol (bInterfaceProtocol) check out hid_interface_protocol_enum_t for possible values
+uint8_t tud_hid_n_interface_protocol(uint8_t instance);
+
+// Get current active protocol: HID_PROTOCOL_BOOT (0) or HID_PROTOCOL_REPORT (1)
+uint8_t tud_hid_n_get_protocol(uint8_t instance);
 
 // Send report to host
-bool tud_hid_n_report(uint8_t itf, uint8_t report_id, void const* report, uint8_t len);
+bool tud_hid_n_report(uint8_t instance, uint8_t report_id, void const* report, uint8_t len);
 
 // KEYBOARD: convenient helper to send keyboard report if application
 // use template layout report as defined by hid_keyboard_report_t
-bool tud_hid_n_keyboard_report(uint8_t itf, uint8_t report_id, uint8_t modifier, uint8_t keycode[6]);
+bool tud_hid_n_keyboard_report(uint8_t instance, uint8_t report_id, uint8_t modifier, uint8_t keycode[6]);
 
 // MOUSE: convenient helper to send mouse report if application
 // use template layout report as defined by hid_mouse_report_t
-bool tud_hid_n_mouse_report(uint8_t itf, uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal);
+bool tud_hid_n_mouse_report(uint8_t instance, uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal);
 
 // Gamepad: convenient helper to send mouse report if application
 // use template layout report TUD_HID_REPORT_DESC_GAMEPAD
-bool tud_hid_n_gamepad_report(uint8_t itf, uint8_t report_id, int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint16_t buttons);
+bool tud_hid_n_gamepad_report(uint8_t instance, uint8_t report_id, int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint16_t buttons);
 
 //--------------------------------------------------------------------+
 // Application API (Single Port)
 //--------------------------------------------------------------------+
-static inline bool tud_hid_ready(void);
-static inline bool tud_hid_boot_mode(void);
-static inline bool tud_hid_report(uint8_t report_id, void const* report, uint8_t len);
-static inline bool tud_hid_keyboard_report(uint8_t report_id, uint8_t modifier, uint8_t keycode[6]);
-static inline bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal);
+static inline bool    tud_hid_ready(void);
+static inline uint8_t tud_hid_interface_protocol(void);
+static inline uint8_t tud_hid_get_protocol(void);
+static inline bool    tud_hid_report(uint8_t report_id, void const* report, uint8_t len);
+static inline bool    tud_hid_keyboard_report(uint8_t report_id, uint8_t modifier, uint8_t keycode[6]);
+static inline bool    tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal);
+static inline bool    tud_hid_gamepad_report(uint8_t report_id, int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint16_t buttons);
 
 //--------------------------------------------------------------------+
 // Callbacks (Weak is optional)
@@ -90,29 +95,30 @@ static inline bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8
 
 // Invoked when received GET HID REPORT DESCRIPTOR request
 // Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
-uint8_t const * tud_hid_descriptor_report_cb(uint8_t itf);
+uint8_t const * tud_hid_descriptor_report_cb(uint8_t instance);
 
 // Invoked when received GET_REPORT control request
 // Application must fill buffer report's content and return its length.
 // Return zero will cause the stack to STALL request
-uint16_t tud_hid_get_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen);
+uint16_t tud_hid_get_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen);
 
 // Invoked when received SET_REPORT control request or
 // received data on OUT endpoint ( Report ID = 0, Type = 0 )
-void tud_hid_set_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize);
+void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize);
 
-// Invoked when received SET_PROTOCOL request ( mode switch Boot <-> Report )
-TU_ATTR_WEAK void tud_hid_boot_mode_cb(uint8_t itf, uint8_t boot_mode);
+// Invoked when received SET_PROTOCOL request
+// protocol is either HID_PROTOCOL_BOOT (0) or HID_PROTOCOL_REPORT (1)
+TU_ATTR_WEAK void tud_hid_set_protocol_cb(uint8_t instance, uint8_t protocol);
 
 // Invoked when received SET_IDLE request. return false will stall the request
 // - Idle Rate = 0 : only send report if there is changes, i.e skip duplication
 // - Idle Rate > 0 : skip duplication, but send at least 1 report every idle rate (in unit of 4 ms).
-TU_ATTR_WEAK bool tud_hid_set_idle_cb(uint8_t itf, uint8_t idle_rate);
+TU_ATTR_WEAK bool tud_hid_set_idle_cb(uint8_t instance, uint8_t idle_rate);
 
 // Invoked when sent REPORT successfully to host
 // Application can use this to send the next report
 // Note: For composite reports, report[0] is report ID
-TU_ATTR_WEAK void tud_hid_report_complete_cb(uint8_t itf, uint8_t const* report, uint8_t len);
+TU_ATTR_WEAK void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, uint8_t len);
 
 
 //--------------------------------------------------------------------+
@@ -123,9 +129,14 @@ static inline bool tud_hid_ready(void)
   return tud_hid_n_ready(0);
 }
 
-static inline bool tud_hid_boot_mode(void)
+static inline uint8_t tud_hid_interface_protocol(void)
+{
+  return tud_hid_n_interface_protocol(0);
+}
+
+static inline uint8_t tud_hid_get_protocol(void)
 {
-  return tud_hid_n_boot_mode(0);
+  return tud_hid_n_get_protocol(0);
 }
 
 static inline bool tud_hid_report(uint8_t report_id, void const* report, uint8_t len)
@@ -143,6 +154,11 @@ static inline bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8
   return tud_hid_n_mouse_report(0, report_id, buttons, x, y, vertical, horizontal);
 }
 
+static inline bool  tud_hid_gamepad_report(uint8_t report_id, int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint16_t buttons)
+{
+  return tud_hid_n_gamepad_report(0, report_id, x, y, z, rz, rx, ry, hat, buttons);
+}
+
 /* --------------------------------------------------------------------+
  * HID Report Descriptor Template
  *

+ 454 - 176
src/class/hid/hid_host.c

@@ -26,7 +26,7 @@
 
 #include "tusb_option.h"
 
-#if (TUSB_OPT_HOST_ENABLED && HOST_CLASS_HID)
+#if (TUSB_OPT_HOST_ENABLED && CFG_TUH_HID)
 
 #include "common/tusb_common.h"
 #include "hid_host.h"
@@ -35,198 +35,279 @@
 // MACRO CONSTANT TYPEDEF
 //--------------------------------------------------------------------+
 
-typedef struct {
-  uint8_t  itf_num;
-  uint8_t  ep_in;
-  uint8_t  ep_out;
-  bool     valid;
+/*
+ "KEYBOARD"               : in_len=8 , out_len=1, usage_page=0x01, usage=0x06   # Generic Desktop, Keyboard
+ "MOUSE"                  : in_len=4 , out_len=0, usage_page=0x01, usage=0x02   # Generic Desktop, Mouse
+ "CONSUMER"               : in_len=2 , out_len=0, usage_page=0x0C, usage=0x01   # Consumer, Consumer Control
+ "SYS_CONTROL"            : in_len=1 , out_len=0, usage_page=0x01, usage=0x80   # Generic Desktop, Sys Control
+ "GAMEPAD"                : in_len=6 , out_len=0, usage_page=0x01, usage=0x05   # Generic Desktop, Game Pad
+ "DIGITIZER"              : in_len=5 , out_len=0, usage_page=0x0D, usage=0x02   # Digitizers, Pen
+ "XAC_COMPATIBLE_GAMEPAD" : in_len=3 , out_len=0, usage_page=0x01, usage=0x05   # Generic Desktop, Game Pad
+ "RAW"                    : in_len=64, out_len=0, usage_page=0xFFAF, usage=0xAF # Vendor 0xFFAF "Adafruit", 0xAF
+ */
+typedef struct
+{
+  uint8_t itf_num;
+  uint8_t ep_in;
+  uint8_t ep_out;
 
-  uint16_t report_size;
-}hidh_interface_t;
+  uint8_t itf_protocol;   // None, Keyboard, Mouse
+  uint8_t protocol_mode;  // Boot (0) or Report protocol (1)
 
-//--------------------------------------------------------------------+
-// HID Interface common functions
-//--------------------------------------------------------------------+
-static inline bool hidh_interface_open(uint8_t rhport, uint8_t dev_addr, uint8_t interface_number, tusb_desc_endpoint_t const *p_endpoint_desc, hidh_interface_t *p_hid)
-{
-  TU_ASSERT( usbh_edpt_open(rhport, dev_addr, p_endpoint_desc) );
+  uint8_t  report_desc_type;
+  uint16_t report_desc_len;
 
-  p_hid->ep_in       = p_endpoint_desc->bEndpointAddress;
-  p_hid->report_size = p_endpoint_desc->wMaxPacketSize.size; // TODO get size from report descriptor
-  p_hid->itf_num     = interface_number;
-  p_hid->valid       = true;
+  uint16_t epin_size;
+  uint16_t epout_size;
 
-  return true;
-}
+  uint8_t epin_buf[CFG_TUH_HID_EP_BUFSIZE];
+  uint8_t epout_buf[CFG_TUH_HID_EP_BUFSIZE];
+} hidh_interface_t;
 
-static inline void hidh_interface_close(hidh_interface_t *p_hid)
+typedef struct
 {
-  tu_memclr(p_hid, sizeof(hidh_interface_t));
-}
+  uint8_t inst_count;
+  hidh_interface_t instances[CFG_TUH_HID];
+} hidh_device_t;
 
-// called from public API need to validate parameters
-tusb_error_t hidh_interface_get_report(uint8_t dev_addr, void * report, hidh_interface_t *p_hid)
-{
-  //------------- parameters validation -------------//
-  // TODO change to use is configured function
-  TU_ASSERT(TUSB_DEVICE_STATE_CONFIGURED == tuh_device_get_state(dev_addr), TUSB_ERROR_DEVICE_NOT_READY);
-  TU_VERIFY(report, TUSB_ERROR_INVALID_PARA);
-  TU_ASSERT(!hcd_edpt_busy(dev_addr, p_hid->ep_in), TUSB_ERROR_INTERFACE_IS_BUSY);
+static hidh_device_t _hidh_dev[CFG_TUSB_HOST_DEVICE_MAX-1];
 
-  TU_ASSERT( usbh_edpt_xfer(dev_addr, p_hid->ep_in, report, p_hid->report_size) ) ;
+//------------- Internal prototypes -------------//
 
-  return TUSB_ERROR_NONE;
+// Get HID device & interface
+TU_ATTR_ALWAYS_INLINE static inline hidh_device_t* get_dev(uint8_t dev_addr);
+TU_ATTR_ALWAYS_INLINE static inline hidh_interface_t* get_instance(uint8_t dev_addr, uint8_t instance);
+static uint8_t get_instance_id_by_itfnum(uint8_t dev_addr, uint8_t itf);
+static uint8_t get_instance_id_by_epaddr(uint8_t dev_addr, uint8_t ep_addr);
+
+TU_ATTR_ALWAYS_INLINE static inline bool hidh_get_report(uint8_t dev_addr, hidh_interface_t* hid_itf)
+{
+  return usbh_edpt_xfer(dev_addr, hid_itf->ep_in, hid_itf->epin_buf, hid_itf->epin_size);
 }
 
 //--------------------------------------------------------------------+
-// KEYBOARD
+// Application API
 //--------------------------------------------------------------------+
-#if CFG_TUH_HID_KEYBOARD
 
-static hidh_interface_t keyboardh_data[CFG_TUSB_HOST_DEVICE_MAX]; // does not have addr0, index = dev_address-1
+uint8_t tuh_hid_instance_count(uint8_t dev_addr)
+{
+  return get_dev(dev_addr)->inst_count;
+}
 
-//------------- KEYBOARD PUBLIC API (parameter validation required) -------------//
-bool  tuh_hid_keyboard_is_mounted(uint8_t dev_addr)
+bool tuh_hid_mounted(uint8_t dev_addr, uint8_t instance)
 {
-  return tuh_device_is_configured(dev_addr) && (keyboardh_data[dev_addr-1].ep_in != 0);
+  hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
+  return (hid_itf->ep_in != 0) || (hid_itf->ep_out != 0);
 }
 
-tusb_error_t tuh_hid_keyboard_get_report(uint8_t dev_addr, void* p_report)
+uint8_t tuh_hid_interface_protocol(uint8_t dev_addr, uint8_t instance)
 {
-  return hidh_interface_get_report(dev_addr, p_report, &keyboardh_data[dev_addr-1]);
+  hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
+  return hid_itf->itf_protocol;
 }
 
-bool tuh_hid_keyboard_is_busy(uint8_t dev_addr)
+bool tuh_hid_get_protocol(uint8_t dev_addr, uint8_t instance)
 {
-  return  tuh_hid_keyboard_is_mounted(dev_addr) && hcd_edpt_busy(dev_addr, keyboardh_data[dev_addr-1].ep_in);
+  hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
+  return hid_itf->protocol_mode;
 }
 
-#endif
+static bool set_protocol_complete(uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result)
+{
+  uint8_t const itf_num     = (uint8_t) request->wIndex;
+  uint8_t const instance    = get_instance_id_by_itfnum(dev_addr, itf_num);
+  hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
 
-//--------------------------------------------------------------------+
-// MOUSE
-//--------------------------------------------------------------------+
-#if CFG_TUH_HID_MOUSE
+  if (XFER_RESULT_SUCCESS == result) hid_itf->protocol_mode = (uint8_t) request->wValue;
 
-static hidh_interface_t mouseh_data[CFG_TUSB_HOST_DEVICE_MAX]; // does not have addr0, index = dev_address-1
+  if (tuh_hid_set_protocol_complete_cb)
+  {
+    tuh_hid_set_protocol_complete_cb(dev_addr, instance, hid_itf->protocol_mode);
+  }
 
-//------------- Public API -------------//
-bool tuh_hid_mouse_is_mounted(uint8_t dev_addr)
-{
-  return tuh_device_is_configured(dev_addr) && (mouseh_data[dev_addr-1].ep_in != 0);
+  return true;
 }
 
-bool tuh_hid_mouse_is_busy(uint8_t dev_addr)
+bool tuh_hid_set_protocol(uint8_t dev_addr, uint8_t instance, uint8_t protocol)
 {
-  return  tuh_hid_mouse_is_mounted(dev_addr) && hcd_edpt_busy(dev_addr, mouseh_data[dev_addr-1].ep_in);
+  hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
+  TU_VERIFY(hid_itf->itf_protocol != HID_ITF_PROTOCOL_NONE);
+
+  TU_LOG2("HID Set Protocol = %d\r\n", protocol);
+
+  tusb_control_request_t const request =
+  {
+    .bmRequestType_bit =
+    {
+      .recipient = TUSB_REQ_RCPT_INTERFACE,
+      .type      = TUSB_REQ_TYPE_CLASS,
+      .direction = TUSB_DIR_OUT
+    },
+    .bRequest = HID_REQ_CONTROL_SET_PROTOCOL,
+    .wValue   = protocol,
+    .wIndex   = hid_itf->itf_num,
+    .wLength  = 0
+  };
+
+  TU_ASSERT( tuh_control_xfer(dev_addr, &request, NULL, set_protocol_complete) );
+  return true;
 }
 
-tusb_error_t tuh_hid_mouse_get_report(uint8_t dev_addr, void * report)
+static bool set_report_complete(uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result)
 {
-  return hidh_interface_get_report(dev_addr, report, &mouseh_data[dev_addr-1]);
+  TU_LOG2("HID Set Report complete\r\n");
+
+  if (tuh_hid_set_report_complete_cb)
+  {
+    uint8_t const itf_num     = (uint8_t) request->wIndex;
+    uint8_t const instance    = get_instance_id_by_itfnum(dev_addr, itf_num);
+
+    uint8_t const report_type = tu_u16_high(request->wValue);
+    uint8_t const report_id   = tu_u16_low(request->wValue);
+
+    tuh_hid_set_report_complete_cb(dev_addr, instance, report_id, report_type, (result == XFER_RESULT_SUCCESS) ? request->wLength : 0);
+  }
+
+  return true;
 }
 
-#endif
+bool tuh_hid_set_report(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t report_type, void* report, uint16_t len)
+{
+  hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
+  TU_LOG2("HID Set Report: id = %u, type = %u, len = %u\r\n", report_id, report_type, len);
 
-//--------------------------------------------------------------------+
-// GENERIC
-//--------------------------------------------------------------------+
-#if CFG_TUSB_HOST_HID_GENERIC
+  tusb_control_request_t const request =
+  {
+    .bmRequestType_bit =
+    {
+      .recipient = TUSB_REQ_RCPT_INTERFACE,
+      .type      = TUSB_REQ_TYPE_CLASS,
+      .direction = TUSB_DIR_OUT
+    },
+    .bRequest = HID_REQ_CONTROL_SET_REPORT,
+    .wValue   = tu_u16(report_type, report_id),
+    .wIndex   = hid_itf->itf_num,
+    .wLength  = len
+  };
 
-//STATIC_ struct {
-//  hidh_interface_info_t
-//} generic_data[CFG_TUSB_HOST_DEVICE_MAX];
+  TU_ASSERT( tuh_control_xfer(dev_addr, &request, report, set_report_complete) );
+  return true;
+}
 
-#endif
+//bool tuh_n_hid_n_ready(uint8_t dev_addr, uint8_t instance)
+//{
+//  TU_VERIFY(tuh_n_hid_n_mounted(dev_addr, instance));
+//
+//  hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
+//  return !hcd_edpt_busy(dev_addr, hid_itf->ep_in);
+//}
+
+//void tuh_hid_send_report(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t const* report, uint16_t len);
 
 //--------------------------------------------------------------------+
-// CLASS-USBH API (don't require to verify parameters)
+// USBH API
 //--------------------------------------------------------------------+
 void hidh_init(void)
 {
-#if CFG_TUH_HID_KEYBOARD
-  tu_memclr(&keyboardh_data, sizeof(hidh_interface_t)*CFG_TUSB_HOST_DEVICE_MAX);
-#endif
+  tu_memclr(_hidh_dev, sizeof(_hidh_dev));
+}
 
-#if CFG_TUH_HID_MOUSE
-  tu_memclr(&mouseh_data, sizeof(hidh_interface_t)*CFG_TUSB_HOST_DEVICE_MAX);
-#endif
+bool hidh_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
+{
+  (void) result;
 
-#if CFG_TUSB_HOST_HID_GENERIC
-  hidh_generic_init();
-#endif
+  uint8_t const dir = tu_edpt_dir(ep_addr);
+  uint8_t const instance = get_instance_id_by_epaddr(dev_addr, ep_addr);
+  hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
+
+  if ( dir == TUSB_DIR_IN )
+  {
+    TU_LOG2("  Get Report callback (%u, %u)\r\n", dev_addr, instance);
+    TU_LOG1_MEM(hid_itf->epin_buf, 8, 2);
+    tuh_hid_report_received_cb(dev_addr, instance, hid_itf->epin_buf, xferred_bytes);
+
+    // queue next report
+    hidh_get_report(dev_addr, hid_itf);
+  }else
+  {
+    if (tuh_hid_report_sent_cb) tuh_hid_report_sent_cb(dev_addr, instance, hid_itf->epout_buf, xferred_bytes);
+  }
+
+  return true;
 }
 
-#if 0
-CFG_TUSB_MEM_SECTION uint8_t report_descriptor[256];
-#endif
+void hidh_close(uint8_t dev_addr)
+{
+  hidh_device_t* hid_dev = get_dev(dev_addr);
+  if (tuh_hid_umount_cb)
+  {
+    for ( uint8_t inst = 0; inst < hid_dev->inst_count; inst++ ) tuh_hid_umount_cb(dev_addr, inst);
+  }
 
-bool hidh_open_subtask(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *p_interface_desc, uint16_t *p_length)
+  tu_memclr(hid_dev, sizeof(hidh_device_t));
+}
+
+//--------------------------------------------------------------------+
+// Enumeration
+//--------------------------------------------------------------------+
+
+static bool config_get_protocol             (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result);
+static bool config_get_report_desc          (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result);
+static bool config_get_report_desc_complete (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result);
+
+bool hidh_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *desc_itf, uint16_t *p_length)
 {
-  uint8_t const *p_desc = (uint8_t const *) p_interface_desc;
+  TU_VERIFY(TUSB_CLASS_HID == desc_itf->bInterfaceClass);
+
+  uint8_t const *p_desc = (uint8_t const *) desc_itf;
 
   //------------- HID descriptor -------------//
-  p_desc += p_desc[DESC_OFFSET_LEN];
-  tusb_hid_descriptor_hid_t const *p_desc_hid = (tusb_hid_descriptor_hid_t const *) p_desc;
-  TU_ASSERT(HID_DESC_TYPE_HID == p_desc_hid->bDescriptorType, TUSB_ERROR_INVALID_PARA);
+  p_desc = tu_desc_next(p_desc);
+  tusb_hid_descriptor_hid_t const *desc_hid = (tusb_hid_descriptor_hid_t const *) p_desc;
+  TU_ASSERT(HID_DESC_TYPE_HID == desc_hid->bDescriptorType);
+
+  // not enough interface, try to increase CFG_TUH_HID
+  // TODO multiple devices
+  hidh_device_t* hid_dev = get_dev(dev_addr);
+  TU_ASSERT(hid_dev->inst_count < CFG_TUH_HID);
 
   //------------- Endpoint Descriptor -------------//
-  p_desc += p_desc[DESC_OFFSET_LEN];
-  tusb_desc_endpoint_t const * p_endpoint_desc = (tusb_desc_endpoint_t const *) p_desc;
-  TU_ASSERT(TUSB_DESC_ENDPOINT == p_endpoint_desc->bDescriptorType, TUSB_ERROR_INVALID_PARA);
+  p_desc = tu_desc_next(p_desc);
+  tusb_desc_endpoint_t const * desc_ep = (tusb_desc_endpoint_t const *) p_desc;
+  TU_ASSERT(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType);
 
-  if ( HID_SUBCLASS_BOOT == p_interface_desc->bInterfaceSubClass )
-  {
-    #if CFG_TUH_HID_KEYBOARD
-    if ( HID_PROTOCOL_KEYBOARD == p_interface_desc->bInterfaceProtocol)
-    {
-      TU_ASSERT( hidh_interface_open(rhport, dev_addr, p_interface_desc->bInterfaceNumber, p_endpoint_desc, &keyboardh_data[dev_addr-1]) );
-      TU_LOG2_HEX(keyboardh_data[dev_addr-1].ep_in);
-    } else
-    #endif
+  // TODO also open endpoint OUT
+  TU_ASSERT( usbh_edpt_open(rhport, dev_addr, desc_ep) );
 
-    #if CFG_TUH_HID_MOUSE
-    if ( HID_PROTOCOL_MOUSE == p_interface_desc->bInterfaceProtocol)
-    {
-      TU_ASSERT ( hidh_interface_open(rhport, dev_addr, p_interface_desc->bInterfaceNumber, p_endpoint_desc, &mouseh_data[dev_addr-1]) );
-      TU_LOG2_HEX(mouseh_data[dev_addr-1].ep_in);
-    } else
-    #endif
+  hidh_interface_t* hid_itf = get_instance(dev_addr, hid_dev->inst_count);
+  hid_dev->inst_count++;
 
-    {
-      // Not supported protocol
-      return false;
-    }
-  }else
-  {
-    // Not supported subclass
-    return false;
-  }
+  hid_itf->itf_num   = desc_itf->bInterfaceNumber;
+  hid_itf->ep_in     = desc_ep->bEndpointAddress;
+  hid_itf->epin_size = desc_ep->wMaxPacketSize.size;
+
+  // Assume bNumDescriptors = 1
+  hid_itf->report_desc_type = desc_hid->bReportType;
+  hid_itf->report_desc_len  = tu_unaligned_read16(&desc_hid->wReportLength);
 
-  *p_length = sizeof(tusb_desc_interface_t) + sizeof(tusb_hid_descriptor_hid_t) + sizeof(tusb_desc_endpoint_t);
+  hid_itf->protocol_mode = HID_PROTOCOL_REPORT; // Per Specs: default is report mode
+  if ( HID_SUBCLASS_BOOT == desc_itf->bInterfaceSubClass ) hid_itf->itf_protocol = desc_itf->bInterfaceProtocol;
+
+  *p_length = sizeof(tusb_desc_interface_t) + sizeof(tusb_hid_descriptor_hid_t) + desc_itf->bNumEndpoints*sizeof(tusb_desc_endpoint_t);
 
   return true;
 }
 
 bool hidh_set_config(uint8_t dev_addr, uint8_t itf_num)
 {
-#if 0
-  //------------- Get Report Descriptor TODO HID parser -------------//
-  if ( p_desc_hid->bNumDescriptors )
-  {
-    STASK_INVOKE(
-        usbh_control_xfer_subtask( dev_addr, bm_request_type(TUSB_DIR_IN, TUSB_REQ_TYPE_STANDARD, TUSB_REQ_RCPT_INTERFACE),
-                                   TUSB_REQ_GET_DESCRIPTOR, (p_desc_hid->bReportType << 8), 0,
-                                   p_desc_hid->wReportLength, report_descriptor ),
-        error
-    );
-    (void) error; // if error in getting report descriptor --> treating like there is none
-  }
-#endif
+  uint8_t const instance    = get_instance_id_by_itfnum(dev_addr, itf_num);
+  hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
+
+  // Idle rate = 0 mean only report when there is changes
+  uint16_t const idle_rate = 0;
 
-#if 0
-  // SET IDLE = 0 request
-  // Device can stall if not support this request
+  // SET IDLE request, device can stall if not support this request
+  TU_LOG2("HID Set Idle \r\n");
   tusb_control_request_t const request =
   {
     .bmRequestType_bit =
@@ -236,84 +317,281 @@ bool hidh_set_config(uint8_t dev_addr, uint8_t itf_num)
       .direction = TUSB_DIR_OUT
     },
     .bRequest = HID_REQ_CONTROL_SET_IDLE,
-    .wValue   = 0, // idle_rate = 0
-    .wIndex   = p_interface_desc->bInterfaceNumber,
+    .wValue   = idle_rate,
+    .wIndex   = itf_num,
     .wLength  = 0
   };
 
-  // stall is a valid response for SET_IDLE, therefore we could ignore result of this request
-  tuh_control_xfer(dev_addr, &request, NULL, NULL);
-#endif
+  TU_ASSERT( tuh_control_xfer(dev_addr, &request, NULL, (hid_itf->itf_protocol != HID_ITF_PROTOCOL_NONE) ? config_get_protocol : config_get_report_desc) );
 
-  usbh_driver_set_config_complete(dev_addr, itf_num);
+  return true;
+}
+
+static bool config_get_protocol(uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result)
+{
+  // Stall is a valid response for SET_IDLE GET_PROTOCOL, therefore we could ignore its result
+  (void) result;
 
-#if CFG_TUH_HID_KEYBOARD
-  if (( keyboardh_data[dev_addr-1].itf_num == itf_num) && keyboardh_data[dev_addr-1].valid)
+  uint8_t const itf_num     = (uint8_t) request->wIndex;
+  uint8_t const instance    = get_instance_id_by_itfnum(dev_addr, itf_num);
+  hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
+
+  TU_LOG2("HID Get Protocol\r\n");
+  tusb_control_request_t const new_request =
   {
-    tuh_hid_keyboard_mounted_cb(dev_addr);
-  }
-#endif
+    .bmRequestType_bit =
+    {
+      .recipient = TUSB_REQ_RCPT_INTERFACE,
+      .type      = TUSB_REQ_TYPE_CLASS,
+      .direction = TUSB_DIR_IN
+    },
+    .bRequest = HID_REQ_CONTROL_GET_PROTOCOL,
+    .wValue   = 0,
+    .wIndex   = hid_itf->itf_num,
+    .wLength  = 1
+  };
+
+  TU_ASSERT( tuh_control_xfer(dev_addr, &new_request, &hid_itf->protocol_mode, config_get_report_desc) );
+  return false;
+}
 
-#if CFG_TUH_HID_MOUSE
-  if (( mouseh_data[dev_addr-1].ep_in == itf_num ) &&  mouseh_data[dev_addr-1].valid)
+static bool config_get_report_desc(uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result)
+{
+  // Stall is a valid response for SET_IDLE GET_PROTOCOL, therefore we could ignore its result
+  (void) result;
+
+  uint8_t const itf_num     = (uint8_t) request->wIndex;
+  uint8_t const instance    = get_instance_id_by_itfnum(dev_addr, itf_num);
+  hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
+
+  // Get Report Descriptor
+  // using usbh enumeration buffer since report descriptor can be very long
+  TU_ASSERT( hid_itf->report_desc_len <= CFG_TUH_ENUMERATION_BUFSZIE );
+
+  TU_LOG2("HID Get Report Descriptor\r\n");
+  tusb_control_request_t const new_request =
   {
-    tuh_hid_mouse_mounted_cb(dev_addr);
-  }
-#endif
+    .bmRequestType_bit =
+    {
+      .recipient = TUSB_REQ_RCPT_INTERFACE,
+      .type      = TUSB_REQ_TYPE_STANDARD,
+      .direction = TUSB_DIR_IN
+    },
+    .bRequest = TUSB_REQ_GET_DESCRIPTOR,
+    .wValue   = tu_u16(hid_itf->report_desc_type, 0),
+    .wIndex   = itf_num,
+    .wLength  = hid_itf->report_desc_len
+  };
+
+  TU_ASSERT(tuh_control_xfer(dev_addr, &new_request, usbh_get_enum_buf(), config_get_report_desc_complete));
+  return true;
+}
+
+static bool config_get_report_desc_complete(uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result)
+{
+  TU_ASSERT(XFER_RESULT_SUCCESS == result);
+
+  uint8_t const itf_num     = (uint8_t) request->wIndex;
+  uint8_t const instance    = get_instance_id_by_itfnum(dev_addr, itf_num);
+  hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
+
+  uint8_t const* desc_report = usbh_get_enum_buf();
+  uint16_t const desc_len    = request->wLength;
+
+  // enumeration is complete
+  tuh_hid_mount_cb(dev_addr, instance, desc_report, desc_len);
+
+  // queue transfer for IN endpoint
+  hidh_get_report(dev_addr, hid_itf);
+
+  // notify usbh that driver enumeration is complete
+  usbh_driver_set_config_complete(dev_addr, itf_num);
 
   return true;
 }
 
-bool hidh_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes)
+//--------------------------------------------------------------------+
+// Report Descriptor Parser
+//--------------------------------------------------------------------+
+
+uint8_t tuh_hid_parse_report_descriptor(tuh_hid_report_info_t* report_info_arr, uint8_t arr_count, uint8_t const* desc_report, uint16_t desc_len)
 {
-  (void) xferred_bytes; // TODO may need to use this para later
+  // Report Item 6.2.2.2 USB HID 1.11
+  union TU_ATTR_PACKED
+  {
+    uint8_t byte;
+    struct TU_ATTR_PACKED
+    {
+        uint8_t size : 2;
+        uint8_t type : 2;
+        uint8_t tag  : 4;
+    };
+  } header;
 
-#if CFG_TUH_HID_KEYBOARD
-  if ( ep_addr == keyboardh_data[dev_addr-1].ep_in )
+  tu_memclr(report_info_arr, arr_count*sizeof(tuh_hid_report_info_t));
+
+  uint8_t report_num = 0;
+  tuh_hid_report_info_t* info = report_info_arr;
+
+  // current parsed report count & size from descriptor
+//  uint8_t ri_report_count = 0;
+//  uint8_t ri_report_size = 0;
+
+  uint8_t ri_collection_depth = 0;
+
+  while(desc_len && report_num < arr_count)
   {
-    tuh_hid_keyboard_isr(dev_addr, event);
-    return true;
+    header.byte = *desc_report++;
+    desc_len--;
+
+    uint8_t const tag  = header.tag;
+    uint8_t const type = header.type;
+    uint8_t const size = header.size;
+
+    uint8_t const data8 = desc_report[0];
+
+    TU_LOG2("tag = %d, type = %d, size = %d, data = ", tag, type, size);
+    for(uint32_t i=0; i<size; i++) TU_LOG2("%02X ", desc_report[i]);
+    TU_LOG2("\r\n");
+
+    switch(type)
+    {
+      case RI_TYPE_MAIN:
+        switch (tag)
+        {
+          case RI_MAIN_INPUT: break;
+          case RI_MAIN_OUTPUT: break;
+          case RI_MAIN_FEATURE: break;
+
+          case RI_MAIN_COLLECTION:
+            ri_collection_depth++;
+          break;
+
+          case RI_MAIN_COLLECTION_END:
+            ri_collection_depth--;
+            if (ri_collection_depth == 0)
+            {
+              info++;
+              report_num++;
+            }
+          break;
+
+          default: break;
+        }
+      break;
+
+      case RI_TYPE_GLOBAL:
+        switch(tag)
+        {
+          case RI_GLOBAL_USAGE_PAGE:
+            // only take in account the "usage page" before REPORT ID
+            if ( ri_collection_depth == 0 ) memcpy(&info->usage_page, desc_report, size);
+          break;
+
+          case RI_GLOBAL_LOGICAL_MIN   : break;
+          case RI_GLOBAL_LOGICAL_MAX   : break;
+          case RI_GLOBAL_PHYSICAL_MIN  : break;
+          case RI_GLOBAL_PHYSICAL_MAX  : break;
+
+          case RI_GLOBAL_REPORT_ID:
+            info->report_id = data8;
+          break;
+
+          case RI_GLOBAL_REPORT_SIZE:
+//            ri_report_size = data8;
+          break;
+
+          case RI_GLOBAL_REPORT_COUNT:
+//            ri_report_count = data8;
+          break;
+
+          case RI_GLOBAL_UNIT_EXPONENT : break;
+          case RI_GLOBAL_UNIT          : break;
+          case RI_GLOBAL_PUSH          : break;
+          case RI_GLOBAL_POP           : break;
+
+          default: break;
+        }
+      break;
+
+      case RI_TYPE_LOCAL:
+        switch(tag)
+        {
+          case RI_LOCAL_USAGE:
+            // only take in account the "usage" before starting REPORT ID
+            if ( ri_collection_depth == 0 ) info->usage = data8;
+          break;
+
+          case RI_LOCAL_USAGE_MIN        : break;
+          case RI_LOCAL_USAGE_MAX        : break;
+          case RI_LOCAL_DESIGNATOR_INDEX : break;
+          case RI_LOCAL_DESIGNATOR_MIN   : break;
+          case RI_LOCAL_DESIGNATOR_MAX   : break;
+          case RI_LOCAL_STRING_INDEX     : break;
+          case RI_LOCAL_STRING_MIN       : break;
+          case RI_LOCAL_STRING_MAX       : break;
+          case RI_LOCAL_DELIMITER        : break;
+          default: break;
+        }
+      break;
+
+      // error
+      default: break;
+    }
+
+    desc_report += size;
+    desc_len    -= size;
   }
-#endif
 
-#if CFG_TUH_HID_MOUSE
-  if ( ep_addr == mouseh_data[dev_addr-1].ep_in )
+  for ( uint8_t i = 0; i < report_num; i++ )
   {
-    tuh_hid_mouse_isr(dev_addr, event);
-    return true;
+    info = report_info_arr+i;
+    TU_LOG2("%u: id = %u, usage_page = %u, usage = %u\r\n", i, info->report_id, info->usage_page, info->usage);
   }
-#endif
 
-#if CFG_TUSB_HOST_HID_GENERIC
+  return report_num;
+}
 
-#endif
+//--------------------------------------------------------------------+
+// Helper
+//--------------------------------------------------------------------+
 
-  return true;
+// Get Device by address
+TU_ATTR_ALWAYS_INLINE static inline hidh_device_t* get_dev(uint8_t dev_addr)
+{
+  return &_hidh_dev[dev_addr-1];
 }
 
-void hidh_close(uint8_t dev_addr)
+// Get Interface by instance number
+TU_ATTR_ALWAYS_INLINE static inline hidh_interface_t* get_instance(uint8_t dev_addr, uint8_t instance)
 {
-#if CFG_TUH_HID_KEYBOARD
-  if ( keyboardh_data[dev_addr-1].ep_in != 0 )
-  {
-    hidh_interface_close(&keyboardh_data[dev_addr-1]);
-    tuh_hid_keyboard_unmounted_cb(dev_addr);
-  }
-#endif
+  return &_hidh_dev[dev_addr-1].instances[instance];
+}
 
-#if CFG_TUH_HID_MOUSE
-  if( mouseh_data[dev_addr-1].ep_in != 0 )
+// Get instance ID by interface number
+static uint8_t get_instance_id_by_itfnum(uint8_t dev_addr, uint8_t itf)
+{
+  for ( uint8_t inst = 0; inst < CFG_TUH_HID; inst++ )
   {
-    hidh_interface_close(&mouseh_data[dev_addr-1]);
-    tuh_hid_mouse_unmounted_cb( dev_addr );
+    hidh_interface_t *hid = get_instance(dev_addr, inst);
+
+    if ( (hid->itf_num == itf) && (hid->ep_in || hid->ep_out) ) return inst;
   }
-#endif
 
-#if CFG_TUSB_HOST_HID_GENERIC
-  hidh_generic_close(dev_addr);
-#endif
+  return 0xff;
 }
 
+// Get instance ID by endpoint address
+static uint8_t get_instance_id_by_epaddr(uint8_t dev_addr, uint8_t ep_addr)
+{
+  for ( uint8_t inst = 0; inst < CFG_TUH_HID; inst++ )
+  {
+    hidh_interface_t *hid = get_instance(dev_addr, inst);
 
+    if ( (ep_addr == hid->ep_in) || ( ep_addr == hid->ep_out) ) return inst;
+  }
+
+  return 0xff;
+}
 
 #endif

+ 62 - 134
src/class/hid/hid_host.h

@@ -39,166 +39,94 @@
 #endif
 
 //--------------------------------------------------------------------+
-// KEYBOARD Application API
+// Class Driver Configuration
 //--------------------------------------------------------------------+
-/** \addtogroup ClassDriver_HID_Keyboard Keyboard
- *  @{ */
 
-/** \defgroup Keyboard_Host Host
- *  The interface API includes status checking function, data transferring function and callback functions
- *  @{ */
+// TODO Highspeed interrupt can be up to 512 bytes
+#ifndef CFG_TUH_HID_EP_BUFSIZE
+#define CFG_TUH_HID_EP_BUFSIZE 64
+#endif
 
-extern uint8_t const hid_keycode_to_ascii_tbl[2][128]; // TODO used weak attr if build failed without KEYBOARD enabled
+typedef struct
+{
+  uint8_t  report_id;
+  uint8_t  usage;
+  uint16_t usage_page;
 
-/** \brief      Check if device supports Keyboard interface or not
- * \param[in]   dev_addr    device address
- * \retval      true if device supports Keyboard interface
- * \retval      false if device does not support Keyboard interface or is not mounted
- */
-bool tuh_hid_keyboard_is_mounted(uint8_t dev_addr);
-
-/** \brief      Check if the interface is currently busy or not
- * \param[in]   dev_addr device address
- * \retval      true if the interface is busy meaning the stack is still transferring/waiting data from/to device
- * \retval      false if the interface is not busy meaning the stack successfully transferred data from/to device
- * \note        This function is primarily used for polling/waiting result after \ref tuh_hid_keyboard_get_report.
- *              Alternatively, asynchronous event API can be used
- */
-bool tuh_hid_keyboard_is_busy(uint8_t dev_addr);
-
-/** \brief        Perform a get report from Keyboard interface
- * \param[in]		  dev_addr device address
- * \param[in,out] p_report address that is used to store data from device. Must be accessible by usb controller (see \ref CFG_TUSB_MEM_SECTION)
- * \returns       \ref tusb_error_t type to indicate success or error condition.
- * \retval        TUSB_ERROR_NONE on success
- * \retval        TUSB_ERROR_INTERFACE_IS_BUSY if the interface is already transferring data with device
- * \retval        TUSB_ERROR_DEVICE_NOT_READY if device is not yet configured (by SET CONFIGURED request)
- * \retval        TUSB_ERROR_INVALID_PARA if input parameters are not correct
- * \note          This function is non-blocking and returns immediately. The result of usb transfer will be reported by the interface's callback function
- */
-tusb_error_t  tuh_hid_keyboard_get_report(uint8_t dev_addr, void * p_report);
-
-//------------- Application Callback -------------//
-/** \brief      Callback function that is invoked when an transferring event occurred
- * \param[in]		dev_addr	Address of device
- * \param[in]   event an value from \ref xfer_result_t
- * \note        event can be one of following
- *              - XFER_RESULT_SUCCESS : previously scheduled transfer completes successfully.
- *              - XFER_RESULT_FAILED   : previously scheduled transfer encountered a transaction error.
- *              - XFER_RESULT_STALLED : previously scheduled transfer is stalled by device.
- * \note        Application should schedule the next report by calling \ref tuh_hid_keyboard_get_report within this callback
- */
-void tuh_hid_keyboard_isr(uint8_t dev_addr, xfer_result_t event);
+  // TODO still use the endpoint size for now
+//  uint8_t in_len;      // length of IN report
+//  uint8_t out_len;     // length of OUT report
+} tuh_hid_report_info_t;
 
-/** \brief 			Callback function that will be invoked when a device with Keyboard interface is mounted
- * \param[in] 	dev_addr Address of newly mounted device
- * \note        This callback should be used by Application to set-up interface-related data
- */
-void tuh_hid_keyboard_mounted_cb(uint8_t dev_addr);
+//--------------------------------------------------------------------+
+// Application API
+//--------------------------------------------------------------------+
 
-/** \brief 			Callback function that will be invoked when a device with Keyboard interface is unmounted
- * \param[in] 	dev_addr Address of newly unmounted device
- * \note        This callback should be used by Application to tear-down interface-related data
- */
-void tuh_hid_keyboard_unmounted_cb(uint8_t dev_addr);
+// Get the number of HID instances
+uint8_t tuh_hid_instance_count(uint8_t dev_addr);
 
-/** @} */ // Keyboard_Host
-/** @} */ // ClassDriver_HID_Keyboard
+// Check if HID instance is mounted
+bool tuh_hid_mounted(uint8_t dev_addr, uint8_t instance);
 
-//--------------------------------------------------------------------+
-// MOUSE Application API
-//--------------------------------------------------------------------+
-/** \addtogroup ClassDriver_HID_Mouse Mouse
- *  @{ */
+// Get interface supported protocol (bInterfaceProtocol) check out hid_interface_protocol_enum_t for possible values
+uint8_t tuh_hid_interface_protocol(uint8_t dev_addr, uint8_t instance);
 
-/** \defgroup Mouse_Host Host
- *  The interface API includes status checking function, data transferring function and callback functions
- *  @{ */
+// Get current active protocol: HID_PROTOCOL_BOOT (0) or HID_PROTOCOL_REPORT (1)
+// Note: as HID spec, device will be initialized in Report mode
+bool tuh_hid_get_protocol(uint8_t dev_addr, uint8_t instance);
 
-/** \brief      Check if device supports Mouse interface or not
- * \param[in]   dev_addr    device address
- * \retval      true if device supports Mouse interface
- * \retval      false if device does not support Mouse interface or is not mounted
- */
-bool          tuh_hid_mouse_is_mounted(uint8_t dev_addr);
-
-/** \brief      Check if the interface is currently busy or not
- * \param[in]   dev_addr device address
- * \retval      true if the interface is busy meaning the stack is still transferring/waiting data from/to device
- * \retval      false if the interface is not busy meaning the stack successfully transferred data from/to device
- * \note        This function is primarily used for polling/waiting result after \ref tuh_hid_mouse_get_report.
- *              Alternatively, asynchronous event API can be used
- */
-bool          tuh_hid_mouse_is_busy(uint8_t dev_addr);
-
-/** \brief        Perform a get report from Mouse interface
- * \param[in]		  dev_addr device address
- * \param[in,out] p_report address that is used to store data from device. Must be accessible by usb controller (see \ref CFG_TUSB_MEM_SECTION)
- * \returns       \ref tusb_error_t type to indicate success or error condition.
- * \retval        TUSB_ERROR_NONE on success
- * \retval        TUSB_ERROR_INTERFACE_IS_BUSY if the interface is already transferring data with device
- * \retval        TUSB_ERROR_DEVICE_NOT_READY if device is not yet configured (by SET CONFIGURED request)
- * \retval        TUSB_ERROR_INVALID_PARA if input parameters are not correct
- * \note          This function is non-blocking and returns immediately. The result of usb transfer will be reported by the interface's callback function
- */
-tusb_error_t  tuh_hid_mouse_get_report(uint8_t dev_addr, void* p_report);
-
-//------------- Application Callback -------------//
-/** \brief      Callback function that is invoked when an transferring event occurred
- * \param[in]		dev_addr	Address of device
- * \param[in]   event an value from \ref xfer_result_t
- * \note        event can be one of following
- *              - XFER_RESULT_SUCCESS : previously scheduled transfer completes successfully.
- *              - XFER_RESULT_FAILED   : previously scheduled transfer encountered a transaction error.
- *              - XFER_RESULT_STALLED : previously scheduled transfer is stalled by device.
- * \note        Application should schedule the next report by calling \ref tuh_hid_mouse_get_report within this callback
- */
-void tuh_hid_mouse_isr(uint8_t dev_addr, xfer_result_t event);
+// Set protocol to HID_PROTOCOL_BOOT (0) or HID_PROTOCOL_REPORT (1)
+// This function is only supported by Boot interface (tuh_n_hid_interface_protocol() != NONE)
+bool tuh_hid_set_protocol(uint8_t dev_addr, uint8_t instance, uint8_t protocol);
 
-/** \brief 			Callback function that will be invoked when a device with Mouse interface is mounted
- * \param[in]	  dev_addr Address of newly mounted device
- * \note        This callback should be used by Application to set-up interface-related data
- */
-void tuh_hid_mouse_mounted_cb(uint8_t dev_addr);
+// Set Report using control endpoint
+// report_type is either Intput, Output or Feature, (value from hid_report_type_t)
+bool tuh_hid_set_report(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t report_type, void* report, uint16_t len);
 
-/** \brief 			Callback function that will be invoked when a device with Mouse interface is unmounted
- * \param[in] 	dev_addr Address of newly unmounted device
- * \note        This callback should be used by Application to tear-down interface-related data
- */
-void tuh_hid_mouse_unmounted_cb(uint8_t dev_addr);
+// Parse report descriptor into array of report_info struct and return number of reports.
+// For complicated report, application should write its own parser.
+uint8_t tuh_hid_parse_report_descriptor(tuh_hid_report_info_t* reports_info_arr, uint8_t arr_count, uint8_t const* desc_report, uint16_t desc_len) TU_ATTR_UNUSED;
+
+// Check if the interface is ready to use
+//bool tuh_n_hid_n_ready(uint8_t dev_addr, uint8_t instance);
 
-/** @} */ // Mouse_Host
-/** @} */ // ClassDriver_HID_Mouse
+// Send report using interrupt endpoint
+// If report_id > 0 (composite), it will be sent as 1st byte, then report contents. Otherwise only report content is sent.
+//void tuh_hid_send_report(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t const* report, uint16_t len);
 
 //--------------------------------------------------------------------+
-// GENERIC Application API
+// Callbacks (Weak is optional)
 //--------------------------------------------------------------------+
-/** \addtogroup ClassDriver_HID_Generic Generic (not supported yet)
- *  @{ */
 
-/** \defgroup Generic_Host Host
- *  The interface API includes status checking function, data transferring function and callback functions
- *  @{ */
+// Invoked when device with hid interface is mounted
+// Report descriptor is also available for use. tuh_hid_parse_report_descriptor()
+// can be used to parse common/simple enough descriptor.
+void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report_desc, uint16_t desc_len);
+
+// Invoked when device with hid interface is un-mounted
+TU_ATTR_WEAK void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance);
+
+// Invoked when received report from device via interrupt endpoint
+// Note: if there is report ID (composite), it is 1st byte of report
+void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len);
 
-bool          tuh_hid_generic_is_mounted(uint8_t dev_addr);
-tusb_error_t  tuh_hid_generic_get_report(uint8_t dev_addr, void* p_report, bool int_on_complete);
-tusb_error_t  tuh_hid_generic_set_report(uint8_t dev_addr, void* p_report, bool int_on_complete);
-tusb_interface_status_t tuh_hid_generic_get_status(uint8_t dev_addr);
-tusb_interface_status_t tuh_hid_generic_set_status(uint8_t dev_addr);
+// Invoked when sent report to device successfully via interrupt endpoint
+TU_ATTR_WEAK void tuh_hid_report_sent_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len);
 
-//------------- Application Callback -------------//
-void tuh_hid_generic_isr(uint8_t dev_addr, xfer_result_t event);
+// Invoked when Sent Report to device via either control endpoint
+// len = 0 indicate there is error in the transfer e.g stalled response
+TU_ATTR_WEAK void tuh_hid_set_report_complete_cb(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t report_type, uint16_t len);
 
-/** @} */ // Generic_Host
-/** @} */ // ClassDriver_HID_Generic
+// Invoked when Set Protocol request is complete
+TU_ATTR_WEAK void tuh_hid_set_protocol_complete_cb(uint8_t dev_addr, uint8_t instance, uint8_t protocol);
 
 //--------------------------------------------------------------------+
 // Internal Class Driver API
 //--------------------------------------------------------------------+
 void hidh_init(void);
-bool hidh_open_subtask(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *p_interface_desc, uint16_t *p_length);
+bool hidh_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *desc_itf, uint16_t *p_length);
 bool hidh_set_config(uint8_t dev_addr, uint8_t itf_num);
-bool hidh_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
+bool hidh_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes);
 void hidh_close(uint8_t dev_addr);
 
 #ifdef __cplusplus

+ 12 - 10
src/class/msc/msc_host.c

@@ -287,7 +287,7 @@ bool tuh_msc_reset(uint8_t dev_addr)
 #endif
 
 //--------------------------------------------------------------------+
-// CLASS-USBH API (don't require to verify parameters)
+// CLASS-USBH API
 //--------------------------------------------------------------------+
 void msch_init(void)
 {
@@ -297,8 +297,11 @@ void msch_init(void)
 void msch_close(uint8_t dev_addr)
 {
   msch_interface_t* p_msc = get_itf(dev_addr);
+
+  // invoke Application Callback
+  if (p_msc->mounted && tuh_msc_umount_cb) tuh_msc_umount_cb(dev_addr);
+
   tu_memclr(p_msc, sizeof(msch_interface_t));
-  tuh_msc_unmount_cb(dev_addr); // invoke Application Callback
 }
 
 bool msch_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes)
@@ -357,15 +360,13 @@ static bool config_test_unit_ready_complete(uint8_t dev_addr, msc_cbw_t const* c
 static bool config_request_sense_complete(uint8_t dev_addr, msc_cbw_t const* cbw, msc_csw_t const* csw);
 static bool config_read_capacity_complete(uint8_t dev_addr, msc_cbw_t const* cbw, msc_csw_t const* csw);
 
-bool msch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t *p_length)
+bool msch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *desc_itf, uint16_t *p_length)
 {
-  TU_VERIFY (MSC_SUBCLASS_SCSI == itf_desc->bInterfaceSubClass &&
-             MSC_PROTOCOL_BOT  == itf_desc->bInterfaceProtocol);
+  TU_VERIFY (MSC_SUBCLASS_SCSI == desc_itf->bInterfaceSubClass &&
+             MSC_PROTOCOL_BOT  == desc_itf->bInterfaceProtocol);
 
   msch_interface_t* p_msc = get_itf(dev_addr);
-
-  //------------- Open Data Pipe -------------//
-  tusb_desc_endpoint_t const * ep_desc = (tusb_desc_endpoint_t const *) tu_desc_next(itf_desc);
+  tusb_desc_endpoint_t const * ep_desc = (tusb_desc_endpoint_t const *) tu_desc_next(desc_itf);
 
   for(uint32_t i=0; i<2; i++)
   {
@@ -383,7 +384,7 @@ bool msch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *it
     ep_desc = (tusb_desc_endpoint_t const *) tu_desc_next(ep_desc);
   }
 
-  p_msc->itf_num = itf_desc->bInterfaceNumber;
+  p_msc->itf_num = desc_itf->bInterfaceNumber;
   (*p_length) += sizeof(tusb_desc_interface_t) + 2*sizeof(tusb_desc_endpoint_t);
 
   return true;
@@ -473,8 +474,9 @@ static bool config_read_capacity_complete(uint8_t dev_addr, msc_cbw_t const* cbw
 
   // Mark enumeration is complete
   p_msc->mounted = true;
-  tuh_msc_mount_cb(dev_addr);
+  if (tuh_msc_mount_cb) tuh_msc_mount_cb(dev_addr);
 
+  // notify usbh that driver enumeration is complete
   usbh_driver_set_config_complete(dev_addr, p_msc->itf_num);
 
   return true;

+ 3 - 3
src/class/msc/msc_host.h

@@ -106,17 +106,17 @@ bool tuh_msc_read_capacity(uint8_t dev_addr, uint8_t lun, scsi_read_capacity10_r
 //------------- Application Callback -------------//
 
 // Invoked when a device with MassStorage interface is mounted
-void tuh_msc_mount_cb(uint8_t dev_addr);
+TU_ATTR_WEAK void tuh_msc_mount_cb(uint8_t dev_addr);
 
 // Invoked when a device with MassStorage interface is unmounted
-void tuh_msc_unmount_cb(uint8_t dev_addr);
+TU_ATTR_WEAK void tuh_msc_umount_cb(uint8_t dev_addr);
 
 //--------------------------------------------------------------------+
 // Internal Class Driver API
 //--------------------------------------------------------------------+
 
 void msch_init(void);
-bool msch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t *p_length);
+bool msch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *desc_itf, uint16_t *p_length);
 bool msch_set_config(uint8_t dev_addr, uint8_t itf_num);
 bool msch_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
 void msch_close(uint8_t dev_addr);

+ 2 - 0
src/common/tusb_types.h

@@ -309,6 +309,8 @@ typedef struct TU_ATTR_PACKED
   uint8_t  bMaxPower           ; ///< Maximum power consumption of the USB device from the bus in this specific configuration when the device is fully operational. Expressed in 2 mA units (i.e., 50 = 100 mA).
 } tusb_desc_configuration_t;
 
+TU_VERIFY_STATIC( sizeof(tusb_desc_configuration_t) == 9, "size is not correct");
+
 /// USB Interface Descriptor
 typedef struct TU_ATTR_PACKED
 {

+ 0 - 6
src/device/dcd.h

@@ -24,10 +24,6 @@
  * This file is part of the TinyUSB stack.
  */
 
-/** \ingroup group_usbd
- * \defgroup group_dcd Device Controller Driver (DCD)
- *  @{ */
-
 #ifndef _TUSB_DCD_H_
 #define _TUSB_DCD_H_
 
@@ -168,5 +164,3 @@ extern void dcd_event_xfer_complete (uint8_t rhport, uint8_t ep_addr, uint32_t x
 #endif
 
 #endif /* _TUSB_DCD_H_ */
-
-/// @}

+ 20 - 8
src/device/usbd.c

@@ -44,6 +44,10 @@
 //--------------------------------------------------------------------+
 // Device Data
 //--------------------------------------------------------------------+
+
+// Invalid driver ID in itf2drv[] ep2drv[][] mapping
+enum { DRVID_INVALID = 0xFFu };
+
 typedef struct
 {
   struct TU_ATTR_PACKED
@@ -76,9 +80,6 @@ typedef struct
 
 static usbd_device_t _usbd_dev;
 
-// Invalid driver ID in itf2drv[] ep2drv[][] mapping
-enum { DRVID_INVALID = 0xFFu };
-
 //--------------------------------------------------------------------+
 // Class Driver
 //--------------------------------------------------------------------+
@@ -253,6 +254,8 @@ static inline usbd_class_driver_t const * get_driver(uint8_t drvid)
 // DCD Event
 //--------------------------------------------------------------------+
 
+static bool _usbd_initialized = false;
+
 // Event queue
 // OPT_MODE_DEVICE is used by OS NONE for mutex (disable usb isr)
 OSAL_QUEUE_DEF(OPT_MODE_DEVICE, _usbd_qdef, CFG_TUD_TASK_QUEUE_SZ, dcd_event_t);
@@ -380,8 +383,16 @@ bool tud_connect(void)
 //--------------------------------------------------------------------+
 // USBD Task
 //--------------------------------------------------------------------+
-bool tud_init (void)
+bool tud_inited(void)
+{
+  return _usbd_initialized;
+}
+
+bool tud_init (uint8_t rhport)
 {
+  // skip if already initialized
+  if (_usbd_initialized) return _usbd_initialized;
+
   TU_LOG2("USBD init\r\n");
 
   tu_varclr(&_usbd_dev);
@@ -411,8 +422,10 @@ bool tud_init (void)
   }
 
   // Init device controller driver
-  dcd_init(TUD_OPT_RHPORT);
-  dcd_int_enable(TUD_OPT_RHPORT);
+  dcd_init(rhport);
+  dcd_int_enable(rhport);
+
+  _usbd_initialized = true;
 
   return true;
 }
@@ -1089,7 +1102,7 @@ void dcd_event_xfer_complete (uint8_t rhport, uint8_t ep_addr, uint32_t xferred_
 }
 
 //--------------------------------------------------------------------+
-// Helper
+// USBD API For Class Driver
 //--------------------------------------------------------------------+
 
 // Parse consecutive endpoint descriptors (IN & OUT)
@@ -1183,7 +1196,6 @@ bool usbd_edpt_claim(uint8_t rhport, uint8_t ep_addr)
 #if CFG_TUSB_OS != OPT_OS_NONE
   // pre-check to help reducing mutex lock
   TU_VERIFY((_usbd_dev.ep_status[epnum][dir].busy == 0) && (_usbd_dev.ep_status[epnum][dir].claimed == 0));
-
   osal_mutex_lock(_usbd_mutex, OSAL_TIMEOUT_WAIT_FOREVER);
 #endif
 

+ 5 - 1
src/device/usbd.h

@@ -41,7 +41,10 @@ extern "C" {
 //--------------------------------------------------------------------+
 
 // Init device stack
-bool tud_init (void);
+bool tud_init (uint8_t rhport);
+
+// Check if device stack is already initialized
+bool tud_inited(void);
 
 // Task function should be called in main/rtos loop
 void tud_task (void);
@@ -67,6 +70,7 @@ bool tud_mounted(void);
 bool tud_suspended(void);
 
 // Check if device is ready to transfer
+TU_ATTR_ALWAYS_INLINE
 static inline bool tud_ready(void)
 {
   return tud_mounted() && !tud_suspended();

+ 1 - 1
src/device/usbd_pvt.h

@@ -47,7 +47,7 @@ typedef struct
   void     (* reset            ) (uint8_t rhport);
   uint16_t (* open             ) (uint8_t rhport, tusb_desc_interface_t const * desc_intf, uint16_t max_len);
   bool     (* control_xfer_cb  ) (uint8_t rhport, uint8_t stage, tusb_control_request_t const * request);
-  bool     (* xfer_cb          ) (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
+  bool     (* xfer_cb          ) (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes);
   void     (* sof              ) (uint8_t rhport); /* optional */
 } usbd_class_driver_t;
 

+ 30 - 19
src/host/hcd.h

@@ -24,14 +24,10 @@
  * This file is part of the TinyUSB stack.
  */
 
-/** \ingroup group_usbh
- * \defgroup Group_HCD Host Controller Driver (HCD)
- *  @{ */
-
 #ifndef _TUSB_HCD_H_
 #define _TUSB_HCD_H_
 
-#include <common/tusb_common.h>
+#include "common/tusb_common.h"
 
 #ifdef __cplusplus
  extern "C" {
@@ -85,9 +81,8 @@ typedef struct
 #if TUSB_OPT_HOST_ENABLED
 // Max number of endpoints per device
 enum {
-  HCD_MAX_ENDPOINT = CFG_TUSB_HOST_DEVICE_MAX*(CFG_TUH_HUB + CFG_TUH_HID_KEYBOARD + CFG_TUH_HID_MOUSE + CFG_TUSB_HOST_HID_GENERIC +
-                     CFG_TUH_MSC*2 + CFG_TUH_CDC*3),
-
+  // TODO better computation
+  HCD_MAX_ENDPOINT = CFG_TUSB_HOST_DEVICE_MAX*(CFG_TUH_HUB + CFG_TUH_HID*2 + CFG_TUH_MSC*2 + CFG_TUH_CDC*3),
   HCD_MAX_XFER     = HCD_MAX_ENDPOINT*2,
 };
 
@@ -96,27 +91,46 @@ enum {
 #endif
 
 //--------------------------------------------------------------------+
-// Controller & Port API
+// Controller API
 //--------------------------------------------------------------------+
+
+// Initialize controller to host mode
 bool hcd_init(uint8_t rhport);
+
+// Interrupt Handler
 void hcd_int_handler(uint8_t rhport);
+
+// Enable USB interrupt
 void hcd_int_enable (uint8_t rhport);
+
+// Disable USB interrupt
 void hcd_int_disable(uint8_t rhport);
 
 // Get micro frame number (125 us)
 uint32_t hcd_uframe_number(uint8_t rhport);
 
 // Get frame number (1ms)
-static inline uint32_t hcd_frame_number(uint8_t rhport)
+TU_ATTR_ALWAYS_INLINE static inline
+uint32_t hcd_frame_number(uint8_t rhport)
 {
   return hcd_uframe_number(rhport) >> 3;
 }
 
-/// return the current connect status of roothub port
-bool hcd_port_connect_status(uint8_t hostid);
-void hcd_port_reset(uint8_t hostid);
+//--------------------------------------------------------------------+
+// Port API
+//--------------------------------------------------------------------+
+
+// Get the current connect status of roothub port
+bool hcd_port_connect_status(uint8_t rhport);
+
+// Reset USB bus on the port
+void hcd_port_reset(uint8_t rhport);
+
+// TODO implement later
 void hcd_port_reset_end(uint8_t rhport);
-tusb_speed_t hcd_port_speed_get(uint8_t hostid);
+
+// Get port link speed
+tusb_speed_t hcd_port_speed_get(uint8_t rhport);
 
 // HCD closes all opened endpoints belong to this device
 void hcd_device_close(uint8_t rhport, uint8_t dev_addr);
@@ -124,6 +138,7 @@ void hcd_device_close(uint8_t rhport, uint8_t dev_addr);
 //--------------------------------------------------------------------+
 // Endpoints API
 //--------------------------------------------------------------------+
+
 bool hcd_setup_send(uint8_t rhport, uint8_t dev_addr, uint8_t const setup_packet[8]);
 bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const * ep_desc);
 
@@ -135,14 +150,12 @@ bool hcd_edpt_clear_stall(uint8_t dev_addr, uint8_t ep_addr);
 bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t buflen);
 
 //--------------------------------------------------------------------+
-// PIPE API
+// PIPE API - TODO remove later
 //--------------------------------------------------------------------+
 // TODO control xfer should be used via usbh layer
 bool hcd_pipe_queue_xfer(uint8_t dev_addr, uint8_t ep_addr, uint8_t buffer[], uint16_t total_bytes); // only queue, not transferring yet
 bool hcd_pipe_xfer(uint8_t dev_addr, uint8_t ep_addr, uint8_t buffer[], uint16_t total_bytes, bool int_on_complete);
 
-// tusb_error_t hcd_pipe_cancel();
-
 //--------------------------------------------------------------------+
 // Event API (implemented by stack)
 //--------------------------------------------------------------------+
@@ -164,5 +177,3 @@ extern void hcd_event_xfer_complete(uint8_t dev_addr, uint8_t ep_addr, uint32_t
 #endif
 
 #endif /* _TUSB_HCD_H_ */
-
-/// @}

+ 146 - 111
src/host/usbh.c

@@ -74,12 +74,12 @@ static usbh_class_driver_t const usbh_class_drivers[] =
     },
   #endif
 
-  #if HOST_CLASS_HID
+  #if CFG_TUH_HID
     {
       DRIVER_NAME("HID")
       .class_code = TUSB_CLASS_HID,
       .init       = hidh_init,
-      .open       = hidh_open_subtask,
+      .open       = hidh_open,
       .set_config = hidh_set_config,
       .xfer_cb    = hidh_xfer_cb,
       .close      = hidh_close
@@ -121,6 +121,8 @@ enum { CONFIG_NUM = 1 }; // default to use configuration 1
 // INTERNAL OBJECT & FUNCTION DECLARATION
 //--------------------------------------------------------------------+
 
+static bool _usbh_initialized = false;
+
 // including zero-address
 CFG_TUSB_MEM_SECTION usbh_device_t _usbh_devices[CFG_TUSB_HOST_DEVICE_MAX+1];
 
@@ -129,26 +131,21 @@ CFG_TUSB_MEM_SECTION usbh_device_t _usbh_devices[CFG_TUSB_HOST_DEVICE_MAX+1];
 OSAL_QUEUE_DEF(OPT_MODE_HOST, _usbh_qdef, CFG_TUH_TASK_QUEUE_SZ, hcd_event_t);
 static osal_queue_t _usbh_q;
 
-CFG_TUSB_MEM_SECTION TU_ATTR_ALIGNED(4) static uint8_t _usbh_ctrl_buf[CFG_TUSB_HOST_ENUM_BUFFER_SIZE];
+CFG_TUSB_MEM_SECTION CFG_TUSB_MEM_ALIGN static uint8_t _usbh_ctrl_buf[CFG_TUH_ENUMERATION_BUFSZIE];
 
 //------------- Helper Function Prototypes -------------//
 static bool enum_new_device(hcd_event_t* event);
+static void process_device_unplugged(uint8_t rhport, uint8_t hub_addr, uint8_t hub_port);
 
 // from usbh_control.c
 extern bool usbh_control_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes);
 
-uint8_t usbh_get_rhport(uint8_t dev_addr)
-{
-  return _usbh_devices[dev_addr].rhport;
-}
-
 //--------------------------------------------------------------------+
 // PUBLIC API (Parameter Verification is required)
 //--------------------------------------------------------------------+
-tusb_device_state_t tuh_device_get_state (uint8_t const dev_addr)
+bool tuh_device_configured(uint8_t dev_addr)
 {
-  TU_ASSERT( dev_addr <= CFG_TUSB_HOST_DEVICE_MAX, TUSB_DEVICE_STATE_UNPLUG);
-  return (tusb_device_state_t) _usbh_devices[dev_addr].state;
+  return _usbh_devices[dev_addr].configured;
 }
 
 tusb_speed_t tuh_device_get_speed (uint8_t const dev_addr)
@@ -170,8 +167,19 @@ void osal_task_delay(uint32_t msec)
 //--------------------------------------------------------------------+
 // CLASS-USBD API (don't require to verify parameters)
 //--------------------------------------------------------------------+
-bool tuh_init(void)
+
+bool tuh_inited(void)
 {
+  return _usbh_initialized;
+}
+
+bool tuh_init(uint8_t rhport)
+{
+  // skip if already initialized
+  if (_usbh_initialized) return _usbh_initialized;
+
+  TU_LOG2("USBH init\r\n");
+
   tu_memclr(_usbh_devices, sizeof(usbh_device_t)*(CFG_TUSB_HOST_DEVICE_MAX+1));
 
   //------------- Enumeration & Reporter Task init -------------//
@@ -199,12 +207,116 @@ bool tuh_init(void)
     usbh_class_drivers[drv_id].init();
   }
 
-  TU_ASSERT(hcd_init(TUH_OPT_RHPORT));
-  hcd_int_enable(TUH_OPT_RHPORT);
+  TU_ASSERT(hcd_init(rhport));
+  hcd_int_enable(rhport);
 
+  _usbh_initialized = true;
   return true;
 }
 
+/* USB Host Driver task
+ * This top level thread manages all host controller event and delegates events to class-specific drivers.
+ * This should be called periodically within the mainloop or rtos thread.
+ *
+   @code
+    int main(void)
+    {
+      application_init();
+      tusb_init();
+
+      while(1) // the mainloop
+      {
+        application_code();
+        tuh_task(); // tinyusb host task
+      }
+    }
+    @endcode
+ */
+void tuh_task(void)
+{
+  // Skip if stack is not initialized
+  if ( !tusb_inited() ) return;
+
+  // Loop until there is no more events in the queue
+  while (1)
+  {
+    hcd_event_t event;
+    if ( !osal_queue_receive(_usbh_q, &event) ) return;
+
+    switch (event.event_id)
+    {
+      case HCD_EVENT_DEVICE_ATTACH:
+        // TODO due to the shared _usbh_ctrl_buf, we must complete enumerating
+        // one device before enumerating another one.
+        TU_LOG2("USBH DEVICE ATTACH\r\n");
+        enum_new_device(&event);
+      break;
+
+      case HCD_EVENT_DEVICE_REMOVE:
+        TU_LOG2("USBH DEVICE REMOVED\r\n");
+        process_device_unplugged(event.rhport, event.connection.hub_addr, event.connection.hub_port);
+
+        #if CFG_TUH_HUB
+        // TODO remove
+        if ( event.connection.hub_addr != 0)
+        {
+          // done with hub, waiting for next data on status pipe
+          (void) hub_status_pipe_queue( event.connection.hub_addr );
+        }
+        #endif
+      break;
+
+      case HCD_EVENT_XFER_COMPLETE:
+      {
+        usbh_device_t* dev = &_usbh_devices[event.dev_addr];
+        uint8_t const ep_addr = event.xfer_complete.ep_addr;
+        uint8_t const epnum   = tu_edpt_number(ep_addr);
+        uint8_t const ep_dir  = tu_edpt_dir(ep_addr);
+
+        TU_LOG2("on EP %02X with %u bytes\r\n", ep_addr, (unsigned int) event.xfer_complete.len);
+
+        dev->ep_status[epnum][ep_dir].busy = false;
+        dev->ep_status[epnum][ep_dir].claimed = 0;
+
+        if ( 0 == epnum )
+        {
+          usbh_control_xfer_cb(event.dev_addr, ep_addr, event.xfer_complete.result, event.xfer_complete.len);
+        }else
+        {
+          uint8_t drv_id = dev->ep2drv[epnum][ep_dir];
+          TU_ASSERT(drv_id < USBH_CLASS_DRIVER_COUNT, );
+
+          TU_LOG2("%s xfer callback\r\n", usbh_class_drivers[drv_id].name);
+          usbh_class_drivers[drv_id].xfer_cb(event.dev_addr, ep_addr, event.xfer_complete.result, event.xfer_complete.len);
+        }
+      }
+      break;
+
+      case USBH_EVENT_FUNC_CALL:
+        if ( event.func_call.func ) event.func_call.func(event.func_call.param);
+      break;
+
+      default: break;
+    }
+  }
+}
+
+//--------------------------------------------------------------------+
+// USBH API For Class Driver
+//--------------------------------------------------------------------+
+
+uint8_t usbh_get_rhport(uint8_t dev_addr)
+{
+  return _usbh_devices[dev_addr].rhport;
+}
+
+uint8_t* usbh_get_enum_buf(void)
+{
+  return _usbh_ctrl_buf;
+}
+
+//------------- Endpoint API -------------//
+
 bool usbh_edpt_claim(uint8_t dev_addr, uint8_t ep_addr)
 {
   uint8_t const epnum = tu_edpt_number(ep_addr);
@@ -260,10 +372,11 @@ bool usbh_edpt_release(uint8_t dev_addr, uint8_t ep_addr)
 bool usbh_edpt_xfer(uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes)
 {
   usbh_device_t* dev = &_usbh_devices[dev_addr];
+  TU_LOG2("  Queue EP %02X with %u bytes ... OK\r\n", ep_addr, total_bytes);
   return hcd_edpt_xfer(dev->rhport, dev_addr, ep_addr, buffer, total_bytes);
 }
 
-bool usbh_pipe_control_open(uint8_t dev_addr, uint8_t max_packet_size)
+bool usbh_edpt_control_open(uint8_t dev_addr, uint8_t max_packet_size)
 {
   tusb_desc_endpoint_t ep0_desc =
   {
@@ -278,9 +391,11 @@ bool usbh_pipe_control_open(uint8_t dev_addr, uint8_t max_packet_size)
   return hcd_edpt_open(_usbh_devices[dev_addr].rhport, dev_addr, &ep0_desc);
 }
 
-bool usbh_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const * ep_desc)
+bool usbh_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const * desc_ep)
 {
-  bool ret = hcd_edpt_open(rhport, dev_addr, ep_desc);
+  TU_LOG2("  Open EP %02X with Size = %u\r\n", desc_ep->bEndpointAddress, desc_ep->wMaxPacketSize.size);
+
+  bool ret = hcd_edpt_open(rhport, dev_addr, desc_ep);
 
   if (ret)
   {
@@ -296,7 +411,7 @@ bool usbh_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const
     }
     TU_ASSERT(drvid < USBH_CLASS_DRIVER_COUNT);
 
-    uint8_t const ep_addr = ep_desc->bEndpointAddress;
+    uint8_t const ep_addr = desc_ep->bEndpointAddress;
     dev->ep2drv[tu_edpt_number(ep_addr)][tu_edpt_dir(ep_addr)] = drvid;
   }
 
@@ -367,7 +482,7 @@ void hcd_event_device_remove(uint8_t hostid, bool in_isr)
 
 // a device unplugged on hostid, hub_addr, hub_port
 // return true if found and unmounted device, false if cannot find
-static void usbh_device_unplugged(uint8_t rhport, uint8_t hub_addr, uint8_t hub_port)
+void process_device_unplugged(uint8_t rhport, uint8_t hub_addr, uint8_t hub_port)
 {
   //------------- find the all devices (star-network) under port that is unplugged -------------//
   for (uint8_t dev_addr = 0; dev_addr <= CFG_TUSB_HOST_DEVICE_MAX; dev_addr ++)
@@ -400,90 +515,6 @@ static void usbh_device_unplugged(uint8_t rhport, uint8_t hub_addr, uint8_t hub_
   }
 }
 
-/* USB Host Driver task
- * This top level thread manages all host controller event and delegates events to class-specific drivers.
- * This should be called periodically within the mainloop or rtos thread.
- *
-   @code
-    int main(void)
-    {
-      application_init();
-      tusb_init();
-
-      while(1) // the mainloop
-      {
-        application_code();
-        tuh_task(); // tinyusb host task
-      }
-    }
-    @endcode
- */
-void tuh_task(void)
-{
-  // Skip if stack is not initialized
-  if ( !tusb_inited() ) return;
-
-  // Loop until there is no more events in the queue
-  while (1)
-  {
-    hcd_event_t event;
-    if ( !osal_queue_receive(_usbh_q, &event) ) return;
-
-    switch (event.event_id)
-    {
-      case HCD_EVENT_DEVICE_ATTACH:
-        // TODO due to the shared _usbh_ctrl_buf, we must complete enumerating
-        // one device before enumerating another one.
-        TU_LOG2("USBH DEVICE ATTACH\r\n");
-        enum_new_device(&event);
-      break;
-
-      case HCD_EVENT_DEVICE_REMOVE:
-        TU_LOG2("USBH DEVICE REMOVED\r\n");
-        usbh_device_unplugged(event.rhport, event.connection.hub_addr, event.connection.hub_port);
-
-        #if CFG_TUH_HUB
-        // TODO remove
-        if ( event.connection.hub_addr != 0)
-        {
-          // done with hub, waiting for next data on status pipe
-          (void) hub_status_pipe_queue( event.connection.hub_addr );
-        }
-        #endif
-      break;
-
-      case HCD_EVENT_XFER_COMPLETE:
-      {
-        usbh_device_t* dev = &_usbh_devices[event.dev_addr];
-        uint8_t const ep_addr = event.xfer_complete.ep_addr;
-        uint8_t const epnum   = tu_edpt_number(ep_addr);
-        uint8_t const ep_dir  = tu_edpt_dir(ep_addr);
-
-        TU_LOG2("on EP %02X with %u bytes\r\n", ep_addr, (unsigned int) event.xfer_complete.len);
-
-        if ( 0 == epnum )
-        {
-          usbh_control_xfer_cb(event.dev_addr, ep_addr, event.xfer_complete.result, event.xfer_complete.len);
-        }else
-        {
-          uint8_t drv_id = dev->ep2drv[epnum][ep_dir];
-          TU_ASSERT(drv_id < USBH_CLASS_DRIVER_COUNT, );
-
-          TU_LOG2("%s xfer callback\r\n", usbh_class_drivers[drv_id].name);
-          usbh_class_drivers[drv_id].xfer_cb(event.dev_addr, ep_addr, event.xfer_complete.result, event.xfer_complete.len);
-        }
-      }
-      break;
-
-      case USBH_EVENT_FUNC_CALL:
-        if ( event.func_call.func ) event.func_call.func(event.func_call.param);
-      break;
-
-      default: break;
-    }
-  }
-}
-
 //--------------------------------------------------------------------+
 // INTERNAL HELPER
 //--------------------------------------------------------------------+
@@ -507,7 +538,7 @@ void usbh_driver_set_config_complete(uint8_t dev_addr, uint8_t itf_num)
     if (drv_id != 0xff)
     {
       usbh_class_driver_t const * driver = &usbh_class_drivers[drv_id];
-      TU_LOG2("%s set config itf = %u\r\n", driver->name, itf_num);
+      TU_LOG2("%s set config: itf = %u\r\n", driver->name, itf_num);
       driver->set_config(dev_addr, itf_num);
       break;
     }
@@ -612,10 +643,11 @@ static bool enum_hub_get_status0_complete(uint8_t dev_addr, tusb_control_request
 static bool enum_request_set_addr(void)
 {
   // Set Address
-  TU_LOG2("Set Address \r\n");
   uint8_t const new_addr = get_new_address();
   TU_ASSERT(new_addr <= CFG_TUSB_HOST_DEVICE_MAX); // TODO notify application we reach max devices
 
+  TU_LOG2("Set Address = %d\r\n", new_addr);
+
   usbh_device_t* dev0    = &_usbh_devices[0];
   usbh_device_t* new_dev = &_usbh_devices[new_addr];
 
@@ -656,7 +688,7 @@ static bool enum_new_device(hcd_event_t* event)
   //------------- connected/disconnected directly with roothub -------------//
   if (dev0->hub_addr == 0)
   {
-    // wait until device is stable
+    // wait until device is stable TODO non blocking
     osal_task_delay(RESET_DELAY);
 
     // device unplugged while delaying
@@ -682,7 +714,7 @@ static bool enum_new_device(hcd_event_t* event)
 static bool enum_request_addr0_device_desc(void)
 {
   // TODO probably doesn't need to open/close each enumeration
-  TU_ASSERT( usbh_pipe_control_open(0, 8) );
+  TU_ASSERT( usbh_edpt_control_open(0, 8) );
 
   //------------- Get first 8 bytes of device descriptor to get Control Endpoint Size -------------//
   TU_LOG2("Get 8 byte of Device Descriptor\r\n");
@@ -766,9 +798,10 @@ static bool enum_set_address_complete(uint8_t dev_addr, tusb_control_request_t c
   dev0->state = TUSB_DEVICE_STATE_UNPLUG;
 
   // open control pipe for new address
-  TU_ASSERT ( usbh_pipe_control_open(new_addr, new_dev->ep0_packet_size) );
+  TU_ASSERT ( usbh_edpt_control_open(new_addr, new_dev->ep0_packet_size) );
 
   // Get full device descriptor
+  TU_LOG2("Get Device Descriptor\r\n");
   tusb_control_request_t const new_request =
   {
     .bmRequestType_bit =
@@ -833,9 +866,10 @@ static bool enum_get_9byte_config_desc_complete(uint8_t dev_addr, tusb_control_r
   // Use offsetof to avoid pointer to the odd/misaligned address
   memcpy(&total_len, (uint8_t*) desc_config + offsetof(tusb_desc_configuration_t, wTotalLength), 2);
 
-  TU_ASSERT(total_len <= CFG_TUSB_HOST_ENUM_BUFFER_SIZE);
+  TU_ASSERT(total_len <= CFG_TUH_ENUMERATION_BUFSZIE);
 
-  //Get full configuration descriptor
+  // Get full configuration descriptor
+  TU_LOG2("Get Configuration Descriptor\r\n");
   tusb_control_request_t const new_request =
   {
     .bmRequestType_bit =
@@ -865,7 +899,7 @@ static bool enum_get_config_desc_complete(uint8_t dev_addr, tusb_control_request
   // Driver open aren't allowed to make any usb transfer yet
   parse_configuration_descriptor(dev_addr, (tusb_desc_configuration_t*) _usbh_ctrl_buf);
 
-  TU_LOG2("Set Configuration Descriptor\r\n");
+  TU_LOG2("Set Configuration = %d\r\n", CONFIG_NUM);
   tusb_control_request_t const new_request =
   {
     .bmRequestType_bit =
@@ -898,6 +932,7 @@ static bool enum_set_config_complete(uint8_t dev_addr, tusb_control_request_t co
   // Start the Set Configuration process for interfaces (itf = 0xff)
   // Since driver can perform control transfer within its set_config, this is done asynchronously.
   // The process continue with next interface when class driver complete its sequence with usbh_driver_set_config_complete()
+  // TODO use separated API instead of usig 0xff
   usbh_driver_set_config_complete(dev_addr, 0xff);
 
   return true;

+ 15 - 4
src/host/usbh.h

@@ -76,7 +76,10 @@ typedef bool (*tuh_control_complete_cb_t)(uint8_t dev_addr, tusb_control_request
 //--------------------------------------------------------------------+
 
 // Init host stack
-bool tuh_init(void);
+bool tuh_init(uint8_t rhport);
+
+// Check if host stack is already initialized
+bool tuh_inited(void);
 
 // Task function should be called in main/rtos loop
 void tuh_task(void);
@@ -85,13 +88,19 @@ void tuh_task(void);
 extern void hcd_int_handler(uint8_t rhport);
 #define tuh_int_handler   hcd_int_handler
 
-tusb_device_state_t tuh_device_get_state (uint8_t dev_addr);
 tusb_speed_t tuh_device_get_speed (uint8_t dev_addr);
-static inline bool tuh_device_is_configured(uint8_t dev_addr)
+
+// Check if device is configured
+bool tuh_device_configured(uint8_t dev_addr);
+
+// Check if device is ready to communicate with
+TU_ATTR_ALWAYS_INLINE
+static inline bool tuh_device_ready(uint8_t dev_addr)
 {
-  return tuh_device_get_state(dev_addr) == TUSB_DEVICE_STATE_CONFIGURED;
+  return tuh_device_configured(dev_addr);
 }
 
+// Carry out control transfer
 bool tuh_control_xfer (uint8_t dev_addr, tusb_control_request_t const* request, void* buffer, tuh_control_complete_cb_t complete_cb);
 
 //--------------------------------------------------------------------+
@@ -121,6 +130,8 @@ void usbh_driver_set_config_complete(uint8_t dev_addr, uint8_t itf_num);
 
 uint8_t usbh_get_rhport(uint8_t dev_addr);
 
+uint8_t* usbh_get_enum_buf(void);
+
 #ifdef __cplusplus
  }
 #endif

+ 2 - 1
src/host/usbh_control.c

@@ -50,7 +50,7 @@ typedef struct
 static usbh_control_xfer_t _ctrl_xfer;
 
 //CFG_TUSB_MEM_SECTION CFG_TUSB_MEM_ALIGN
-//static uint8_t _tuh_ctrl_buf[CFG_TUSB_HOST_ENUM_BUFFER_SIZE];
+//static uint8_t _tuh_ctrl_buf[CFG_TUH_ENUMERATION_BUFSZIE];
 
 //--------------------------------------------------------------------+
 // MACRO TYPEDEF CONSTANT ENUM DECLARATION
@@ -80,6 +80,7 @@ bool tuh_control_xfer (uint8_t dev_addr, tusb_control_request_t const* request,
 
 static void _xfer_complete(uint8_t dev_addr, xfer_result_t result)
 {
+  TU_LOG2("\r\n");
   if (_ctrl_xfer.complete_cb) _ctrl_xfer.complete_cb(dev_addr, &_ctrl_xfer.request, result);
 }
 

+ 9 - 11
src/portable/ehci/ehci.c

@@ -37,6 +37,7 @@
 
 #include "host/hcd.h"
 #include "host/usbh_hcd.h"
+#include "hcd_ehci.h"
 #include "ehci.h"
 
 //--------------------------------------------------------------------+
@@ -49,10 +50,6 @@
 // Periodic frame list must be 4K alignment
 CFG_TUSB_MEM_SECTION TU_ATTR_ALIGNED(4096) static ehci_data_t ehci_data;
 
-// EHCI portable
-uint32_t hcd_ehci_register_addr(uint8_t rhport);
-bool hcd_ehci_init (uint8_t rhport); // TODO move later
-
 //--------------------------------------------------------------------+
 // PROTOTYPE
 //--------------------------------------------------------------------+
@@ -516,18 +513,19 @@ static void qhd_xfer_complete_isr(ehci_qhd_t * p_qhd)
   // free all TDs from the head td to the first active TD
   while(p_qhd->p_qtd_list_head != NULL && !p_qhd->p_qtd_list_head->active)
   {
-    // TD need to be freed and removed from qhd, before invoking callback
-    bool is_ioc = (p_qhd->p_qtd_list_head->int_on_complete != 0);
-    p_qhd->total_xferred_bytes += p_qhd->p_qtd_list_head->expected_bytes - p_qhd->p_qtd_list_head->total_bytes;
+    ehci_qtd_t * volatile qtd = (ehci_qtd_t * volatile) p_qhd->p_qtd_list_head;
+    bool const is_ioc = (qtd->int_on_complete != 0);
+    uint8_t const ep_addr = tu_edpt_addr(p_qhd->ep_number, qtd->pid == EHCI_PID_IN ? 1 : 0);
 
-    p_qhd->p_qtd_list_head->used = 0; // free QTD
+    p_qhd->total_xferred_bytes += qtd->expected_bytes - qtd->total_bytes;
+
+    // TD need to be freed and removed from qhd, before invoking callback
+    qtd->used = 0; // free QTD
     qtd_remove_1st_from_qhd(p_qhd);
 
     if (is_ioc)
     {
-      // end of request
-      // call USBH callback
-      hcd_event_xfer_complete(p_qhd->dev_addr, tu_edpt_addr(p_qhd->ep_number, p_qhd->pid == EHCI_PID_IN ? 1 : 0), p_qhd->total_xferred_bytes, XFER_RESULT_SUCCESS, true);
+      hcd_event_xfer_complete(p_qhd->dev_addr, ep_addr, p_qhd->total_xferred_bytes, XFER_RESULT_SUCCESS, true);
       p_qhd->total_xferred_bytes = 0;
     }
   }

+ 53 - 0
src/portable/ehci/hcd_ehci.h

@@ -0,0 +1,53 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2021, Ha Thach (tinyusb.org)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * This file is part of the TinyUSB stack.
+ */
+
+#ifndef _TUSB_HCD_EHCI_H_
+#define _TUSB_HCD_EHCI_H_
+
+#ifdef __cplusplus
+ extern "C" {
+#endif
+
+
+//--------------------------------------------------------------------+
+// API Implemented by HCD
+//--------------------------------------------------------------------+
+
+// Get operational address i.e EHCI Command register
+uint32_t hcd_ehci_register_addr(uint8_t rhport);
+
+//--------------------------------------------------------------------+
+// API Implemented by EHCI
+//--------------------------------------------------------------------+
+
+// Initialize EHCI driver
+extern bool hcd_ehci_init (uint8_t rhport);
+
+#ifdef __cplusplus
+ }
+#endif
+
+#endif

+ 1 - 1
src/portable/espressif/esp32sx/dcd_esp32sx.c

@@ -27,7 +27,6 @@
  */
 
 #include "tusb_option.h"
-#include "common/tusb_fifo.h"
 
 #if (((CFG_TUSB_MCU == OPT_MCU_ESP32S2) ||  (CFG_TUSB_MCU == OPT_MCU_ESP32S3)) && TUSB_OPT_DEVICE_ENABLED)
 
@@ -41,6 +40,7 @@
 #include "soc/gpio_sig_map.h"
 #include "soc/usb_periph.h"
 
+#include "common/tusb_fifo.h"
 #include "device/dcd.h"
 
 // Since TinyUSB doesn't use SOF for now, and this interrupt too often (1ms interval)

+ 1 - 1
src/portable/microchip/samg/dcd_samg.c

@@ -25,11 +25,11 @@
  */
 
 #include "tusb_option.h"
-#include "common/tusb_fifo.h"
 
 #if CFG_TUSB_MCU == OPT_MCU_SAMG
 
 #include "sam.h"
+#include "common/tusb_fifo.h"
 #include "device/dcd.h"
 
 // TODO should support (SAM3S || SAM4S || SAM4E || SAMG55)

+ 5 - 2
src/portable/nordic/nrf5x/dcd_nrf5x.c

@@ -833,9 +833,8 @@ void tusb_hal_nrf_power_event (uint32_t event)
         NRF_USBD->EVENTCAUSE = USBD_EVENTCAUSE_READY_Msk;
         __ISB(); __DSB(); // for sync
 
-        /* Enable the peripheral */
+#ifdef NRF52_SERIES
         // ERRATA 171, 187, 166
-
         if ( nrfx_usbd_errata_187() )
         {
           // CRITICAL_REGION_ENTER();
@@ -867,7 +866,9 @@ void tusb_hal_nrf_power_event (uint32_t event)
           }
           // CRITICAL_REGION_EXIT();
         }
+#endif
 
+        /* Enable the peripheral */
         NRF_USBD->ENABLE = 1;
         __ISB(); __DSB(); // for sync
 
@@ -889,6 +890,7 @@ void tusb_hal_nrf_power_event (uint32_t event)
       NRF_USBD->EVENTCAUSE = USBD_EVENTCAUSE_READY_Msk;
       __ISB(); __DSB(); // for sync
 
+#ifdef NRF52_SERIES
       if ( nrfx_usbd_errata_171() )
       {
         // CRITICAL_REGION_ENTER();
@@ -929,6 +931,7 @@ void tusb_hal_nrf_power_event (uint32_t event)
 
         __ISB(); __DSB();
       }
+#endif
 
       // ISO buffer Lower half for IN, upper half for OUT
       NRF_USBD->ISOSPLIT = USBD_ISOSPLIT_SPLIT_HalfIN;

+ 1 - 1
src/portable/nuvoton/nuc120/dcd_nuc120.c

@@ -34,10 +34,10 @@
 */
 
 #include "tusb_option.h"
-#include "common/tusb_fifo.h"
 
 #if TUSB_OPT_DEVICE_ENABLED && (CFG_TUSB_MCU == OPT_MCU_NUC120)
 
+#include "common/tusb_fifo.h"
 #include "device/dcd.h"
 #include "NUC100Series.h"
 

+ 1 - 1
src/portable/nuvoton/nuc121/dcd_nuc121.c

@@ -34,10 +34,10 @@
 */
 
 #include "tusb_option.h"
-#include "common/tusb_fifo.h"
 
 #if TUSB_OPT_DEVICE_ENABLED && ( (CFG_TUSB_MCU == OPT_MCU_NUC121) || (CFG_TUSB_MCU == OPT_MCU_NUC126) )
 
+#include "common/tusb_fifo.h"
 #include "device/dcd.h"
 #include "NuMicro.h"
 

+ 1 - 1
src/portable/nuvoton/nuc505/dcd_nuc505.c

@@ -34,10 +34,10 @@
 */
 
 #include "tusb_option.h"
-#include "common/tusb_fifo.h"
 
 #if TUSB_OPT_DEVICE_ENABLED && (CFG_TUSB_MCU == OPT_MCU_NUC505)
 
+#include "common/tusb_fifo.h"
 #include "device/dcd.h"
 #include "NUC505Series.h"
 

+ 1 - 3
src/portable/nxp/transdimension/hcd_transdimension.c

@@ -43,6 +43,7 @@
 
 #include "common/tusb_common.h"
 #include "common_transdimension.h"
+#include "portable/ehci/hcd_ehci.h"
 
 //--------------------------------------------------------------------+
 // MACRO CONSTANT TYPEDEF
@@ -75,9 +76,6 @@ typedef struct
   };
 #endif
 
-// TODO better prototype later
-extern bool hcd_ehci_init (uint8_t rhport); // from ehci.c
-
 //--------------------------------------------------------------------+
 // Controller API
 //--------------------------------------------------------------------+

+ 2 - 3
src/portable/raspberrypi/rp2040/hcd_rp2040.c

@@ -306,8 +306,8 @@ static void _hw_endpoint_init(struct hw_endpoint *ep, uint8_t dev_addr, uint8_t
     ep->wMaxPacketSize = wMaxPacketSize;
     ep->transfer_type = transfer_type;
 
-    pico_trace("hw_endpoint_init dev %d ep %d %s xfer %d\n", ep->dev_addr, ep->num, ep_dir_string[ep->in], ep->transfer_type);
-    pico_trace("dev %d ep %d %s setup buffer @ 0x%p\n", ep->dev_addr, ep->num, ep_dir_string[ep->in], ep->hw_data_buf);
+    pico_trace("hw_endpoint_init dev %d ep %d %s xfer %d\n", ep->dev_addr, tu_edpt_number(ep->ep_addr), ep_dir_string[tu_edpt_dir(ep->ep_addr)], ep->transfer_type);
+    pico_trace("dev %d ep %d %s setup buffer @ 0x%p\n", ep->dev_addr, tu_edpt_number(ep->ep_addr), ep_dir_string[tu_edpt_dir(ep->ep_addr)], ep->hw_data_buf);
     uint dpram_offset = hw_data_offset(ep->hw_data_buf);
     // Bits 0-5 should be 0
     assert(!(dpram_offset & 0b111111));
@@ -487,7 +487,6 @@ bool hcd_setup_send(uint8_t rhport, uint8_t dev_addr, uint8_t const setup_packet
     // EP0 out
     _hw_endpoint_init(ep, dev_addr, 0x00, ep->wMaxPacketSize, 0, 0);
     assert(ep->configured);
-    assert(ep->num == 0 && !ep->in);
     ep->total_len = 8;
     ep->transfer_size = 8;
     ep->active = true;

+ 1 - 1
src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c

@@ -102,7 +102,6 @@
  */
 
 #include "tusb_option.h"
-#include "common/tusb_fifo.h"
 
 #if defined(STM32F102x6) || defined(STM32F102xB) || \
     defined(STM32F103x6) || defined(STM32F103xB) || \
@@ -121,6 +120,7 @@
 // Some definitions are copied to our private include file.
 #undef USE_HAL_DRIVER
 
+#include "common/tusb_fifo.h"
 #include "device/dcd.h"
 #include "portable/st/stm32_fsdev/dcd_stm32_fsdev_pvt_st.h"
 

+ 1 - 1
src/portable/st/synopsys/dcd_synopsys.c

@@ -28,7 +28,6 @@
  */
 
 #include "tusb_option.h"
-#include "common/tusb_fifo.h"
 
 // Since TinyUSB doesn't use SOF for now, and this interrupt too often (1ms interval)
 // We disable SOF for now until needed later on
@@ -98,6 +97,7 @@
 
 #endif
 
+#include "common/tusb_fifo.h"
 #include "device/dcd.h"
 
 //--------------------------------------------------------------------+

+ 1 - 1
src/portable/template/dcd_template.c

@@ -25,10 +25,10 @@
  */
 
 #include "tusb_option.h"
-#include "common/tusb_fifo.h"
 
 #if CFG_TUSB_MCU == OPT_MCU_NONE
 
+#include "common/tusb_fifo.h"
 #include "device/dcd.h"
 
 //--------------------------------------------------------------------+

+ 1 - 1
src/portable/ti/msp430x5xx/dcd_msp430x5xx.c

@@ -26,11 +26,11 @@
  */
 
 #include "tusb_option.h"
-#include "common/tusb_fifo.h"
 
 #if TUSB_OPT_DEVICE_ENABLED && ( CFG_TUSB_MCU == OPT_MCU_MSP430x5xx )
 
 #include "msp430.h"
+#include "common/tusb_fifo.h"
 #include "device/dcd.h"
 
 /*------------------------------------------------------------------*/

+ 15 - 12
src/tusb.c

@@ -30,8 +30,6 @@
 
 #include "tusb.h"
 
-static bool _initialized = false;
-
 // TODO clean up
 #if TUSB_OPT_DEVICE_ENABLED
 #include "device/usbd_pvt.h"
@@ -39,25 +37,30 @@ static bool _initialized = false;
 
 bool tusb_init(void)
 {
-  // skip if already initialized
-  if (_initialized) return true;
-
-#if TUSB_OPT_HOST_ENABLED
-  TU_ASSERT( tuh_init() ); // init host stack
-#endif
-
 #if TUSB_OPT_DEVICE_ENABLED
-  TU_ASSERT ( tud_init() ); // init device stack
+  TU_ASSERT ( tud_init(TUD_OPT_RHPORT) ); // init device stack
 #endif
 
-  _initialized = true;
+#if TUSB_OPT_HOST_ENABLED
+  TU_ASSERT( tuh_init(TUH_OPT_RHPORT) ); // init host stack
+#endif
 
   return true;
 }
 
 bool tusb_inited(void)
 {
-  return _initialized;
+  bool ret = false;
+
+#if TUSB_OPT_DEVICE_ENABLED
+  ret = ret || tud_inited();
+#endif
+
+#if TUSB_OPT_HOST_ENABLED
+  ret = ret || tuh_inited();
+#endif
+
+  return ret;
 }
 
 /*------------------------------------------------------------------*/

+ 1 - 1
src/tusb.h

@@ -42,7 +42,7 @@
 #if TUSB_OPT_HOST_ENABLED
   #include "host/usbh.h"
 
-  #if HOST_CLASS_HID
+  #if CFG_TUH_HID
     #include "class/hid/hid_host.h"
   #endif
 

+ 2 - 5
src/tusb_option.h

@@ -274,11 +274,8 @@
     #error there is no benefit enable hub with max device is 1. Please disable hub or increase CFG_TUSB_HOST_DEVICE_MAX
   #endif
 
-  //------------- HID CLASS -------------//
-  #define HOST_CLASS_HID   ( CFG_TUH_HID_KEYBOARD + CFG_TUH_HID_MOUSE + CFG_TUSB_HOST_HID_GENERIC )
-
-  #ifndef CFG_TUSB_HOST_ENUM_BUFFER_SIZE
-    #define CFG_TUSB_HOST_ENUM_BUFFER_SIZE 256
+  #ifndef CFG_TUH_ENUMERATION_BUFSZIE
+    #define CFG_TUH_ENUMERATION_BUFSZIE 256
   #endif
 
   //------------- CLASS -------------//