Skip to content

Commit

Permalink
reuse ccid buffer for CfgDesc
Browse files Browse the repository at this point in the history
  • Loading branch information
z4yx committed Oct 29, 2023
1 parent 8385dba commit 2781e80
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
1 change: 1 addition & 0 deletions include/apdu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion interfaces/USB/device/usb_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
23 changes: 15 additions & 8 deletions interfaces/USB/device/usbd_desc.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <usbd_ctaphid.h>
#include <usbd_desc.h>
#include <usbd_kbdhid.h>
#include <apdu.h>

#define USBD_LANGID_STRING 0x0409
#define USBD_MANUFACTURER_STRING "canokeys.org"
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 2781e80

Please sign in to comment.