diff options
| author | hathach <[email protected]> | 2019-05-12 15:54:11 +0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2019-05-12 15:54:11 +0700 |
| commit | aaf5848ce15c5d6ccf4c51c3ef7b04fb787639ee (patch) | |
| tree | 2a04ff594ea42234c532fc4ab5f8e6f0b4603623 | |
| parent | bfa073818c83266c2353ccf4fce580eef62e4a0c (diff) | |
| parent | 1174949308bb7595e99fd5789ebf3466cf2ab123 (diff) | |
Merge pull request #64 from hathach/develop
add callback for descriptors device/configuration/string/hid
| -rw-r--r-- | examples/device/cdc_msc_hid/src/usb_descriptors.c | 110 | ||||
| -rw-r--r-- | examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c | 110 | ||||
| -rw-r--r-- | examples/device/hid_generic_inout/src/usb_descriptors.c | 79 | ||||
| -rw-r--r-- | examples/device/msc_dual_lun/src/usb_descriptors.c | 71 | ||||
| -rw-r--r-- | src/class/hid/hid_device.c | 3 | ||||
| -rw-r--r-- | src/class/hid/hid_device.h | 4 | ||||
| -rw-r--r-- | src/common/tusb_common.h | 5 | ||||
| -rw-r--r-- | src/common/tusb_types.h | 4 | ||||
| -rw-r--r-- | src/device/usbd.c | 53 | ||||
| -rw-r--r-- | src/device/usbd.h | 27 | ||||
| -rw-r--r-- | src/device/usbd_control.c | 4 |
11 files changed, 303 insertions, 167 deletions
diff --git a/examples/device/cdc_msc_hid/src/usb_descriptors.c b/examples/device/cdc_msc_hid/src/usb_descriptors.c index ef3cc9cef..73c8e7ce1 100644 --- a/examples/device/cdc_msc_hid/src/usb_descriptors.c +++ b/examples/device/cdc_msc_hid/src/usb_descriptors.c @@ -79,25 +79,34 @@ uint8_t const desc_hid_report[] = TUD_HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(REPORT_ID_KEYBOARD), ), TUD_HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(REPORT_ID_MOUSE), ) }; + +// Invoked when received GET HID REPORT DESCRIPTOR +// Application return pointer to descriptor +// Descriptor contents must exist long enough for transfer to complete +uint8_t const * tud_hid_descriptor_report_cb(void) +{ + return desc_hid_report; +} + #endif //------------- Configuration Descriptor -------------// enum { - #if CFG_TUD_CDC - ITF_NUM_CDC = 0, - ITF_NUM_CDC_DATA, - #endif +#if CFG_TUD_CDC + ITF_NUM_CDC = 0, + ITF_NUM_CDC_DATA, +#endif - #if CFG_TUD_MSC - ITF_NUM_MSC, - #endif +#if CFG_TUD_MSC + ITF_NUM_MSC, +#endif - #if CFG_TUD_HID - ITF_NUM_HID, - #endif +#if CFG_TUD_HID + ITF_NUM_HID, +#endif - ITF_NUM_TOTAL + ITF_NUM_TOTAL }; enum @@ -136,42 +145,65 @@ uint8_t const desc_configuration[] = #endif }; +// Invoked when received GET DEVICE DESCRIPTOR +// Application return pointer to descriptor +uint8_t const * tud_descriptor_device_cb(void) +{ + return (uint8_t const *) &desc_device; +} + +// Invoked when received GET CONFIGURATION DESCRIPTOR +// Application return pointer to descriptor +// Descriptor contents must exist long enough for transfer to complete +uint8_t const * tud_descriptor_configuration_cb(void) +{ + return desc_configuration; +} + //------------- String Descriptors -------------// + // array of pointer to string descriptors -uint16_t const * const string_desc_arr [] = +char 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'), + (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 +}; - // 2: Product - TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', ' ', 'd', 'e', 'v', 'i', 'c', 'e'), +static uint16_t _desc_str[32]; - // 3: Serials, should use chip ID - TUD_DESC_STRCONV('1', '2', '3', '4', '5', '6'), +// Invoked when received GET STRING DESCRIPTOR request +// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete +uint16_t const* tud_descriptor_string_cb(uint8_t index) +{ + uint8_t chr_count; - // 4: CDC Interface - TUD_DESC_STRCONV('t','u','s','b',' ','c','d','c'), + if ( index == 0) + { + memcpy(&_desc_str[1], string_desc_arr[0], 2); + chr_count = 1; + }else + { + if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL; - // 5: MSC Interface - TUD_DESC_STRCONV('t','u','s','b',' ','m','s','c'), + const char* str = string_desc_arr[index]; - // 6: HID - TUD_DESC_STRCONV('t','u','s','b',' ','h','i','d') -}; + // Cap at max char + chr_count = strlen(str); + if ( chr_count > 31 ) chr_count = 31; -// tud_desc_set is required by tinyusb stack -tud_desc_set_t tud_desc_set = -{ - .device = &desc_device, - .config = desc_configuration, + for(uint8_t i=0; i<chr_count; i++) + { + _desc_str[1+i] = str[i]; + } + } - .string_arr = (uint8_t const **) string_desc_arr, - .string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]), + // first byte is len, second byte is string type + _desc_str[0] = TUD_DESC_STR_HEADER(chr_count); -#if CFG_TUD_HID - .hid_report = desc_hid_report, -#endif -}; + return _desc_str; +} diff --git a/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c b/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c index 1faaaed66..92ce8c6ec 100644 --- a/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c +++ b/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c @@ -79,25 +79,34 @@ uint8_t const desc_hid_report[] = TUD_HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(REPORT_ID_KEYBOARD), ), TUD_HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(REPORT_ID_MOUSE), ) }; + +// Invoked when received GET HID REPORT DESCRIPTOR +// Application return pointer to descriptor +// Descriptor contents must exist long enough for transfer to complete +uint8_t const * tud_hid_descriptor_report_cb(void) +{ + return desc_hid_report; +} + #endif //------------- Configuration Descriptor -------------// enum { - #if CFG_TUD_CDC - ITF_NUM_CDC = 0, - ITF_NUM_CDC_DATA, - #endif +#if CFG_TUD_CDC + ITF_NUM_CDC = 0, + ITF_NUM_CDC_DATA, +#endif - #if CFG_TUD_MSC - ITF_NUM_MSC, - #endif +#if CFG_TUD_MSC + ITF_NUM_MSC, +#endif - #if CFG_TUD_HID - ITF_NUM_HID, - #endif +#if CFG_TUD_HID + ITF_NUM_HID, +#endif - ITF_NUM_TOTAL + ITF_NUM_TOTAL }; enum @@ -136,42 +145,65 @@ uint8_t const desc_configuration[] = #endif }; +// Invoked when received GET DEVICE DESCRIPTOR +// Application return pointer to descriptor +uint8_t const * tud_descriptor_device_cb(void) +{ + return (uint8_t const *) &desc_device; +} + +// Invoked when received GET CONFIGURATION DESCRIPTOR +// Application return pointer to descriptor +// Descriptor contents must exist long enough for transfer to complete +uint8_t const * tud_descriptor_configuration_cb(void) +{ + return desc_configuration; +} + //------------- String Descriptors -------------// + // array of pointer to string descriptors -uint16_t const * const string_desc_arr [] = +char 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'), + (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 +}; - // 2: Product - TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', ' ', 'd', 'e', 'v', 'i', 'c', 'e'), +static uint16_t _desc_str[32]; - // 3: Serials, should use chip ID - TUD_DESC_STRCONV('1', '2', '3', '4', '5', '6'), +// Invoked when received GET STRING DESCRIPTOR request +// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete +uint16_t const* tud_descriptor_string_cb(uint8_t index) +{ + uint8_t chr_count; - // 4: CDC Interface - TUD_DESC_STRCONV('t','u','s','b',' ','c','d','c'), + if ( index == 0) + { + memcpy(&_desc_str[1], string_desc_arr[0], 2); + chr_count = 1; + }else + { + if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL; - // 5: MSC Interface - TUD_DESC_STRCONV('t','u','s','b',' ','m','s','c'), + const char* str = string_desc_arr[index]; - // 6: HID - TUD_DESC_STRCONV('t','u','s','b',' ','h','i','d') -}; + // Cap at max char + chr_count = strlen(str); + if ( chr_count > 31 ) chr_count = 31; -// tud_desc_set is required by tinyusb stack -tud_desc_set_t tud_desc_set = -{ - .device = &desc_device, - .config = desc_configuration, + for(uint8_t i=0; i<chr_count; i++) + { + _desc_str[1+i] = str[i]; + } + } - .string_arr = (uint8_t const **) string_desc_arr, - .string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]), + // first byte is len, second byte is string type + _desc_str[0] = TUD_DESC_STR_HEADER(chr_count); -#if CFG_TUD_HID - .hid_report = desc_hid_report, -#endif -}; + return _desc_str; +} diff --git a/examples/device/hid_generic_inout/src/usb_descriptors.c b/examples/device/hid_generic_inout/src/usb_descriptors.c index 231eb4fa6..fb4dd378c 100644 --- a/examples/device/hid_generic_inout/src/usb_descriptors.c +++ b/examples/device/hid_generic_inout/src/usb_descriptors.c @@ -88,30 +88,71 @@ 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 [] = + +// Invoked when received GET DEVICE DESCRIPTOR +// Application return pointer to descriptor +uint8_t const * tud_descriptor_device_cb(void) +{ + return (uint8_t const *) &desc_device; +} + +// Invoked when received GET CONFIGURATION DESCRIPTOR +// Application return pointer to descriptor +// Descriptor contents must exist long enough for transfer to complete +uint8_t const * tud_descriptor_configuration_cb(void) { - // 0: is supported language = English - TUD_DESC_STRCONV(0x0409), + return desc_configuration; +} - // 1: Manufacturer - TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', '.', 'o', 'r', 'g'), +// Invoked when received GET HID REPORT DESCRIPTOR +// Application return pointer to descriptor +// Descriptor contents must exist long enough for transfer to complete +uint8_t const * tud_hid_descriptor_report_cb(void) +{ + return desc_hid_report; +} - // 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 = +static uint16_t _desc_str[32]; + +// Invoked when received GET STRING DESCRIPTOR request +// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete +uint16_t const* tud_descriptor_string_cb(uint8_t index) { - .device = &desc_device, - .config = desc_configuration, + uint8_t chr_count; - .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_str[1], string_desc_arr[0], 2); + chr_count = 1; + }else + { + if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL; + + const char* str = string_desc_arr[index]; + + // Cap at max char + chr_count = strlen(str); + if ( chr_count > 31 ) chr_count = 31; + + for(uint8_t i=0; i<chr_count; i++) + { + _desc_str[1+i] = str[i]; + } + } + + // first byte is len, second byte is string type + _desc_str[0] = TUD_DESC_STR_HEADER(chr_count); + + return _desc_str; +} diff --git a/examples/device/msc_dual_lun/src/usb_descriptors.c b/examples/device/msc_dual_lun/src/usb_descriptors.c index af2b1a091..1ae000fd9 100644 --- a/examples/device/msc_dual_lun/src/usb_descriptors.c +++ b/examples/device/msc_dual_lun/src/usb_descriptors.c @@ -82,29 +82,62 @@ 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 [] = +// Invoked when received GET DEVICE DESCRIPTOR +// Application return pointer to descriptor +uint8_t const * tud_descriptor_device_cb(void) { - // 0: is supported language = English - TUD_DESC_STRCONV(0x0409), + return (uint8_t const *) &desc_device; +} - // 1: Manufacturer - TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', '.', 'o', 'r', 'g'), +// Invoked when received GET CONFIGURATION DESCRIPTOR +// Application return pointer to descriptor +// Descriptor contents must exist long enough for transfer to complete +uint8_t const * tud_descriptor_configuration_cb(void) +{ + return desc_configuration; +} - // 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 = +static uint16_t _desc_str[32]; + +// Invoked when received GET STRING DESCRIPTOR request +// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete +uint16_t const* tud_descriptor_string_cb(uint8_t index) { - .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, -}; + uint8_t chr_count; + + if ( index == 0) + { + memcpy(&_desc_str[1], string_desc_arr[0], 2); + chr_count = 1; + }else + { + if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL; + + const char* str = string_desc_arr[index]; + + // Cap at max char + chr_count = strlen(str); + if ( chr_count > 31 ) chr_count = 31; + + for(uint8_t i=0; i<chr_count; i++) + { + _desc_str[1+i] = str[i]; + } + } + + // first byte is len, second byte is string type + _desc_str[0] = TUD_DESC_STR_HEADER(chr_count); + + return _desc_str; +} diff --git a/src/class/hid/hid_device.c b/src/class/hid/hid_device.c index e82932e1f..1c971a606 100644 --- a/src/class/hid/hid_device.c +++ b/src/class/hid/hid_device.c @@ -201,7 +201,8 @@ bool hidd_control_request(uint8_t rhport, tusb_control_request_t const * p_reque if (p_request->bRequest == TUSB_REQ_GET_DESCRIPTOR && desc_type == HID_DESC_TYPE_REPORT)
{
- usbd_control_xfer(rhport, p_request, (void*) tud_desc_set.hid_report, p_hid->reprot_desc_len);
+ uint8_t const * desc_report = tud_hid_descriptor_report_cb();
+ usbd_control_xfer(rhport, p_request, (void*) desc_report, p_hid->reprot_desc_len);
}else
{
return false; // stall unsupported request
diff --git a/src/class/hid/hid_device.h b/src/class/hid/hid_device.h index 825be2181..f838e6fad 100644 --- a/src/class/hid/hid_device.h +++ b/src/class/hid/hid_device.h @@ -68,6 +68,10 @@ bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y // Callbacks (Weak is optional)
//--------------------------------------------------------------------+
+// 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(void);
+
// 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
diff --git a/src/common/tusb_common.h b/src/common/tusb_common.h index 0b5a37922..e687b8983 100644 --- a/src/common/tusb_common.h +++ b/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);
diff --git a/src/common/tusb_types.h b/src/common/tusb_types.h index 909deffa8..82b5a65c1 100644 --- a/src/common/tusb_types.h +++ b/src/common/tusb_types.h @@ -403,10 +403,10 @@ static inline uint8_t tu_desc_len(void const* desc) }
// Length of the string descriptors in bytes with slen characters
-#define TUD_DESC_STRLEN(_slen) (2*(_slen) + 2)
+#define TUD_DESC_STRLEN(_chr_count) (2*(_chr_count) + 2)
// Header of string descriptors with len + string type
-#define TUD_DESC_STR_HEADER(_slen) ( (uint16_t) ( (TUSB_DESC_STRING << 8 ) | TUD_DESC_STRLEN(_slen)) )
+#define TUD_DESC_STR_HEADER(_chr_count) ( (uint16_t) ( (TUSB_DESC_STRING << 8 ) | TUD_DESC_STRLEN(_chr_count)) )
// Convert comma-separated string to descriptor unicode format
#define TUD_DESC_STRCONV( ... ) (const uint16_t[]) { TUD_DESC_STR_HEADER(VA_ARGS_NUM_(__VA_ARGS__)), __VA_ARGS__ }
diff --git a/src/device/usbd.c b/src/device/usbd.c index 434a2c3d8..1eda970a2 100644 --- a/src/device/usbd.c +++ b/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:
@@ -485,7 +479,7 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const // This function parse configuration descriptor & open drivers accordingly
static bool process_set_config(uint8_t rhport)
{
- tusb_desc_configuration_t const * desc_cfg = (tusb_desc_configuration_t const *) tud_desc_set.config;
+ tusb_desc_configuration_t const * desc_cfg = (tusb_desc_configuration_t const *) tud_descriptor_configuration_cb();
TU_ASSERT(desc_cfg != NULL && desc_cfg->bDescriptorType == TUSB_DESC_CONFIGURATION);
// Parse configuration descriptor
@@ -556,56 +550,51 @@ 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_descriptor_device_cb(), 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;
+ {
+ tusb_desc_configuration_t const* desc_config = (tusb_desc_configuration_t const*) tud_descriptor_configuration_cb();
+ return usbd_control_xfer(rhport, p_request, (void*) desc_config, desc_config->wTotalLength);
+ }
break;
case TUSB_DESC_STRING:
// String Descriptor always uses the desc set from user
- if ( desc_index < tud_desc_set.string_count )
+ if ( desc_index == 0xEE )
{
- 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
- {
- // 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
+ {
+ uint8_t const* desc_str = (uint8_t const*) tud_descriptor_string_cb(desc_index);
+ TU_ASSERT(desc_str);
+
+ // first byte of descriptor is its size
+ return usbd_control_xfer(rhport, p_request, (void*) desc_str, desc_str[0]);
}
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;
}
//--------------------------------------------------------------------+
diff --git a/src/device/usbd.h b/src/device/usbd.h index 8debbf535..8193c4ce0 100644 --- a/src/device/usbd.h +++ b/src/device/usbd.h @@ -37,21 +37,6 @@ #include "common/tusb_common.h"
#include "device/dcd.h"
-/// \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;
-
- uint8_t const* hid_report;
-
-}tud_desc_set_t;
-
-// Descriptor collection set, must be defined by application
-extern tud_desc_set_t tud_desc_set;
-
//--------------------------------------------------------------------+
// Application API
//--------------------------------------------------------------------+
@@ -78,6 +63,18 @@ bool tud_remote_wakeup(void); // Application Callbacks (WEAK is optional)
//--------------------------------------------------------------------+
+// Invoked when received GET DEVICE DESCRIPTOR request
+// Application return pointer to descriptor
+uint8_t const * tud_descriptor_device_cb(void);
+
+// Invoked when received GET CONFIGURATION DESCRIPTOR request
+// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
+uint8_t const * tud_descriptor_configuration_cb(void);
+
+// Invoked when received GET STRING DESCRIPTOR request
+// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
+uint16_t const* tud_descriptor_string_cb(uint8_t index);
+
// Invoked when device is mounted (configured)
ATTR_WEAK void tud_mount_cb(void);
diff --git a/src/device/usbd_control.c b/src/device/usbd_control.c index 8460a11b0..0b878fbcb 100644 --- a/src/device/usbd_control.c +++ b/src/device/usbd_control.c @@ -93,8 +93,10 @@ 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 ( len )
{
+ TU_ASSERT(buffer);
+
// Data stage
TU_ASSERT( start_control_data_xact(rhport) );
}else
|
