From f3a954e7deaa33db5fed8e807cd146e65aaeb104 Mon Sep 17 00:00:00 2001 From: hathach Date: Sat, 30 Mar 2019 17:38:00 +0700 Subject: [PATCH] self powered and remote wakeup support are from configuration descriptor attribute --- src/common/tusb_types.h | 3 +-- src/device/usbd.c | 44 +++++++++++++++++-------------------- src/device/usbd.h | 3 --- src/device/usbd_auto_desc.c | 2 +- 4 files changed, 22 insertions(+), 30 deletions(-) diff --git a/src/common/tusb_types.h b/src/common/tusb_types.h index 5e4da898..3a4ddb4c 100644 --- a/src/common/tusb_types.h +++ b/src/common/tusb_types.h @@ -166,8 +166,7 @@ typedef enum enum { TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP = TU_BIT(5), - TUSB_DESC_CONFIG_ATT_SELF_POWER = TU_BIT(6), - TUSB_DESC_CONFIG_ATT_BUS_POWER = TU_BIT(7) + TUSB_DESC_CONFIG_ATT_SELF_POWERED = TU_BIT(6), }; #define TUSB_DESC_CONFIG_POWER_MA(x) ((x)/2) diff --git a/src/device/usbd.c b/src/device/usbd.c index dde98939..fb624952 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -42,12 +42,13 @@ typedef struct { struct ATTR_PACKED { - volatile uint8_t connected : 1; - volatile uint8_t configured : 1; - volatile uint8_t suspended : 1; + volatile uint8_t connected : 1; + volatile uint8_t configured : 1; + volatile uint8_t suspended : 1; - uint8_t remote_wakeup_en : 1; - uint8_t self_powered : 1; + uint8_t remote_wakeup_en : 1; // enable/disable by host + uint8_t remote_wakeup_support : 1; // configuration descriptor's attribute + uint8_t self_powered : 1; // configuration descriptor's attribute }; // uint8_t ep_busy_mask[2]; // bit mask for busy endpoint @@ -181,17 +182,12 @@ bool tud_mounted(void) return _usbd_dev.configured; } -void tud_set_self_powered(bool self_powered) -{ - _usbd_dev.self_powered = self_powered; -} - bool tud_remote_wakeup(void) { - // only wake up host if this feature is enabled - if (_usbd_dev.suspended && _usbd_dev.remote_wakeup_en ) dcd_remote_wakeup(TUD_OPT_RHPORT); - - return _usbd_dev.remote_wakeup_en; + // only wake up host if this feature is supported and enabled and we are suspended + TU_VERIFY (_usbd_dev.suspended && _usbd_dev.remote_wakeup_support && _usbd_dev.remote_wakeup_en ); + dcd_remote_wakeup(TUD_OPT_RHPORT); + return true; } //--------------------------------------------------------------------+ @@ -217,13 +213,8 @@ bool usbd_init (void) static void usbd_reset(uint8_t rhport) { - // self_powered bit is set by application and unchanged - bool self_powered = _usbd_dev.self_powered; - tu_varclr(&_usbd_dev); - _usbd_dev.self_powered = self_powered; - memset(_usbd_dev.itf2drv, 0xff, sizeof(_usbd_dev.itf2drv)); // invalid mapping memset(_usbd_dev.ep2drv , 0xff, sizeof(_usbd_dev.ep2drv )); // invalid mapping @@ -499,13 +490,18 @@ 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) { - uint8_t const * desc_cfg = (uint8_t const *) usbd_desc_set->config; - TU_ASSERT(desc_cfg != NULL); + tusb_desc_configuration_t const * desc_cfg = (tusb_desc_configuration_t const *) usbd_desc_set->config; + TU_ASSERT(desc_cfg != NULL && desc_cfg->bDescriptorType == TUSB_DESC_CONFIGURATION); - uint8_t const * p_desc = desc_cfg + sizeof(tusb_desc_configuration_t); - uint16_t const cfg_len = ((tusb_desc_configuration_t*)desc_cfg)->wTotalLength; + // Parse configuration descriptor + _usbd_dev.remote_wakeup_support = (desc_cfg->bmAttributes & TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP) ? 1 : 0; + _usbd_dev.self_powered = (desc_cfg->bmAttributes & TUSB_DESC_CONFIG_ATT_SELF_POWERED) ? 1 : 0; - while( p_desc < desc_cfg + cfg_len ) + // Parse interface descriptor + uint8_t const * p_desc = ((uint8_t const*) desc_cfg) + sizeof(tusb_desc_configuration_t); + uint8_t const * desc_end = ((uint8_t const*) desc_cfg) + desc_cfg->wTotalLength; + + while( p_desc < desc_end ) { // Each interface always starts with Interface or Association descriptor if ( TUSB_DESC_INTERFACE_ASSOCIATION == tu_desc_type(p_desc) ) diff --git a/src/device/usbd.h b/src/device/usbd.h index 2365d645..d9b9cb8a 100644 --- a/src/device/usbd.h +++ b/src/device/usbd.h @@ -66,9 +66,6 @@ void tud_task (void); // Check if device is connected and configured bool tud_mounted(void); -// Tell stack that device is self or bus powered (default = bus) -void tud_set_self_powered(bool self_powered); - // Remote wake up host, only if suspended and enabled by host bool tud_remote_wakeup(void); diff --git a/src/device/usbd_auto_desc.c b/src/device/usbd_auto_desc.c index 8272ea5a..e418cd23 100644 --- a/src/device/usbd_auto_desc.c +++ b/src/device/usbd_auto_desc.c @@ -342,7 +342,7 @@ desc_auto_cfg_t const _desc_auto_config_struct = .bConfigurationValue = 1, .iConfiguration = 0x00, - .bmAttributes = TUSB_DESC_CONFIG_ATT_BUS_POWER, + .bmAttributes = TU_BIT(7) | TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, .bMaxPower = TUSB_DESC_CONFIG_POWER_MA(100) },