From ef52ae37d3d2fbe533f44a30d5145134c0f0ed00 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Mon, 30 May 2022 12:20:42 +0200 Subject: [PATCH] Reorganizing usb layer. --- CMakeLists.txt | 11 +- src/usb/usb.c | 211 +------------------------------------- src/usb/usb_descriptors.c | 28 ++++- src/usb/usb_descriptors.h | 31 ++++-- 4 files changed, 60 insertions(+), 221 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 39ecce6..d564061 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,6 +44,8 @@ set_source_files_properties( target_sources(pico_ccid PUBLIC ${CMAKE_CURRENT_LIST_DIR}/src/usb/usb.c + ${CMAKE_CURRENT_LIST_DIR}/src/usb/usb_descriptors.c + ${CMAKE_CURRENT_LIST_DIR}/src/ccid/ccid2040.c ${CMAKE_CURRENT_LIST_DIR}/src/fs/file.c ${CMAKE_CURRENT_LIST_DIR}/src/fs/flash.c ${CMAKE_CURRENT_LIST_DIR}/src/fs/low_flash.c @@ -61,9 +63,14 @@ target_include_directories(pico_ccid PUBLIC ${CMAKE_CURRENT_LIST_DIR}/mbedtls/include ${CMAKE_CURRENT_LIST_DIR}/mbedtls/library ) - + +target_compile_options(pico_ccid PUBLIC + -Wall + -Werror +) + pico_add_extra_outputs(pico_ccid) #target_compile_definitions(pico_ccid PRIVATE MBEDTLS_ECDSA_DETERMINISTIC=1) -target_link_libraries(pico_ccid PRIVATE pico_stdlib pico_multicore hardware_flash hardware_sync hardware_adc pico_unique_id hardware_rtc) +target_link_libraries(pico_ccid PRIVATE pico_stdlib pico_multicore hardware_flash hardware_sync hardware_adc pico_unique_id hardware_rtc tinyusb_device tinyusb_board) diff --git a/src/usb/usb.c b/src/usb/usb.c index d6a579a..7d7bc90 100644 --- a/src/usb/usb.c +++ b/src/usb/usb.c @@ -25,6 +25,7 @@ #include "pico/stdlib.h" #include "tusb.h" #include "device/usbd_pvt.h" +#include "usb_descriptors.h" // For memcpy #include @@ -122,215 +123,6 @@ void tud_vendor_tx_cb(uint8_t itf, uint32_t sent_bytes) { #define MAX_USB_POWER 1 -struct ccid_class_descriptor { - uint8_t bLength; - uint8_t bDescriptorType; - uint16_t bcdCCID; - uint8_t bMaxSlotIndex; - uint8_t bVoltageSupport; - uint32_t dwProtocols; - uint32_t dwDefaultClock; - uint32_t dwMaximumClock; - uint8_t bNumClockSupport; - uint32_t dwDataRate; - uint32_t dwMaxDataRate; - uint8_t bNumDataRatesSupported; - uint32_t dwMaxIFSD; - uint32_t dwSynchProtocols; - uint32_t dwMechanical; - uint32_t dwFeatures; - uint32_t dwMaxCCIDMessageLength; - uint8_t bClassGetResponse; - uint8_t bclassEnvelope; - uint16_t wLcdLayout; - uint8_t bPINSupport; - uint8_t bMaxCCIDBusySlots; -} __attribute__ ((__packed__)); - -static const struct ccid_class_descriptor desc_ccid = { - .bLength = sizeof(struct ccid_class_descriptor), - .bDescriptorType = 0x21, - .bcdCCID = (0x0110), - .bMaxSlotIndex = 0, - .bVoltageSupport = 0x01, // 5.0V - .dwProtocols = ( - 0x01| // T=0 - 0x02), // T=1 - .dwDefaultClock = (0xDFC), - .dwMaximumClock = (0xDFC), - .bNumClockSupport = 0, - .dwDataRate = (0x2580), - .dwMaxDataRate = (0x2580), - .bNumDataRatesSupported = 0, - .dwMaxIFSD = (0xFE), // IFSD is handled by the real reader driver - .dwSynchProtocols = (0), - .dwMechanical = (0), - .dwFeatures = 0x40840, //USB-ICC, short & extended APDU - .dwMaxCCIDMessageLength = 65544+10, - .bClassGetResponse = 0xFF, - .bclassEnvelope = 0xFF, - .wLcdLayout = 0x0, - .bPINSupport = 0x0, - .bMaxCCIDBusySlots = 0x01, -}; - -//--------------------------------------------------------------------+ -// Device Descriptors -//--------------------------------------------------------------------+ -tusb_desc_device_t const desc_device = -{ - .bLength = sizeof(tusb_desc_device_t), - .bDescriptorType = TUSB_DESC_DEVICE, - .bcdUSB = (USB_BCD), - - .bDeviceClass = 0x00, - .bDeviceSubClass = 0, - .bDeviceProtocol = 0, - .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, - - .idVendor = (USB_VID), - .idProduct = (USB_PID), - .bcdDevice = CCID_VERSION, - - .iManufacturer = 1, - .iProduct = 2, - .iSerialNumber = 3, - - .bNumConfigurations = 1 -}; - -uint8_t const * tud_descriptor_device_cb(void) -{ - return (uint8_t const *) &desc_device; -} - -tusb_desc_interface_t const desc_interface = -{ - .bLength = sizeof(tusb_desc_interface_t), - .bDescriptorType = TUSB_DESC_INTERFACE, - .bInterfaceNumber = 0, - .bAlternateSetting = 0, - .bNumEndpoints = 2, - .bInterfaceClass = TUSB_CLASS_SMART_CARD, - .bInterfaceSubClass = 0, - .bInterfaceProtocol = 0, - .iInterface = 5, -}; - -//--------------------------------------------------------------------+ -// Configuration Descriptor -//--------------------------------------------------------------------+ - -tusb_desc_configuration_t const desc_config = -{ - .bLength = sizeof(tusb_desc_configuration_t), - .bDescriptorType = TUSB_DESC_CONFIGURATION, - .wTotalLength = (sizeof(tusb_desc_configuration_t) + sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) + 2*sizeof(tusb_desc_endpoint_t)), - .bNumInterfaces = 1, - .bConfigurationValue = 1, - .iConfiguration = 4, - .bmAttributes = USB_CONFIG_ATT_ONE | TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, - .bMaxPower = TUSB_DESC_CONFIG_POWER_MA(MAX_USB_POWER+1), -}; - -tusb_desc_endpoint_t const desc_ep1 = -{ - .bLength = sizeof(tusb_desc_endpoint_t), - .bDescriptorType = TUSB_DESC_ENDPOINT, - .bEndpointAddress = TUSB_DIR_IN_MASK | 1, - .bmAttributes.xfer = TUSB_XFER_BULK, - .wMaxPacketSize.size = (64), - .bInterval = 0 -}; - -tusb_desc_endpoint_t const desc_ep2 = -{ - .bLength = sizeof(tusb_desc_endpoint_t), - .bDescriptorType = TUSB_DESC_ENDPOINT, - .bEndpointAddress = 2, - .bmAttributes.xfer = TUSB_XFER_BULK, - .wMaxPacketSize.size = (64), - .bInterval = 0 -}; - -static uint8_t desc_config_extended[sizeof(tusb_desc_configuration_t) + sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) + 2*sizeof(tusb_desc_endpoint_t)]; - -uint8_t const * tud_descriptor_configuration_cb(uint8_t index) -{ - (void) index; // for multiple configurations - - static uint8_t initd = 0; - if (initd == 0) - { - uint8_t *p = desc_config_extended; - memcpy(p, &desc_config, sizeof(tusb_desc_configuration_t)); p += sizeof(tusb_desc_configuration_t); - memcpy(p, &desc_interface, sizeof(tusb_desc_interface_t)); p += sizeof(tusb_desc_interface_t); - memcpy(p, &desc_ccid, sizeof(struct ccid_class_descriptor)); p += sizeof(struct ccid_class_descriptor); - memcpy(p, &desc_ep1, sizeof(tusb_desc_endpoint_t)); p += sizeof(tusb_desc_endpoint_t); - memcpy(p, &desc_ep2, sizeof(tusb_desc_endpoint_t)); p += sizeof(tusb_desc_endpoint_t); - initd = 1; - } - return (const uint8_t *)desc_config_extended; -} - -//--------------------------------------------------------------------+ -// String Descriptors -//--------------------------------------------------------------------+ - -// array of pointer to string descriptors -char const* string_desc_arr [] = -{ - (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409) - "Pol Henarejos", // 1: Manufacturer - "Pico HSM", // 2: Product - "11223344", // 3: Serials, should use chip ID - "Pico HSM Config", // 4: Vendor Interface - "Pico HSM Interface" -}; - -static uint16_t _desc_str[32]; - -uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid) -{ - (void) langid; - - uint8_t chr_count; - - if (index == 0) { - memcpy(&_desc_str[1], string_desc_arr[0], 2); - chr_count = 1; - } - else { - // Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors. - // https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors - - if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) - return NULL; - - const char* str = string_desc_arr[index]; - char unique_id_str[2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1]; - if (index == 3) { - pico_unique_board_id_t unique_id; - pico_get_unique_board_id(&unique_id); - pico_get_unique_board_id_string(unique_id_str, 2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1); - str = unique_id_str; - } - - chr_count = strlen(str); - if ( chr_count > 31 ) - chr_count = 31; - - // Convert ASCII string into UTF-16 - for(uint8_t i=0; i= 2 .name = "CCID", diff --git a/src/usb/usb_descriptors.c b/src/usb/usb_descriptors.c index e42a712..98dc728 100644 --- a/src/usb/usb_descriptors.c +++ b/src/usb/usb_descriptors.c @@ -17,11 +17,9 @@ #include "tusb.h" #include "usb_descriptors.h" -#include "ccid.h" #include "pico/unique_id.h" #include "ccid_version.h" - #ifndef USB_VID #define USB_VID 0xFEFF #endif @@ -35,6 +33,32 @@ #define MAX_USB_POWER 1 +static const struct ccid_class_descriptor desc_ccid = { + .bLength = sizeof(struct ccid_class_descriptor), + .bDescriptorType = 0x21, + .bcdCCID = (0x0110), + .bMaxSlotIndex = 0, + .bVoltageSupport = 0x01, // 5.0V + .dwProtocols = ( + 0x01| // T=0 + 0x02), // T=1 + .dwDefaultClock = (0xDFC), + .dwMaximumClock = (0xDFC), + .bNumClockSupport = 0, + .dwDataRate = (0x2580), + .dwMaxDataRate = (0x2580), + .bNumDataRatesSupported = 0, + .dwMaxIFSD = (0xFE), // IFSD is handled by the real reader driver + .dwSynchProtocols = (0), + .dwMechanical = (0), + .dwFeatures = 0x40840, //USB-ICC, short & extended APDU + .dwMaxCCIDMessageLength = 65544+10, + .bClassGetResponse = 0xFF, + .bclassEnvelope = 0xFF, + .wLcdLayout = 0x0, + .bPINSupport = 0x0, + .bMaxCCIDBusySlots = 0x01, +}; //--------------------------------------------------------------------+ // Device Descriptors diff --git a/src/usb/usb_descriptors.h b/src/usb/usb_descriptors.h index 4026a04..ac36e06 100644 --- a/src/usb/usb_descriptors.h +++ b/src/usb/usb_descriptors.h @@ -18,12 +18,29 @@ #ifndef USB_DESCRIPTORS_H_ #define USB_DESCRIPTORS_H_ -enum -{ - VENDOR_REQUEST_WEBUSB = 1, - VENDOR_REQUEST_MICROSOFT = 2 -}; - -extern uint8_t const desc_ms_os_20[]; +struct ccid_class_descriptor { + uint8_t bLength; + uint8_t bDescriptorType; + uint16_t bcdCCID; + uint8_t bMaxSlotIndex; + uint8_t bVoltageSupport; + uint32_t dwProtocols; + uint32_t dwDefaultClock; + uint32_t dwMaximumClock; + uint8_t bNumClockSupport; + uint32_t dwDataRate; + uint32_t dwMaxDataRate; + uint8_t bNumDataRatesSupported; + uint32_t dwMaxIFSD; + uint32_t dwSynchProtocols; + uint32_t dwMechanical; + uint32_t dwFeatures; + uint32_t dwMaxCCIDMessageLength; + uint8_t bClassGetResponse; + uint8_t bclassEnvelope; + uint16_t wLcdLayout; + uint8_t bPINSupport; + uint8_t bMaxCCIDBusySlots; +} __attribute__ ((__packed__)); #endif /* USB_DESCRIPTORS_H_ */