Эх сурвалжийг харах

add tud_descriptor_string_cb() for getting string descriptor from application

- remove tud_desc_set.string_arr/string_count
hathach 6 жил өмнө
parent
commit
de56a0ca89

+ 42 - 28
examples/device/cdc_msc_hid/src/usb_descriptors.c

@@ -136,31 +136,6 @@ uint8_t const desc_configuration[] =
 #endif
 };
 
-//------------- String Descriptors -------------//
-// array of pointer to string descriptors
-uint16_t const * const string_desc_arr [] =
-{
-  // 0: is supported language = English
-  TUD_DESC_STRCONV(0x0409),
-
-  // 1: Manufacturer
-  TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', '.', 'o', 'r', 'g'),
-
-  // 2: Product
-  TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', ' ', 'd', 'e', 'v', 'i', 'c', 'e'),
-
-  // 3: Serials, should use chip ID
-  TUD_DESC_STRCONV('1', '2', '3', '4', '5', '6'),
-
-  // 4: CDC Interface
-  TUD_DESC_STRCONV('t','u','s','b',' ','c','d','c'),
-
-  // 5: MSC Interface
-  TUD_DESC_STRCONV('t','u','s','b',' ','m','s','c'),
-
-  // 6: HID
-  TUD_DESC_STRCONV('t','u','s','b',' ','h','i','d')
-};
 
 // tud_desc_set is required by tinyusb stack
 tud_desc_set_t tud_desc_set =
@@ -168,10 +143,49 @@ tud_desc_set_t tud_desc_set =
   .device     = &desc_device,
   .config     = desc_configuration,
 
-  .string_arr   = (uint8_t const **) string_desc_arr,
-  .string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]),
-
 #if CFG_TUD_HID
   .hid_report = desc_hid_report,
 #endif
 };
+
+//------------- String Descriptors -------------//
+
+// array of pointer to string descriptors
+char const* string_desc_arr [] =
+{
+  (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
+  "TinyUSB",                     // 1: Manufacturer
+  "TinyUSB Device",              // 2: Product
+  "123456",                      // 3: Serials, should use chip ID
+  "TinyUSB CDC",                 // 4: CDC Interface
+  "TinyUSB MSC",                 // 5: MSC Interface
+  "TinyUSB HID"                  // 6: HID
+};
+
+// Invoked when received GET_STRING_DESC request
+// max_char is CFG_TUD_ENDOINT0_SIZE/2 -1, typically max_char = 31 if Endpoint0 size is 64
+// Return number of characters. Note usb string is in 16-bits unicode format
+uint8_t tud_descriptor_string_cb(uint8_t index, uint16_t* desc, uint8_t max_char)
+{
+  if ( index == 0)
+  {
+    memcpy(desc, string_desc_arr[0], 2);
+    return 1;
+  }else
+  {
+    if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return 0;
+
+    const char* str = string_desc_arr[index];
+
+    // Cap at max char
+    uint8_t count = strlen(str);
+    if ( count > max_char ) count = max_char;
+
+    for(uint8_t i=0; i<count; i++)
+    {
+      *desc++ = str[i];
+    }
+
+    return count;
+  }
+}

+ 42 - 29
examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c

@@ -136,42 +136,55 @@ uint8_t const desc_configuration[] =
 #endif
 };
 
-//------------- String Descriptors -------------//
-// array of pointer to string descriptors
-uint16_t const * const string_desc_arr [] =
-{
-  // 0: is supported language = English
-  TUD_DESC_STRCONV(0x0409),
-
-  // 1: Manufacturer
-  TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', '.', 'o', 'r', 'g'),
-
-  // 2: Product
-  TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', ' ', 'd', 'e', 'v', 'i', 'c', 'e'),
-
-  // 3: Serials, should use chip ID
-  TUD_DESC_STRCONV('1', '2', '3', '4', '5', '6'),
-
-  // 4: CDC Interface
-  TUD_DESC_STRCONV('t','u','s','b',' ','c','d','c'),
-
-  // 5: MSC Interface
-  TUD_DESC_STRCONV('t','u','s','b',' ','m','s','c'),
-
-  // 6: HID
-  TUD_DESC_STRCONV('t','u','s','b',' ','h','i','d')
-};
-
 // tud_desc_set is required by tinyusb stack
 tud_desc_set_t tud_desc_set =
 {
     .device     = &desc_device,
     .config     = desc_configuration,
 
-    .string_arr   = (uint8_t const **) string_desc_arr,
-    .string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]),
-
 #if CFG_TUD_HID
     .hid_report = desc_hid_report,
 #endif
 };
+
+//------------- String Descriptors -------------//
+
+// array of pointer to string descriptors
+char const* string_desc_arr [] =
+{
+  (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
+  "TinyUSB",                     // 1: Manufacturer
+  "TinyUSB Device",              // 2: Product
+  "123456",                      // 3: Serials, should use chip ID
+  "TinyUSB CDC",                 // 4: CDC Interface
+  "TinyUSB MSC",                 // 5: MSC Interface
+  "TinyUSB HID"                  // 6: HID
+};
+
+// Invoked when received GET_STRING_DESC request
+// max_char is CFG_TUD_ENDOINT0_SIZE/2 -1, typically max_char = 31 if Endpoint0 size is 64
+// Return number of characters. Note usb string is in 16-bits unicode format
+uint8_t tud_descriptor_string_cb(uint8_t index, uint16_t* desc, uint8_t max_char)
+{
+  if ( index == 0)
+  {
+    memcpy(desc, string_desc_arr[0], 2);
+    return 1;
+  }else
+  {
+    if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return 0;
+
+    const char* str = string_desc_arr[index];
+
+    // Cap at max char
+    uint8_t count = strlen(str);
+    if ( count > max_char ) count = max_char;
+
+    for(uint8_t i=0; i<count; i++)
+    {
+      *desc++ = str[i];
+    }
+
+    return count;
+  }
+}

+ 43 - 20
examples/device/hid_generic_inout/src/usb_descriptors.c

@@ -88,30 +88,53 @@ uint8_t const desc_configuration[] =
   TUD_HID_INOUT_DESCRIPTOR(ITF_NUM_HID, 0, HID_PROTOCOL_NONE, sizeof(desc_hid_report), 0x80 | EPNUM_HID, EPNUM_HID, 16, 10)
 };
 
-//------------- String Descriptors -------------//
-// array of pointer to string descriptors
-uint16_t const * const string_desc_arr [] =
+
+// tud_desc_set is required by tinyusb stack
+tud_desc_set_t tud_desc_set =
 {
-  // 0: is supported language = English
-  TUD_DESC_STRCONV(0x0409),
+    .device     = &desc_device,
+    .config     = desc_configuration,
 
-  // 1: Manufacturer
-  TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', '.', 'o', 'r', 'g'),
+#if CFG_TUD_HID
+    .hid_report = desc_hid_report,
+#endif
+};
 
-  // 2: Product
-  TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', ' ', 'd', 'e', 'v', 'i', 'c', 'e'),
+//------------- String Descriptors -------------//
 
-  // 3: Serials, should use chip ID
-  TUD_DESC_STRCONV('1', '2', '3', '4', '5', '6'),
+// array of pointer to string descriptors
+char const* string_desc_arr [] =
+{
+  (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
+  "TinyUSB",                     // 1: Manufacturer
+  "TinyUSB Device",              // 2: Product
+  "123456",                      // 3: Serials, should use chip ID
 };
 
-// tud_desc_set is required by tinyusb stack
-tud_desc_set_t tud_desc_set =
+// Invoked when received GET_STRING_DESC request
+// max_char is CFG_TUD_ENDOINT0_SIZE/2 -1, typically max_char = 31 if Endpoint0 size is 64
+// Return number of characters. Note usb string is in 16-bits unicode format
+uint8_t tud_descriptor_string_cb(uint8_t index, uint16_t* desc, uint8_t max_char)
 {
-  .device       = &desc_device,
-  .config       = desc_configuration,
-
-  .string_arr   = (uint8_t const **) string_desc_arr,
-  .string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]),
-  .hid_report   = desc_hid_report,
-};
+  if ( index == 0)
+  {
+    memcpy(desc, string_desc_arr[0], 2);
+    return 1;
+  }else
+  {
+    if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return 0;
+
+    const char* str = string_desc_arr[index];
+
+    // Cap at max char
+    uint8_t count = strlen(str);
+    if ( count > max_char ) count = max_char;
+
+    for(uint8_t i=0; i<count; i++)
+    {
+      *desc++ = str[i];
+    }
+
+    return count;
+  }
+}

+ 43 - 19
examples/device/msc_dual_lun/src/usb_descriptors.c

@@ -82,29 +82,53 @@ uint8_t const desc_configuration[] =
   TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 0, EPNUM_MSC, 0x80 | EPNUM_MSC, 64), // highspeed 512
 };
 
-//------------- String Descriptors -------------//
-// array of pointer to string descriptors
-uint16_t const * const string_desc_arr [] =
+
+// tud_desc_set is required by tinyusb stack
+tud_desc_set_t tud_desc_set =
 {
-  // 0: is supported language = English
-  TUD_DESC_STRCONV(0x0409),
+    .device     = &desc_device,
+    .config     = desc_configuration,
 
-  // 1: Manufacturer
-  TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', '.', 'o', 'r', 'g'),
+#if CFG_TUD_HID
+    .hid_report = desc_hid_report,
+#endif
+};
 
-  // 2: Product
-  TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', ' ', 'd', 'e', 'v', 'i', 'c', 'e'),
+//------------- String Descriptors -------------//
 
-  // 3: Serials, should use chip ID
-  TUD_DESC_STRCONV('1', '2', '3', '4', '5', '6')
+// array of pointer to string descriptors
+char const* string_desc_arr [] =
+{
+  (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
+  "TinyUSB",                     // 1: Manufacturer
+  "TinyUSB Device",              // 2: Product
+  "123456",                      // 3: Serials, should use chip ID
 };
 
-// tud_desc_set is required by tinyusb stack
-tud_desc_set_t tud_desc_set =
+// Invoked when received GET_STRING_DESC request
+// max_char is CFG_TUD_ENDOINT0_SIZE/2 -1, typically max_char = 31 if Endpoint0 size is 64
+// Return number of characters. Note usb string is in 16-bits unicode format
+uint8_t tud_descriptor_string_cb(uint8_t index, uint16_t* desc, uint8_t max_char)
 {
-  .device       = &desc_device,
-  .config       = desc_configuration,
-  .string_arr   = (uint8_t const **) string_desc_arr,
-  .string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]),
-  .hid_report   = NULL,
-};
+  if ( index == 0)
+  {
+    memcpy(desc, string_desc_arr[0], 2);
+    return 1;
+  }else
+  {
+    if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return 0;
+
+    const char* str = string_desc_arr[index];
+
+    // Cap at max char
+    uint8_t count = strlen(str);
+    if ( count > max_char ) count = max_char;
+
+    for(uint8_t i=0; i<count; i++)
+    {
+      *desc++ = str[i];
+    }
+
+    return count;
+  }
+}

+ 5 - 0
src/common/tusb_common.h

@@ -142,6 +142,11 @@ static inline uint32_t tu_u32_from_u8(uint8_t b1, uint8_t b2, uint8_t b3, uint8_
   return ( ((uint32_t) b1) << 24) + ( ((uint32_t) b2) << 16) + ( ((uint32_t) b3) << 8) + b4;
 }
 
+static inline uint16_t tu_u16_from_u8(uint8_t high, uint8_t low)
+{
+  return (((uint16_t) high) << 8) + low;
+}
+
 static inline uint8_t tu_u16_high(uint16_t u16)
 {
   return (uint8_t) ( ((uint16_t) (u16 >> 8)) & 0x00ff);

+ 22 - 31
src/device/usbd.c

@@ -160,7 +160,7 @@ static osal_queue_t _usbd_q;
 static void mark_interface_endpoint(uint8_t ep2drv[8][2], uint8_t const* p_desc, uint16_t desc_len, uint8_t driver_id);
 static bool process_control_request(uint8_t rhport, tusb_control_request_t const * p_request);
 static bool process_set_config(uint8_t rhport);
-static void const* get_descriptor(tusb_control_request_t const * p_request, uint16_t* desc_len);
+static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const * p_request);
 
 void usbd_control_reset (uint8_t rhport);
 bool usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
@@ -382,13 +382,7 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const
         break;
 
         case TUSB_REQ_GET_DESCRIPTOR:
-        {
-          uint16_t len = 0;
-          void* buf = (void*) get_descriptor(p_request, &len);
-          if ( buf == NULL || len == 0 ) return false;
-
-          usbd_control_xfer(rhport, p_request, buf, len);
-        }
+          TU_ASSERT( process_get_descriptor(rhport, p_request) );
         break;
 
         case TUSB_REQ_SET_FEATURE:
@@ -556,56 +550,53 @@ static void mark_interface_endpoint(uint8_t ep2drv[8][2], uint8_t const* p_desc,
 }
 
 // return descriptor's buffer and update desc_len
-static void const* get_descriptor(tusb_control_request_t const * p_request, uint16_t* desc_len)
+static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const * p_request)
 {
   tusb_desc_type_t const desc_type = (tusb_desc_type_t) tu_u16_high(p_request->wValue);
   uint8_t const desc_index = tu_u16_low( p_request->wValue );
 
-  uint8_t const * desc_data = NULL;
-  uint16_t len = 0;
-
-  *desc_len = 0;
-
   switch(desc_type)
   {
     case TUSB_DESC_DEVICE:
-      desc_data = (uint8_t const *) tud_desc_set.device;
-      len       = sizeof(tusb_desc_device_t);
+      return usbd_control_xfer(rhport, p_request, (void*) tud_desc_set.device, sizeof(tusb_desc_device_t));
     break;
 
     case TUSB_DESC_CONFIGURATION:
-      desc_data = (uint8_t const *) tud_desc_set.config;
-      len       = ((tusb_desc_configuration_t const*) desc_data)->wTotalLength;
+      return usbd_control_xfer(rhport, p_request, (void*) tud_desc_set.config, ((tusb_desc_configuration_t const*) tud_desc_set.config)->wTotalLength);
     break;
 
     case TUSB_DESC_STRING:
       // String Descriptor always uses the desc set from user
-      if ( desc_index < tud_desc_set.string_count )
-      {
-        desc_data = tud_desc_set.string_arr[desc_index];
-        TU_VERIFY( desc_data != NULL, NULL );
-
-        len  = desc_data[0];  // first byte of descriptor is its size
-      }else
+      if ( desc_index == 0xEE )
       {
-        // out of range
         // The 0xEE index string is a Microsoft USB extension.
         // It can be used to tell Windows what driver it should use for the device !!!
-        return NULL;
+        return false;
+      }else
+      {
+        uint16_t desc_str[CFG_TUD_ENDOINT0_SIZE/2]; // up to endpoint0 size only
+        uint8_t len = 2*tud_descriptor_string_cb(desc_index, desc_str+1, CFG_TUD_ENDOINT0_SIZE/2-1);
+
+        TU_ASSERT(len > 0);
+
+        // first byte of descriptor is size, second byte is string type
+        len += 2; // header len
+        desc_str[0] = tu_u16_from_u8(TUSB_DESC_STRING, len);
+
+        return usbd_control_xfer(rhport, p_request, desc_str, len);
       }
     break;
 
     case TUSB_DESC_DEVICE_QUALIFIER:
       // TODO If not highspeed capable stall this request otherwise
       // return the descriptor that could work in highspeed
-      return NULL;
+      return false;
     break;
 
-    default: return NULL;
+    default: return false;
   }
 
-  *desc_len = len;
-  return desc_data;
+  return true;
 }
 
 //--------------------------------------------------------------------+

+ 7 - 6
src/device/usbd.h

@@ -39,12 +39,8 @@
 
 /// \brief Descriptor pointer collector to all the needed.
 typedef struct {
-  void const * device;            ///< pointer to device descriptor \ref tusb_desc_device_t
-  void const * config;            ///< pointer to the whole configuration descriptor, starting by \ref tusb_desc_configuration_t
-
-  uint8_t const** string_arr;     ///< a array of pointers to string descriptors
-  uint16_t        string_count;
-
+  void const * device;        ///< pointer to device descriptor \ref tusb_desc_device_t
+  void const * config;        ///< pointer to the whole configuration descriptor, starting by \ref tusb_desc_configuration_t
   uint8_t const*  hid_report;
 
 }tud_desc_set_t;
@@ -78,6 +74,11 @@ bool tud_remote_wakeup(void);
 // Application Callbacks (WEAK is optional)
 //--------------------------------------------------------------------+
 
+// Invoked when received GET_STRING_DESC request
+// max_char is CFG_TUD_ENDOINT0_SIZE/2 -1, typically max_char = 31 if Endpoint0 size is 64
+// Return number of characters. Note usb string is in 16-bits unicode format
+uint8_t tud_descriptor_string_cb(uint8_t index, uint16_t* desc, uint8_t max_char);
+
 // Invoked when device is mounted (configured)
 ATTR_WEAK void tud_mount_cb(void);
 

+ 1 - 1
src/device/usbd_control.c

@@ -93,7 +93,7 @@ bool usbd_control_xfer(uint8_t rhport, tusb_control_request_t const * request, v
   _control_state.total_len = tu_min16(len, request->wLength);
   _control_state.total_transferred = 0;
 
-  if ( buffer != NULL && len )
+  if ( (buffer != NULL) && len )
   {
     // Data stage
     TU_ASSERT( start_control_data_xact(rhport) );