diff --git a/src/usb/ccid/ccid.c b/src/usb/ccid/ccid.c index 372e1b8..8f61d8a 100644 --- a/src/usb/ccid/ccid.c +++ b/src/usb/ccid/ccid.c @@ -122,6 +122,9 @@ uint8_t sc_itf_to_usb_itf(uint8_t itf) { } void ccid_init_buffers() { + if (ITF_SC_TOTAL == 0) { + return; + } if (ccid_rx == NULL) { ccid_rx = (usb_buffer_t *)calloc(ITF_SC_TOTAL, sizeof(usb_buffer_t)); } @@ -170,11 +173,6 @@ void tud_vendor_rx_cb(uint8_t itf, const uint8_t *buffer, uint16_t bufsize) { } while (len > 0); } -void tud_vendor_tx_cb(uint8_t itf, uint32_t sent_bytes) { - (void) sent_bytes; - tud_vendor_n_write_flush(itf); -} - int driver_write_ccid(uint8_t itf, const uint8_t *tx_buffer, uint16_t buffer_size) { if (*tx_buffer != 0x81) { DEBUG_PAYLOAD(tx_buffer, buffer_size); @@ -346,16 +344,17 @@ void ccid_task() { } } -#ifndef ENABLE_EMULATION - -#define USB_CONFIG_ATT_ONE TU_BIT(7) - -#define MAX_USB_POWER 1 - void ccid_init() { ccid_init_buffers(); } +#ifndef ENABLE_EMULATION + +void tud_vendor_tx_cb(uint8_t itf, uint32_t sent_bytes) { + (void) sent_bytes; + tud_vendor_n_write_flush(itf); +} + static void ccid_init_cb(void) { vendord_init(); } diff --git a/src/usb/emulation/emulation.c b/src/usb/emulation/emulation.c index a53ac32..9c37a60 100644 --- a/src/usb/emulation/emulation.c +++ b/src/usb/emulation/emulation.c @@ -303,28 +303,24 @@ uint16_t emul_read(uint8_t itf) { driver_write_emul(itf, ccid_atr ? ccid_atr + 1 : NULL, ccid_atr ? ccid_atr[0] : 0); } } - else { - switch(itf) { #ifdef USB_ITF_CCID - case ITF_CCID: { - uint16_t sent = 0; - DEBUG_PAYLOAD(emul_rx, len); - apdu.rdata = emul_tx; - if ((sent = apdu_process(itf, emul_rx, len)) > 0) { - process_apdu(); - apdu_finish(); - } - if (sent > 0) { - uint16_t ret = apdu_next(); - DEBUG_PAYLOAD(apdu.rdata, ret); - driver_write_emul(itf, apdu.rdata, ret); - } - break; + else if (itf == ITF_CCID) { + uint16_t sent = 0; + DEBUG_PAYLOAD(emul_rx, len); + apdu.rdata = emul_tx; + if ((sent = apdu_process(itf, emul_rx, len)) > 0) { + process_apdu(); + apdu_finish(); } + if (sent > 0) { + uint16_t ret = apdu_next(); + DEBUG_PAYLOAD(apdu.rdata, ret); + driver_write_emul(itf, apdu.rdata, ret); + } + } #endif - default: - emul_rx_size += valread; - } + else { + emul_rx_size += valread; } return (uint16_t)emul_rx_size; } diff --git a/src/usb/hid/hid.c b/src/usb/hid/hid.c index ad93013..d0ddd95 100644 --- a/src/usb/hid/hid.c +++ b/src/usb/hid/hid.c @@ -66,6 +66,9 @@ int driver_write_hid(uint8_t itf, const uint8_t *buffer, uint16_t buffer_size); int driver_process_usb_nopacket_hid(); void hid_init() { + if (ITF_HID_TOTAL == 0) { + return; + } if (send_buffer_size == NULL) { send_buffer_size = (uint16_t *)calloc(ITF_HID_TOTAL, sizeof(uint16_t)); } diff --git a/src/usb/usb.c b/src/usb/usb.c index bfebca8..5b5875f 100644 --- a/src/usb/usb.c +++ b/src/usb/usb.c @@ -45,15 +45,15 @@ static mutex_t mutex; #endif #ifdef USB_ITF_HID - uint8_t ITF_HID_CTAP = 0, ITF_HID_KB = 0; - uint8_t ITF_HID = 0, ITF_KEYBOARD = 0; + uint8_t ITF_HID_CTAP = ITF_INVALID, ITF_HID_KB = ITF_INVALID; + uint8_t ITF_HID = ITF_INVALID, ITF_KEYBOARD = ITF_INVALID; uint8_t ITF_HID_TOTAL = 0; extern void hid_init(); #endif #ifdef USB_ITF_CCID - uint8_t ITF_SC_CCID = 0, ITF_SC_WCID = 0; - uint8_t ITF_CCID = 0, ITF_WCID = 0; + uint8_t ITF_SC_CCID = ITF_INVALID, ITF_SC_WCID = ITF_INVALID; + uint8_t ITF_CCID = ITF_INVALID, ITF_WCID = ITF_INVALID; uint8_t ITF_SC_TOTAL = 0; extern void ccid_init(); #endif @@ -80,6 +80,13 @@ void usb_init() { queue_init(&card_to_usb_q, sizeof(uint32_t), 64); queue_init(&usb_to_card_q, sizeof(uint32_t), 64); + uint8_t enabled_usb_itf = PHY_USB_ITF_CCID | PHY_USB_ITF_WCID | PHY_USB_ITF_HID | PHY_USB_ITF_KB; +#ifndef ENABLE_EMULATION + if (phy_data.enabled_usb_itf_present) { + enabled_usb_itf = phy_data.enabled_usb_itf; + } +#endif + #ifdef USB_ITF_HID ITF_HID_TOTAL = 0; #endif @@ -88,21 +95,21 @@ void usb_init() { #endif ITF_TOTAL = 0; #ifdef USB_ITF_HID - if (1) { + if (enabled_usb_itf & PHY_USB_ITF_HID) { ITF_HID_CTAP = ITF_HID_TOTAL++; ITF_HID = ITF_TOTAL++; } - if (1) { + if (enabled_usb_itf & PHY_USB_ITF_KB) { ITF_HID_KB = ITF_HID_TOTAL++; ITF_KEYBOARD = ITF_TOTAL++; } #endif #ifdef USB_ITF_CCID - if (1) { + if (enabled_usb_itf & PHY_USB_ITF_CCID) { ITF_SC_CCID = ITF_SC_TOTAL++; ITF_CCID = ITF_TOTAL++; } - if (1) { + if (enabled_usb_itf & PHY_USB_ITF_WCID) { ITF_SC_WCID = ITF_SC_TOTAL++; ITF_WCID = ITF_TOTAL++; } @@ -112,10 +119,14 @@ void usb_init() { timeout_counter = (uint32_t *)calloc(ITF_TOTAL, sizeof(uint32_t)); } #ifdef USB_ITF_HID - hid_init(); + if (ITF_HID_TOTAL > 0) { + hid_init(); + } #endif #ifdef USB_ITF_CCID - ccid_init(); + if (ITF_SC_TOTAL > 0) { + ccid_init(); + } #endif } diff --git a/src/usb/usb.h b/src/usb/usb.h index 545f674..06a799e 100644 --- a/src/usb/usb.h +++ b/src/usb/usb.h @@ -44,6 +44,8 @@ #define EV_BUTTON_TIMEOUT 16 #define EV_BUTTON_PRESSED 32 +static const uint8_t ITF_INVALID = 0xFF; + #ifdef USB_ITF_HID extern uint8_t ITF_HID_CTAP, ITF_HID_KB; extern uint8_t ITF_HID, ITF_KEYBOARD; diff --git a/src/usb/usb_descriptors.c b/src/usb/usb_descriptors.c index d28b66a..f0f35ba 100644 --- a/src/usb/usb_descriptors.c +++ b/src/usb/usb_descriptors.c @@ -147,38 +147,38 @@ uint8_t const *tud_descriptor_configuration_cb(uint8_t index) { TUSB_DESC_TOTAL_LEN = TUD_CONFIG_DESC_LEN; uint8_t *p = desc_config + TUD_CONFIG_DESC_LEN; #ifdef USB_ITF_HID - if (1) { + if (ITF_HID != ITF_INVALID) { TUSB_DESC_TOTAL_LEN += TUD_HID_INOUT_DESC_LEN; const uint8_t desc[] = { TUD_HID_INOUT_DESCRIPTOR(0, 0, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, TUSB_DIR_IN_MASK | EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 10) }; memcpy(p, desc, sizeof(desc)); p[2] = ITF_HID; - p[8] = ITF_HID + 5; + p[8] = 5; p += sizeof(desc); } - if (1) { + if (ITF_KEYBOARD != ITF_INVALID) { TUSB_DESC_TOTAL_LEN += TUD_HID_DESC_LEN; const uint8_t desc_kb[] = { 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) }; memcpy(p, desc_kb, sizeof(desc_kb)); p[2] = ITF_KEYBOARD; - p[8] = ITF_KEYBOARD + 5; + p[8] = 6; p += sizeof(desc_kb); } #endif #ifdef USB_ITF_CCID - if (1) { + if (ITF_CCID != ITF_INVALID) { TUSB_DESC_TOTAL_LEN += TUSB_SMARTCARD_CCID_DESC_LEN; const uint8_t desc_ccid[] = { TUD_SMARTCARD_DESCRIPTOR(ITF_CCID, ITF_CCID+5, 1, TUSB_DIR_IN_MASK | 1, TUSB_DIR_IN_MASK | 2, 64) }; memcpy(p, desc_ccid, sizeof(desc_ccid)); p[2] = ITF_CCID; - p[8] = ITF_CCID + 5; + p[8] = 7; p += sizeof(desc_ccid); } - if (1) { + if (ITF_WCID != ITF_INVALID) { TUSB_DESC_TOTAL_LEN += TUSB_SMARTCARD_WCID_DESC_LEN; const uint8_t desc_wcid[] = { TUD_SMARTCARD_DESCRIPTOR_WEB(ITF_WCID, ITF_WCID+5, 3, TUSB_DIR_IN_MASK | 3, 64) }; memcpy(p, desc_wcid, sizeof(desc_wcid)); p[2] = ITF_WCID; - p[8] = ITF_WCID + 5; + p[8] = 8; p += sizeof(desc_wcid); } #endif @@ -302,14 +302,10 @@ char const *string_desc_arr [] = { "Pico Key", // 2: Product "11223344", // 3: Serials, should use chip ID "Config" // 4: Vendor Interface -#ifdef USB_ITF_HID , "HID Interface" , "HID Keyboard Interface" -#endif -#ifdef USB_ITF_CCID , "CCID OTP FIDO Interface" , "WebCCID Interface" -#endif }; #ifdef ESP_PLATFORM