From 2781e80f624c9785621e6fdbe12332d18b9f76ae Mon Sep 17 00:00:00 2001 From: z4yx Date: Sun, 29 Oct 2023 10:30:42 +0800 Subject: [PATCH] reuse ccid buffer for CfgDesc --- include/apdu.h | 1 + interfaces/USB/device/usb_device.c | 2 +- interfaces/USB/device/usbd_desc.c | 23 +++++++++++++++-------- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/include/apdu.h b/include/apdu.h index 1846decc..7ea68260 100644 --- a/include/apdu.h +++ b/include/apdu.h @@ -83,6 +83,7 @@ enum { BUFFER_OWNER_NONE = 1, BUFFER_OWNER_CCID, BUFFER_OWNER_WEBUSB, + BUFFER_OWNER_USBD, // store the configuration descriptor during a control transfer }; void init_apdu_buffer(void); // implement in ccid.c for reusing the ccid buffer diff --git a/interfaces/USB/device/usb_device.c b/interfaces/USB/device/usb_device.c index e15e5b09..be4b9db5 100644 --- a/interfaces/USB/device/usb_device.c +++ b/interfaces/USB/device/usb_device.c @@ -11,7 +11,7 @@ EP_SIZE_TABLE_t EP_SIZE_TABLE; void usb_device_init(void) { usb_resources_alloc(); - USBD_DescriptorInit(); + // USBD_DescriptorInit(); USBD_Init(&usb_device, &usbdDescriptors, 0); USBD_RegisterClass(&usb_device, &USBD_CANOKEY); USBD_Start(&usb_device); diff --git a/interfaces/USB/device/usbd_desc.c b/interfaces/USB/device/usbd_desc.c index cdab857b..57eef3a9 100644 --- a/interfaces/USB/device/usbd_desc.c +++ b/interfaces/USB/device/usbd_desc.c @@ -7,6 +7,7 @@ #include #include #include +#include #define USBD_LANGID_STRING 0x0409 #define USBD_MANUFACTURER_STRING "canokeys.org" @@ -213,11 +214,7 @@ static const uint8_t USBD_FS_IfDesc_CCID[] = { 0x00, /* bInterval: Polling Interval */ }; -static uint8_t USBD_FS_CfgDesc[USB_LEN_CFG_DESC + - sizeof(USBD_FS_IfDesc_CCID) + - sizeof(USBD_FS_IfDesc_WEBUSB) + - sizeof(USBD_FS_IfDesc_KBDHID) + - sizeof(USBD_FS_IfDesc_CTAPHID)] = { +static const uint8_t USBD_FS_CfgDescHeader[USB_LEN_CFG_DESC] = { USB_LEN_CFG_DESC, /* bLength: Configuration Descriptor size */ USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ 0x00, 0x00, /* wTotalLength: To be filled by program */ @@ -343,9 +340,13 @@ static void patch_interface_descriptor(uint8_t *desc, uint8_t *desc_end, uint8_t } void USBD_DescriptorInit(void) { - uint8_t *desc = USBD_FS_CfgDesc + USB_LEN_CFG_DESC; + uint8_t *USBD_FS_CfgDesc = global_buffer; + uint8_t *desc = USBD_FS_CfgDesc; uint8_t nIface = 3; + memcpy(desc, USBD_FS_CfgDescHeader, USB_LEN_CFG_DESC); + desc += USB_LEN_CFG_DESC; + memcpy(desc, USBD_FS_IfDesc_CTAPHID, sizeof(USBD_FS_IfDesc_CTAPHID)); patch_interface_descriptor(desc, desc + sizeof(USBD_FS_IfDesc_CTAPHID), USBD_CANOKEY_CTAPHID_IF, EP_IN(ctap_hid), EP_OUT(ctap_hid), EP_SIZE(ctap_hid)); @@ -382,8 +383,13 @@ const uint8_t *USBD_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) } const uint8_t *USBD_ConfigurationDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { - *length = sizeof(USBD_FS_CfgDesc); - return USBD_FS_CfgDesc; + USBD_DescriptorInit(); + *length = USB_LEN_CFG_DESC + + sizeof(USBD_FS_IfDesc_CCID) + + sizeof(USBD_FS_IfDesc_WEBUSB) + + sizeof(USBD_FS_IfDesc_KBDHID) + + sizeof(USBD_FS_IfDesc_CTAPHID); + return global_buffer; } const uint8_t *USBD_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { @@ -411,6 +417,7 @@ const uint8_t *USBD_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *lengt } const uint8_t *USBD_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { + static_assert(sizeof(USBD_FS_BOSDesc) <= USBD_MAX_STR_DESC_SIZ); *length = sizeof(USBD_FS_BOSDesc); memcpy(USBD_StrDesc, USBD_FS_BOSDesc, sizeof(USBD_FS_BOSDesc)); // use USBD_StrDesc to store this descriptor USBD_StrDesc[28] = cfg_is_webusb_landing_enable();