Merge branch 'esp32'

This commit is contained in:
Pol Henarejos
2024-06-19 22:01:46 +02:00
29 changed files with 487 additions and 378 deletions

View File

@@ -15,28 +15,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
// Pico
#include "pico/stdlib.h"
// For memcpy
#include <string.h>
// Include descriptor struct definitions
//#include "usb_common.h"
// USB register definitions from pico-sdk
#include "hardware/regs/usb.h"
// USB hardware struct definitions from pico-sdk
#include "hardware/structs/usb.h"
// For interrupt enable and numbers
#include "hardware/irq.h"
// For resetting the USB controller
#include "hardware/resets.h"
#include "random.h"
#include "pico_keys.h"
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
#include "hardware/rtc.h"
#endif
#include "tusb.h"
#include "ccid.h"
#include "device/usbd_pvt.h"

View File

@@ -17,8 +17,10 @@
#ifndef ENABLE_EMULATION
#include "tusb.h"
#ifndef ESP_PLATFORM
#include "bsp/board.h"
#endif
#endif
#include "ctap_hid.h"
#include "pico_keys.h"
#include "pico_keys_version.h"

View File

@@ -26,8 +26,6 @@
#ifndef _TUSB_CONFIG_H_
#define _TUSB_CONFIG_H_
#include "usb.h"
#ifdef __cplusplus
extern "C" {
#endif
@@ -47,7 +45,7 @@ extern "C" {
#endif
//--------------------------------------------------------------------
// COMMON CONFIGURATION
// Common Configuration
//--------------------------------------------------------------------
// defined by compiler flags for flexibility
@@ -55,20 +53,20 @@ extern "C" {
#error CFG_TUSB_MCU must be defined
#endif
#if CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_LPC43XX || \
CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX || \
CFG_TUSB_MCU == OPT_MCU_NUC505 || CFG_TUSB_MCU == OPT_MCU_CXD56
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_HIGH_SPEED)
#else
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE
#ifndef CFG_TUSB_OS
#if CFG_TUSB_MCU == OPT_MCU_RP2040
#define CFG_TUSB_OS OPT_OS_PICO
#elif CFG_TUSB_MCU == OPT_MCU_ESP32S2 || CFG_TUSB_MCU == OPT_MCU_ESP32S3
#define CFG_TUSB_OS OPT_OS_FREERTOS
#endif
#endif
#ifndef CFG_TUSB_OS
#define CFG_TUSB_OS OPT_OS_PICO
#ifndef CFG_TUSB_RHPORT0_MODE
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_FULL_SPEED)
#endif
#ifndef CFG_TUSB_DEBUG
#define CFG_TUSB_DEBUG 1
#define CFG_TUSB_DEBUG 0
#endif
// Enable Device stack
@@ -120,6 +118,12 @@ extern "C" {
// HID buffer size Should be sufficient to hold ID (if any) + Data
#define CFG_TUD_HID_EP_BUFSIZE 64
// CDC FIFO size of TX and RX
#define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
// CDC Endpoint transfer buffer size, more is faster
#define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
#ifdef __cplusplus
}

View File

@@ -18,7 +18,7 @@
#include <stdio.h>
// Pico
#ifndef ENABLE_EMULATION
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
#include "pico/stdlib.h"
#include "pico/multicore.h"
#include "bsp/board.h"
@@ -26,6 +26,13 @@
#include "pico_keys.h"
#include "usb.h"
#include "apdu.h"
#ifndef ENABLE_EMULATION
#include "tusb.h"
#endif
#ifndef ENABLE_EMULATION
#include "tusb.h"
#endif
#ifndef ENABLE_EMULATION
#include "tusb.h"
@@ -156,7 +163,7 @@ void usb_clear_rx(uint8_t itf) {
#define USB_BCD 0x0200
#ifndef ENABLE_EMULATION
#if !defined(ENABLE_EMULATION)
queue_t usb_to_card_q;
queue_t card_to_usb_q;
#endif

View File

@@ -18,12 +18,17 @@
#ifndef _USB_H_
#define _USB_H_
#ifndef ENABLE_EMULATION
#include "pico/util/queue.h"
#else
#if defined(ENABLE_EMULATION)
#include <stdbool.h>
#elif defined(ESP_PLATFORM)
#include "esp_compat.h"
#else
#include "pico/util/queue.h"
#endif
#include "esp_compat.h"
/* USB thread */
#define EV_CARD_CHANGE 1
#define EV_TX_FINISHED 2

View File

@@ -17,7 +17,12 @@
#include "tusb.h"
#include "usb_descriptors.h"
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
#include "pico/unique_id.h"
#endif
#ifdef ESP_PLATFORM
#include "tinyusb.h"
#endif
#include "pico_keys_version.h"
#include "usb.h"
#include "pico_keys.h"
@@ -59,144 +64,33 @@ tusb_desc_device_t desc_device = {
.bNumConfigurations = 1
};
#ifndef ESP_PLATFORM
uint8_t const *tud_descriptor_device_cb(void) {
return (uint8_t const *) &desc_device;
}
#endif
//--------------------------------------------------------------------+
// 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)
#ifdef USB_ITF_CCID
+ (sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) +
3 * sizeof(tusb_desc_endpoint_t))
+ (sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) +
2 * sizeof(tusb_desc_endpoint_t))
#endif
#define TUD_INTERFACE_DESC_LEN 9
#define TUD_ENDPOINT_DESC_LEN 7
#define TUSB_SMARTCARD_LEN 54
#define TUSB_SMARTCARD_CCID_DESC_LEN (TUD_INTERFACE_DESC_LEN + TUSB_SMARTCARD_LEN + 3 * TUD_ENDPOINT_DESC_LEN)
#define TUSB_SMARTCARD_WCID_DESC_LEN (TUD_INTERFACE_DESC_LEN + TUSB_SMARTCARD_LEN + 2 * TUD_ENDPOINT_DESC_LEN)
enum {
TUSB_DESC_TOTAL_LEN = TUD_CONFIG_DESC_LEN
#ifdef USB_ITF_HID
+ TUD_HID_INOUT_DESC_LEN + TUD_HID_DESC_LEN
+ TUD_HID_INOUT_DESC_LEN + TUD_HID_DESC_LEN
#endif
),
.bNumInterfaces = ITF_TOTAL,
.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),
};
#ifdef USB_ITF_CCID
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 = (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,
};
tusb_desc_interface_t const desc_interface = {
.bLength = sizeof(tusb_desc_interface_t),
.bDescriptorType = TUSB_DESC_INTERFACE,
.bInterfaceNumber = ITF_CCID,
.bAlternateSetting = 0,
.bNumEndpoints = 3,
.bInterfaceClass = TUSB_CLASS_SMART_CARD,
.bInterfaceSubClass = 0,
.bInterfaceProtocol = 0,
.iInterface = ITF_CCID + 5,
};
tusb_desc_interface_t const desc_interface_wcid = {
.bLength = sizeof(tusb_desc_interface_t),
.bDescriptorType = TUSB_DESC_INTERFACE,
.bInterfaceNumber = ITF_WCID,
.bAlternateSetting = 0,
.bNumEndpoints = 2,
.bInterfaceClass = 0xFF,
.bInterfaceSubClass = 0,
.bInterfaceProtocol = 0,
.iInterface = ITF_WCID + 5,
};
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 = (64),
.bInterval = 0
};
tusb_desc_endpoint_t const desc_ep2 = {
.bLength = sizeof(tusb_desc_endpoint_t),
.bDescriptorType = TUSB_DESC_ENDPOINT,
.bEndpointAddress = 1,
.bmAttributes.xfer = TUSB_XFER_BULK,
.wMaxPacketSize = (64),
.bInterval = 0
};
tusb_desc_endpoint_t const desc_ep3 = {
.bLength = sizeof(tusb_desc_endpoint_t),
.bDescriptorType = TUSB_DESC_ENDPOINT,
.bEndpointAddress = TUSB_DIR_IN_MASK | 2,
.bmAttributes.xfer = TUSB_XFER_INTERRUPT,
.wMaxPacketSize = (64),
.bInterval = 0
};
tusb_desc_endpoint_t const desc_ep1_wcid = {
.bLength = sizeof(tusb_desc_endpoint_t),
.bDescriptorType = TUSB_DESC_ENDPOINT,
.bEndpointAddress = (TUSB_DIR_IN_MASK | 1) + 3,
.bmAttributes.xfer = TUSB_XFER_BULK,
.wMaxPacketSize = (64),
.bInterval = 0
};
tusb_desc_endpoint_t const desc_ep2_wcid = {
.bLength = sizeof(tusb_desc_endpoint_t),
.bDescriptorType = TUSB_DESC_ENDPOINT,
.bEndpointAddress = 1 + 3,
.bmAttributes.xfer = TUSB_XFER_BULK,
.wMaxPacketSize = (64),
.bInterval = 0
};
+ TUSB_SMARTCARD_CCID_DESC_LEN
#ifdef USB_ITF_WCID
+ TUSB_SMARTCARD_WCID_DESC_LEN
#endif
static uint8_t desc_config_extended[sizeof(tusb_desc_configuration_t)
#ifdef USB_ITF_CCID
+ (sizeof(tusb_desc_interface_t) +
sizeof(struct ccid_class_descriptor) + 3 *
sizeof(tusb_desc_endpoint_t))
+ (sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) +
2 * sizeof(tusb_desc_endpoint_t))
#endif
#ifdef USB_ITF_HID
+ TUD_HID_INOUT_DESC_LEN + TUD_HID_DESC_LEN
#endif
];
};
#ifdef USB_ITF_HID
uint8_t const desc_hid_report[] = {
@@ -205,23 +99,40 @@ uint8_t const desc_hid_report[] = {
uint8_t const desc_hid_report_kb[] = {
TUD_HID_REPORT_DESC_KEYBOARD(HID_USAGE(HID_USAGE_DESKTOP_GAMEPAD), HID_LOGICAL_MIN(0), HID_LOGICAL_MAX_N(255, 2), HID_REPORT_COUNT(8), HID_REPORT_SIZE(8), HID_FEATURE( HID_DATA | HID_VARIABLE | HID_ABSOLUTE), )
};
#define EPNUM_HID 0x03
#define EPNUM_HID 0x04
#endif
static uint8_t desc_hid[] = {
TUD_HID_INOUT_DESCRIPTOR(ITF_HID,
ITF_HID + 5,
HID_ITF_PROTOCOL_NONE,
sizeof(desc_hid_report),
EPNUM_HID,
0x80 | EPNUM_HID,
CFG_TUD_HID_EP_BUFSIZE,
10)
#ifdef USB_ITF_CCID
#ifdef USB_ITF_WCID
#define TUD_SMARTCARD_DESCRIPTOR_WEB(_itf, _strix, _epout, _epin, _epsize) \
9, TUSB_DESC_INTERFACE, _itf, 0, 2, 0xFF, 0, 0, _strix, \
54, 0x21, U16_TO_U8S_LE(0x0110), 0, 0x1, U32_TO_U8S_LE(0x2), U32_TO_U8S_LE(0xDFC), U32_TO_U8S_LE(0xDFC), 0, U32_TO_U8S_LE(0x2580), U32_TO_U8S_LE(0x2580), 0, U32_TO_U8S_LE(0xFE), U32_TO_U8S_LE(0), U32_TO_U8S_LE(0), U32_TO_U8S_LE(0x40840), U32_TO_U8S_LE(65544+10), 0xFF, 0xFF, U16_TO_U8S_LE(0x0), 0, 0x1, \
7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0, \
7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0
#endif
#define TUD_SMARTCARD_DESCRIPTOR(_itf, _strix, _epout, _epin, _epint, _epsize) \
9, TUSB_DESC_INTERFACE, _itf, 0, 3, TUSB_CLASS_SMART_CARD, 0, 0, _strix, \
54, 0x21, U16_TO_U8S_LE(0x0110), 0, 0x1, U32_TO_U8S_LE(0x2), U32_TO_U8S_LE(0xDFC), U32_TO_U8S_LE(0xDFC), 0, U32_TO_U8S_LE(0x2580), U32_TO_U8S_LE(0x2580), 0, U32_TO_U8S_LE(0xFE), U32_TO_U8S_LE(0), U32_TO_U8S_LE(0), U32_TO_U8S_LE(0x40840), U32_TO_U8S_LE(65544+10), 0xFF, 0xFF, U16_TO_U8S_LE(0x0), 0, 0x1, \
7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0, \
7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0, \
7, TUSB_DESC_ENDPOINT, _epint, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_epsize), 0
#endif
const uint8_t desc_config[] = {
TUD_CONFIG_DESCRIPTOR(1, ITF_TOTAL, 4, TUSB_DESC_TOTAL_LEN, USB_CONFIG_ATT_ONE | TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
#ifdef USB_ITF_HID
TUD_HID_INOUT_DESCRIPTOR(ITF_HID, ITF_HID + 5, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, TUSB_DIR_IN_MASK | EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 10),
TUD_HID_DESCRIPTOR(ITF_KEYBOARD, ITF_KEYBOARD + 5, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report_kb), TUSB_DIR_IN_MASK | (EPNUM_HID + 1), 16, 5),
#endif
#ifdef USB_ITF_CCID
TUD_SMARTCARD_DESCRIPTOR(ITF_CCID, ITF_CCID+5, 1, TUSB_DIR_IN_MASK | 1, TUSB_DIR_IN_MASK | 2, 64),
#ifdef USB_ITF_WCID
TUD_SMARTCARD_DESCRIPTOR_WEB(ITF_WCID, ITF_WCID+5, 3, TUSB_DIR_IN_MASK | 3, 64),
#endif
#endif
};
static uint8_t desc_hid_kb[] = {
TUD_HID_DESCRIPTOR(ITF_KEYBOARD, ITF_KEYBOARD + 5, HID_ITF_PROTOCOL_NONE,
sizeof(desc_hid_report_kb), 0x80 | (EPNUM_HID + 1), 16, 5)
};
#ifdef USB_ITF_HID
#include "apdu.h"
uint8_t const *tud_hid_descriptor_report_cb(uint8_t itf) {
printf("report_cb %d\n", itf);
@@ -235,50 +146,24 @@ uint8_t const *tud_hid_descriptor_report_cb(uint8_t itf) {
return NULL;
}
#endif
#ifndef ESP_PLATFORM
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);
#ifdef USB_ITF_HID
memcpy(p, &desc_hid, sizeof(desc_hid)); p += sizeof(desc_hid);
memcpy(p, &desc_hid_kb, sizeof(desc_hid_kb)); p += sizeof(desc_hid_kb);
#endif
#ifdef USB_ITF_CCID
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);
memcpy(p, &desc_ep3, sizeof(tusb_desc_endpoint_t)); p += sizeof(tusb_desc_endpoint_t);
memcpy(p, &desc_interface_wcid, 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_wcid, sizeof(tusb_desc_endpoint_t)); p += sizeof(tusb_desc_endpoint_t);
memcpy(p, &desc_ep2_wcid, sizeof(tusb_desc_endpoint_t)); p += sizeof(tusb_desc_endpoint_t);
#endif
initd = 1;
}
return (const uint8_t *) desc_config_extended;
printf("tud_descriptor_configuration_cb %d\n",index);
return desc_config;
}
#endif
#ifdef USB_ITF_WCID
#define BOS_TOTAL_LEN (TUD_BOS_DESC_LEN + TUD_BOS_WEBUSB_DESC_LEN + TUD_BOS_MICROSOFT_OS_DESC_LEN)
#define MS_OS_20_DESC_LEN 0xB2
enum
{
VENDOR_REQUEST_WEBUSB = 1,
VENDOR_REQUEST_MICROSOFT = 2
};
#define URL "picokeys.com/pki/"
#define URL "www.picokeys.com/pki/"
static bool web_serial_connected = false;
const tusb_desc_webusb_url_t desc_url =
@@ -369,10 +254,9 @@ uint8_t const desc_bos[] = {
};
uint8_t const *tud_descriptor_bos_cb(void) {
printf("!!!!!!!!!!!! tud_descriptor_bos_cb\n");
return desc_bos;
}
#endif
//--------------------------------------------------------------------+
// String Descriptors
//--------------------------------------------------------------------+
@@ -390,10 +274,21 @@ char const *string_desc_arr [] = {
#endif
#ifdef USB_ITF_CCID
, "Pico Key CCID Interface"
#ifdef USB_ITF_WCID
, "Pico Key WebCCID Interface"
#endif
#endif
};
#ifdef ESP_PLATFORM
const tinyusb_config_t tusb_cfg = {
.device_descriptor = &desc_device,
.string_descriptor = string_desc_arr,
.string_descriptor_count = sizeof(string_desc_arr) / sizeof(string_desc_arr[0]),
.external_phy = false,
.configuration_descriptor = desc_config,
};
#else
static uint16_t _desc_str[32];
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
@@ -414,12 +309,8 @@ uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
}
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;
str = pico_serial_str;
}
chr_count = strlen(str);
@@ -437,3 +328,4 @@ uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
return _desc_str;
}
#endif