mirror of
https://github.com/polhenarejos/pico-keys-sdk
synced 2026-05-08 05:56:11 +02:00
Harmonizing coding style.
Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
@@ -50,33 +50,33 @@
|
||||
#define USB_BUF_SIZE (MAX_CMD_APDU_DATA_SIZE+20+9)
|
||||
#endif
|
||||
|
||||
#define CCID_SET_PARAMS 0x61 /* non-ICCD command */
|
||||
#define CCID_POWER_ON 0x62
|
||||
#define CCID_POWER_OFF 0x63
|
||||
#define CCID_SLOT_STATUS 0x65 /* non-ICCD command */
|
||||
#define CCID_SECURE 0x69 /* non-ICCD command */
|
||||
#define CCID_GET_PARAMS 0x6C /* non-ICCD command */
|
||||
#define CCID_RESET_PARAMS 0x6D /* non-ICCD command */
|
||||
#define CCID_XFR_BLOCK 0x6F
|
||||
#define CCID_DATA_BLOCK_RET 0x80
|
||||
#define CCID_SLOT_STATUS_RET 0x81 /* non-ICCD result */
|
||||
#define CCID_PARAMS_RET 0x82 /* non-ICCD result */
|
||||
#define CCID_SET_PARAMS 0x61 /* non-ICCD command */
|
||||
#define CCID_POWER_ON 0x62
|
||||
#define CCID_POWER_OFF 0x63
|
||||
#define CCID_SLOT_STATUS 0x65 /* non-ICCD command */
|
||||
#define CCID_SECURE 0x69 /* non-ICCD command */
|
||||
#define CCID_GET_PARAMS 0x6C /* non-ICCD command */
|
||||
#define CCID_RESET_PARAMS 0x6D /* non-ICCD command */
|
||||
#define CCID_XFR_BLOCK 0x6F
|
||||
#define CCID_DATA_BLOCK_RET 0x80
|
||||
#define CCID_SLOT_STATUS_RET 0x81 /* non-ICCD result */
|
||||
#define CCID_PARAMS_RET 0x82 /* non-ICCD result */
|
||||
|
||||
#define CCID_MSG_SEQ_OFFSET 6
|
||||
#define CCID_MSG_STATUS_OFFSET 7
|
||||
#define CCID_MSG_ERROR_OFFSET 8
|
||||
#define CCID_MSG_CHAIN_OFFSET 9
|
||||
#define CCID_MSG_DATA_OFFSET 10 /* == CCID_MSG_HEADER_SIZE */
|
||||
#define CCID_MAX_MSG_DATA_SIZE USB_BUF_SIZE
|
||||
#define CCID_MSG_SEQ_OFFSET 6
|
||||
#define CCID_MSG_STATUS_OFFSET 7
|
||||
#define CCID_MSG_ERROR_OFFSET 8
|
||||
#define CCID_MSG_CHAIN_OFFSET 9
|
||||
#define CCID_MSG_DATA_OFFSET 10 /* == CCID_MSG_HEADER_SIZE */
|
||||
#define CCID_MAX_MSG_DATA_SIZE USB_BUF_SIZE
|
||||
|
||||
#define CCID_STATUS_RUN 0x00
|
||||
#define CCID_STATUS_PRESENT 0x01
|
||||
#define CCID_STATUS_NOTPRESENT 0x02
|
||||
#define CCID_CMD_STATUS_OK 0x00
|
||||
#define CCID_CMD_STATUS_ERROR 0x40
|
||||
#define CCID_CMD_STATUS_TIMEEXT 0x80
|
||||
#define CCID_STATUS_RUN 0x00
|
||||
#define CCID_STATUS_PRESENT 0x01
|
||||
#define CCID_STATUS_NOTPRESENT 0x02
|
||||
#define CCID_CMD_STATUS_OK 0x00
|
||||
#define CCID_CMD_STATUS_ERROR 0x40
|
||||
#define CCID_CMD_STATUS_TIMEEXT 0x80
|
||||
|
||||
#define CCID_ERROR_XFR_OVERRUN 0xFC
|
||||
#define CCID_ERROR_XFR_OVERRUN 0xFC
|
||||
|
||||
/*
|
||||
* Since command-byte is at offset 0,
|
||||
@@ -97,67 +97,78 @@ struct ccid_header {
|
||||
uint8_t abRFU0;
|
||||
uint16_t abRFU1;
|
||||
uint8_t apdu; //Actually it is an array
|
||||
} __attribute__ ((__packed__));
|
||||
} __attribute__((__packed__));
|
||||
|
||||
uint8_t ccid_status = 1;
|
||||
static uint8_t itf_num;
|
||||
|
||||
void ccid_write_offset(uint16_t size, uint16_t offset) {
|
||||
if (*usb_get_tx(ITF_CCID)+offset != 0x81)
|
||||
DEBUG_PAYLOAD(usb_get_tx(ITF_CCID)+offset,size+10);
|
||||
void ccid_write_offset(uint16_t size, uint16_t offset)
|
||||
{
|
||||
if (*usb_get_tx(ITF_CCID)+offset != 0x81) {
|
||||
DEBUG_PAYLOAD(usb_get_tx(ITF_CCID)+offset, size+10);
|
||||
}
|
||||
usb_write_offset(ITF_CCID, size+10, offset);
|
||||
}
|
||||
|
||||
void ccid_write(uint16_t size) {
|
||||
void ccid_write(uint16_t size)
|
||||
{
|
||||
ccid_write_offset(size, 0);
|
||||
}
|
||||
|
||||
struct ccid_header *ccid_response;
|
||||
struct ccid_header *ccid_header;
|
||||
|
||||
int driver_init_ccid() {
|
||||
ccid_header = (struct ccid_header *)usb_get_rx(ITF_CCID);
|
||||
int driver_init_ccid()
|
||||
{
|
||||
ccid_header = (struct ccid_header *) usb_get_rx(ITF_CCID);
|
||||
// apdu.header = &ccid_header->apdu;
|
||||
|
||||
ccid_response = (struct ccid_header *)usb_get_tx(ITF_CCID);
|
||||
ccid_response = (struct ccid_header *) usb_get_tx(ITF_CCID);
|
||||
|
||||
usb_set_timeout_counter(ITF_CCID, 1500);
|
||||
|
||||
return CCID_OK;
|
||||
}
|
||||
|
||||
void tud_vendor_rx_cb(uint8_t itf) {
|
||||
void tud_vendor_rx_cb(uint8_t itf)
|
||||
{
|
||||
(void) itf;
|
||||
|
||||
uint32_t len = tud_vendor_available();
|
||||
usb_rx(ITF_CCID, NULL, len);
|
||||
}
|
||||
|
||||
void tud_vendor_tx_cb(uint8_t itf, uint32_t sent_bytes) {
|
||||
printf("written %ld\n",sent_bytes);
|
||||
void tud_vendor_tx_cb(uint8_t itf, uint32_t sent_bytes)
|
||||
{
|
||||
printf("written %ld\n", sent_bytes);
|
||||
usb_write_flush(ITF_CCID);
|
||||
}
|
||||
|
||||
int driver_write_ccid(const uint8_t *buffer, size_t buffer_size) {
|
||||
int driver_write_ccid(const uint8_t *buffer, size_t buffer_size)
|
||||
{
|
||||
return tud_vendor_write(buffer, buffer_size);
|
||||
}
|
||||
|
||||
size_t driver_read_ccid(uint8_t *buffer, size_t buffer_size) {
|
||||
size_t driver_read_ccid(uint8_t *buffer, size_t buffer_size)
|
||||
{
|
||||
return tud_vendor_read(buffer, buffer_size);
|
||||
}
|
||||
|
||||
int driver_process_usb_nopacket_ccid() {
|
||||
int driver_process_usb_nopacket_ccid()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int driver_process_usb_packet_ccid(uint16_t rx_read) {
|
||||
int driver_process_usb_packet_ccid(uint16_t rx_read)
|
||||
{
|
||||
if (rx_read >= 10) {
|
||||
driver_init_ccid();
|
||||
//printf("%d %d %x\r\n",tccid->dwLength,rx_read-10,tccid->bMessageType);
|
||||
if (ccid_header->dwLength <= rx_read-10) {
|
||||
size_t apdu_sent = 0;
|
||||
if (ccid_header->bMessageType != 0x65)
|
||||
DEBUG_PAYLOAD(usb_get_rx(ITF_CCID),usb_read_available(ITF_CCID));
|
||||
if (ccid_header->bMessageType != 0x65) {
|
||||
DEBUG_PAYLOAD(usb_get_rx(ITF_CCID), usb_read_available(ITF_CCID));
|
||||
}
|
||||
if (ccid_header->bMessageType == 0x65) {
|
||||
ccid_response->bMessageType = CCID_SLOT_STATUS_RET;
|
||||
ccid_response->dwLength = 0;
|
||||
@@ -166,8 +177,7 @@ int driver_process_usb_packet_ccid(uint16_t rx_read) {
|
||||
ccid_response->abRFU0 = ccid_status;
|
||||
ccid_response->abRFU1 = 0;
|
||||
ccid_write(0);
|
||||
}
|
||||
else if (ccid_header->bMessageType == 0x62) {
|
||||
} else if (ccid_header->bMessageType == 0x62) {
|
||||
size_t size_atr = (ccid_atr ? ccid_atr[0] : 0);
|
||||
ccid_response->bMessageType = 0x80;
|
||||
ccid_response->dwLength = size_atr;
|
||||
@@ -180,10 +190,10 @@ int driver_process_usb_packet_ccid(uint16_t rx_read) {
|
||||
card_start(apdu_thread);
|
||||
ccid_status = 0;
|
||||
ccid_write(size_atr);
|
||||
}
|
||||
else if (ccid_header->bMessageType == 0x63) {
|
||||
if (ccid_status == 0)
|
||||
} else if (ccid_header->bMessageType == 0x63) {
|
||||
if (ccid_status == 0) {
|
||||
card_exit(0);
|
||||
}
|
||||
ccid_status = 1;
|
||||
ccid_response->bMessageType = CCID_SLOT_STATUS_RET;
|
||||
ccid_response->dwLength = 0;
|
||||
@@ -192,8 +202,7 @@ int driver_process_usb_packet_ccid(uint16_t rx_read) {
|
||||
ccid_response->abRFU0 = ccid_status;
|
||||
ccid_response->abRFU1 = 0;
|
||||
ccid_write(0);
|
||||
}
|
||||
else if (ccid_header->bMessageType == 0x6F) {
|
||||
} else if (ccid_header->bMessageType == 0x6F) {
|
||||
apdu_sent = apdu_process(ITF_CCID, &ccid_header->apdu, ccid_header->dwLength);
|
||||
}
|
||||
usb_clear_rx(ITF_CCID);
|
||||
@@ -201,23 +210,25 @@ int driver_process_usb_packet_ccid(uint16_t rx_read) {
|
||||
}
|
||||
}
|
||||
/*
|
||||
if (usb_read_available() && c->epo->ready) {
|
||||
if (usb_read_available() && c->epo->ready) {
|
||||
if ()
|
||||
uint32_t count = usb_read(endp1_rx_buf, sizeof(endp1_rx_buf));
|
||||
//if (endp1_rx_buf[0] != 0x65)
|
||||
DEBUG_PAYLOAD(endp1_rx_buf, count);
|
||||
//DEBUG_PAYLOAD(endp1_rx_buf, count);
|
||||
ccid_rx_ready(count);
|
||||
}
|
||||
*/
|
||||
}
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool driver_mounted_ccid() {
|
||||
bool driver_mounted_ccid()
|
||||
{
|
||||
return tud_vendor_mounted();
|
||||
}
|
||||
|
||||
void driver_exec_timeout_ccid() {
|
||||
void driver_exec_timeout_ccid()
|
||||
{
|
||||
ccid_response->bMessageType = CCID_DATA_BLOCK_RET;
|
||||
ccid_response->dwLength = 0;
|
||||
ccid_response->bSlot = 0;
|
||||
@@ -227,7 +238,8 @@ void driver_exec_timeout_ccid() {
|
||||
ccid_write(0);
|
||||
}
|
||||
|
||||
void driver_exec_finished_ccid(size_t size_next) {
|
||||
void driver_exec_finished_ccid(size_t size_next)
|
||||
{
|
||||
ccid_response->bMessageType = CCID_DATA_BLOCK_RET;
|
||||
ccid_response->dwLength = size_next;
|
||||
ccid_response->bSlot = 0;
|
||||
@@ -237,9 +249,10 @@ void driver_exec_finished_ccid(size_t size_next) {
|
||||
ccid_write(size_next);
|
||||
}
|
||||
|
||||
void driver_exec_finished_cont_ccid(size_t size_next, size_t offset) {
|
||||
void driver_exec_finished_cont_ccid(size_t size_next, size_t offset)
|
||||
{
|
||||
|
||||
ccid_response = (struct ccid_header *)(usb_get_tx(ITF_CCID)+offset-10);
|
||||
ccid_response = (struct ccid_header *) (usb_get_tx(ITF_CCID)+offset-10);
|
||||
ccid_response->bMessageType = CCID_DATA_BLOCK_RET;
|
||||
ccid_response->dwLength = size_next;
|
||||
ccid_response->bSlot = 0;
|
||||
@@ -249,40 +262,47 @@ void driver_exec_finished_cont_ccid(size_t size_next, size_t offset) {
|
||||
ccid_write_offset(size_next, offset-10);
|
||||
}
|
||||
|
||||
uint8_t *driver_prepare_response_ccid() {
|
||||
ccid_response = (struct ccid_header *)usb_get_tx(ITF_CCID);
|
||||
uint8_t *driver_prepare_response_ccid()
|
||||
{
|
||||
ccid_response = (struct ccid_header *) usb_get_tx(ITF_CCID);
|
||||
apdu.rdata = &ccid_response->apdu;
|
||||
return &ccid_response->apdu;
|
||||
}
|
||||
#define USB_CONFIG_ATT_ONE TU_BIT(7)
|
||||
|
||||
#define MAX_USB_POWER 1
|
||||
#define MAX_USB_POWER 1
|
||||
|
||||
static void ccid_init_cb(void) {
|
||||
static void ccid_init_cb(void)
|
||||
{
|
||||
TU_LOG1("-------- CCID INIT\r\n");
|
||||
vendord_init();
|
||||
|
||||
//ccid_notify_slot_change(c);
|
||||
}
|
||||
|
||||
static void ccid_reset_cb(uint8_t rhport) {
|
||||
static void ccid_reset_cb(uint8_t rhport)
|
||||
{
|
||||
TU_LOG1("-------- CCID RESET\r\n");
|
||||
itf_num = 0;
|
||||
vendord_reset(rhport);
|
||||
}
|
||||
|
||||
static uint16_t ccid_open(uint8_t rhport, tusb_desc_interface_t const *itf_desc, uint16_t max_len) {
|
||||
uint8_t *itf_vendor = (uint8_t *)malloc(sizeof(uint8_t)*max_len);
|
||||
static uint16_t ccid_open(uint8_t rhport, tusb_desc_interface_t const *itf_desc, uint16_t max_len)
|
||||
{
|
||||
uint8_t *itf_vendor = (uint8_t *) malloc(sizeof(uint8_t)*max_len);
|
||||
//TU_LOG1("-------- CCID OPEN\r\n");
|
||||
TU_VERIFY(itf_desc->bInterfaceClass == TUSB_CLASS_SMART_CARD && itf_desc->bInterfaceSubClass == 0 && itf_desc->bInterfaceProtocol == 0, 0);
|
||||
TU_VERIFY(
|
||||
itf_desc->bInterfaceClass == TUSB_CLASS_SMART_CARD && itf_desc->bInterfaceSubClass == 0 && itf_desc->bInterfaceProtocol == 0,
|
||||
0);
|
||||
|
||||
//vendord_open expects a CLASS_VENDOR interface class
|
||||
memcpy(itf_vendor, itf_desc, sizeof(uint8_t)*max_len);
|
||||
((tusb_desc_interface_t *)itf_vendor)->bInterfaceClass = TUSB_CLASS_VENDOR_SPECIFIC;
|
||||
vendord_open(rhport, (tusb_desc_interface_t *)itf_vendor, max_len);
|
||||
((tusb_desc_interface_t *) itf_vendor)->bInterfaceClass = TUSB_CLASS_VENDOR_SPECIFIC;
|
||||
vendord_open(rhport, (tusb_desc_interface_t *) itf_vendor, max_len);
|
||||
free(itf_vendor);
|
||||
|
||||
uint16_t const drv_len = sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) + 2*sizeof(tusb_desc_endpoint_t);
|
||||
uint16_t const drv_len = sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) +
|
||||
2*sizeof(tusb_desc_endpoint_t);
|
||||
TU_VERIFY(max_len >= drv_len, 0);
|
||||
|
||||
itf_num = itf_desc->bInterfaceNumber;
|
||||
@@ -290,44 +310,56 @@ static uint16_t ccid_open(uint8_t rhport, tusb_desc_interface_t const *itf_desc,
|
||||
}
|
||||
|
||||
// Support for parameterized reset via vendor interface control request
|
||||
static bool ccid_control_xfer_cb(uint8_t __unused rhport, uint8_t stage, tusb_control_request_t const * request) {
|
||||
static bool ccid_control_xfer_cb(uint8_t __unused rhport,
|
||||
uint8_t stage,
|
||||
tusb_control_request_t const *request)
|
||||
{
|
||||
// nothing to do with DATA & ACK stage
|
||||
TU_LOG2("-------- CCID CTRL XFER\r\n");
|
||||
if (stage != CONTROL_STAGE_SETUP) return true;
|
||||
if (stage != CONTROL_STAGE_SETUP) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (request->wIndex == itf_num)
|
||||
{
|
||||
TU_LOG2("-------- bmRequestType %x, bRequest %x, wValue %x, wLength %x\r\n",request->bmRequestType,request->bRequest, request->wValue, request->wLength);
|
||||
if (request->wIndex == itf_num) {
|
||||
TU_LOG2("-------- bmRequestType %x, bRequest %x, wValue %x, wLength %x\r\n",
|
||||
request->bmRequestType,
|
||||
request->bRequest,
|
||||
request->wValue,
|
||||
request->wLength);
|
||||
/*
|
||||
#if PICO_STDIO_USB_RESET_INTERFACE_SUPPORT_RESET_TO_BOOTSEL
|
||||
#if PICO_STDIO_USB_RESET_INTERFACE_SUPPORT_RESET_TO_BOOTSEL
|
||||
if (request->bRequest == RESET_REQUEST_BOOTSEL) {
|
||||
#ifdef PICO_STDIO_USB_RESET_BOOTSEL_ACTIVITY_LED
|
||||
#ifdef PICO_STDIO_USB_RESET_BOOTSEL_ACTIVITY_LED
|
||||
uint gpio_mask = 1u << PICO_STDIO_USB_RESET_BOOTSEL_ACTIVITY_LED;
|
||||
#else
|
||||
#else
|
||||
uint gpio_mask = 0u;
|
||||
#endif
|
||||
#if !PICO_STDIO_USB_RESET_BOOTSEL_FIXED_ACTIVITY_LED
|
||||
#endif
|
||||
#if !PICO_STDIO_USB_RESET_BOOTSEL_FIXED_ACTIVITY_LED
|
||||
if (request->wValue & 0x100) {
|
||||
gpio_mask = 1u << (request->wValue >> 9u);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
reset_usb_boot(gpio_mask, (request->wValue & 0x7f) | PICO_STDIO_USB_RESET_BOOTSEL_INTERFACE_DISABLE_MASK);
|
||||
// does not return, otherwise we'd return true
|
||||
}
|
||||
#endif
|
||||
#if PICO_STDIO_USB_RESET_INTERFACE_SUPPORT_RESET_TO_FLASH_BOOT
|
||||
#endif
|
||||
#if PICO_STDIO_USB_RESET_INTERFACE_SUPPORT_RESET_TO_FLASH_BOOT
|
||||
if (request->bRequest == RESET_REQUEST_FLASH) {
|
||||
watchdog_reboot(0, 0, PICO_STDIO_USB_RESET_RESET_TO_FLASH_DELAY_MS);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
*/
|
||||
#endif
|
||||
*/
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool ccid_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) {
|
||||
static bool ccid_xfer_cb(uint8_t rhport,
|
||||
uint8_t ep_addr,
|
||||
xfer_result_t result,
|
||||
uint32_t xferred_bytes)
|
||||
{
|
||||
//printf("------ CALLED XFER_CB\r\n");
|
||||
return vendord_xfer_cb(rhport, ep_addr, result, xferred_bytes);
|
||||
//return true;
|
||||
@@ -346,7 +378,8 @@ static const usbd_class_driver_t ccid_driver = {
|
||||
};
|
||||
|
||||
// Implement callback to add our custom driver
|
||||
usbd_class_driver_t const *usbd_app_driver_get_cb(uint8_t *driver_count) {
|
||||
usbd_class_driver_t const *usbd_app_driver_get_cb(uint8_t *driver_count)
|
||||
{
|
||||
*driver_count = 1;
|
||||
return &ccid_driver;
|
||||
}
|
||||
|
||||
@@ -26,16 +26,16 @@ extern const uint8_t historical_bytes[];
|
||||
#define USB_LL_BUF_SIZE 64
|
||||
|
||||
enum ccid_state {
|
||||
CCID_STATE_NOCARD, /* No card available */
|
||||
CCID_STATE_START, /* Initial */
|
||||
CCID_STATE_WAIT, /* Waiting APDU */
|
||||
CCID_STATE_NOCARD, /* No card available */
|
||||
CCID_STATE_START, /* Initial */
|
||||
CCID_STATE_WAIT, /* Waiting APDU */
|
||||
|
||||
CCID_STATE_EXECUTE, /* Executing command */
|
||||
CCID_STATE_ACK_REQUIRED_0, /* Ack required (executing)*/
|
||||
CCID_STATE_ACK_REQUIRED_1, /* Waiting user's ACK (execution finished) */
|
||||
CCID_STATE_EXECUTE, /* Executing command */
|
||||
CCID_STATE_ACK_REQUIRED_0, /* Ack required (executing)*/
|
||||
CCID_STATE_ACK_REQUIRED_1, /* Waiting user's ACK (execution finished) */
|
||||
|
||||
CCID_STATE_EXITED, /* CCID Thread Terminated */
|
||||
CCID_STATE_EXEC_REQUESTED, /* Exec requested */
|
||||
CCID_STATE_EXITED, /* CCID Thread Terminated */
|
||||
CCID_STATE_EXEC_REQUESTED, /* Exec requested */
|
||||
};
|
||||
|
||||
extern const uint8_t *ccid_atr;
|
||||
|
||||
@@ -133,29 +133,29 @@ struct usb_endpoint_descriptor_long {
|
||||
|
||||
|
||||
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__));
|
||||
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 ccid_desc = {
|
||||
.bLength = sizeof(struct ccid_class_descriptor),
|
||||
@@ -164,8 +164,8 @@ static const struct ccid_class_descriptor ccid_desc = {
|
||||
.bMaxSlotIndex = 0,
|
||||
.bVoltageSupport = 0x01, // 5.0V
|
||||
.dwProtocols = (
|
||||
0x01| // T=0
|
||||
0x02), // T=1
|
||||
0x01| // T=0
|
||||
0x02), // T=1
|
||||
.dwDefaultClock = (0xDFC),
|
||||
.dwMaximumClock = (0xDFC),
|
||||
.bNumClockSupport = 0,
|
||||
@@ -219,90 +219,90 @@ struct usb_device_configuration {
|
||||
|
||||
// EP0 IN and OUT
|
||||
static const struct usb_endpoint_descriptor ep0_out = {
|
||||
.bLength = sizeof(struct usb_endpoint_descriptor),
|
||||
.bDescriptorType = USB_DT_ENDPOINT,
|
||||
.bEndpointAddress = EP0_OUT_ADDR, // EP number 0, OUT from host (rx to device)
|
||||
.bmAttributes = USB_TRANSFER_TYPE_CONTROL,
|
||||
.wMaxPacketSize = 64,
|
||||
.bInterval = 0
|
||||
.bLength = sizeof(struct usb_endpoint_descriptor),
|
||||
.bDescriptorType = USB_DT_ENDPOINT,
|
||||
.bEndpointAddress = EP0_OUT_ADDR, // EP number 0, OUT from host (rx to device)
|
||||
.bmAttributes = USB_TRANSFER_TYPE_CONTROL,
|
||||
.wMaxPacketSize = 64,
|
||||
.bInterval = 0
|
||||
};
|
||||
|
||||
static const struct usb_endpoint_descriptor ep0_in = {
|
||||
.bLength = sizeof(struct usb_endpoint_descriptor),
|
||||
.bDescriptorType = USB_DT_ENDPOINT,
|
||||
.bEndpointAddress = EP0_IN_ADDR, // EP number 0, OUT from host (rx to device)
|
||||
.bmAttributes = USB_TRANSFER_TYPE_CONTROL,
|
||||
.wMaxPacketSize = 64,
|
||||
.bInterval = 0
|
||||
.bLength = sizeof(struct usb_endpoint_descriptor),
|
||||
.bDescriptorType = USB_DT_ENDPOINT,
|
||||
.bEndpointAddress = EP0_IN_ADDR, // EP number 0, OUT from host (rx to device)
|
||||
.bmAttributes = USB_TRANSFER_TYPE_CONTROL,
|
||||
.wMaxPacketSize = 64,
|
||||
.bInterval = 0
|
||||
};
|
||||
|
||||
// Descriptors
|
||||
static const struct usb_device_descriptor device_descriptor = {
|
||||
.bLength = sizeof(struct usb_device_descriptor),
|
||||
.bDescriptorType = USB_DT_DEVICE,
|
||||
.bcdUSB = 0x0200, // USB 1.1 device
|
||||
.bDeviceClass = 0, // Specified in interface descriptor
|
||||
.bDeviceSubClass = 0, // No subclass
|
||||
.bDeviceProtocol = 0, // No protocol
|
||||
.bMaxPacketSize0 = 64, // Max packet size for ep0
|
||||
.idVendor = 0x20a0, // Your vendor id
|
||||
.idProduct = 0x4230, // Your product ID
|
||||
.bcdDevice = 0x0101, // No device revision number
|
||||
.iManufacturer = 1, // Manufacturer string index
|
||||
.iProduct = 2, // Product string index
|
||||
.iSerialNumber = 3, // No serial number
|
||||
.bNumConfigurations = 1 // One configuration
|
||||
.bLength = sizeof(struct usb_device_descriptor),
|
||||
.bDescriptorType = USB_DT_DEVICE,
|
||||
.bcdUSB = 0x0200, // USB 1.1 device
|
||||
.bDeviceClass = 0, // Specified in interface descriptor
|
||||
.bDeviceSubClass = 0, // No subclass
|
||||
.bDeviceProtocol = 0, // No protocol
|
||||
.bMaxPacketSize0 = 64, // Max packet size for ep0
|
||||
.idVendor = 0x20a0, // Your vendor id
|
||||
.idProduct = 0x4230, // Your product ID
|
||||
.bcdDevice = 0x0101, // No device revision number
|
||||
.iManufacturer = 1, // Manufacturer string index
|
||||
.iProduct = 2, // Product string index
|
||||
.iSerialNumber = 3, // No serial number
|
||||
.bNumConfigurations = 1 // One configuration
|
||||
};
|
||||
|
||||
static const struct usb_interface_descriptor interface_descriptor = {
|
||||
.bLength = sizeof(struct usb_interface_descriptor),
|
||||
.bDescriptorType = USB_DT_INTERFACE,
|
||||
.bInterfaceNumber = 0,
|
||||
.bAlternateSetting = 0,
|
||||
.bNumEndpoints = 2, // Interface has 2 endpoints
|
||||
.bInterfaceClass = 0x0b, // Vendor specific endpoint
|
||||
.bInterfaceSubClass = 0,
|
||||
.bInterfaceProtocol = 0,
|
||||
.iInterface = 5
|
||||
.bLength = sizeof(struct usb_interface_descriptor),
|
||||
.bDescriptorType = USB_DT_INTERFACE,
|
||||
.bInterfaceNumber = 0,
|
||||
.bAlternateSetting = 0,
|
||||
.bNumEndpoints = 2, // Interface has 2 endpoints
|
||||
.bInterfaceClass = 0x0b, // Vendor specific endpoint
|
||||
.bInterfaceSubClass = 0,
|
||||
.bInterfaceProtocol = 0,
|
||||
.iInterface = 5
|
||||
};
|
||||
|
||||
static const struct usb_endpoint_descriptor ep1_out = {
|
||||
.bLength = sizeof(struct usb_endpoint_descriptor),
|
||||
.bDescriptorType = USB_DT_ENDPOINT,
|
||||
.bEndpointAddress = EP1_OUT_ADDR, // EP number 1, OUT from host (rx to device)
|
||||
.bmAttributes = USB_TRANSFER_TYPE_BULK,
|
||||
.wMaxPacketSize = 64,
|
||||
.bInterval = 0
|
||||
.bLength = sizeof(struct usb_endpoint_descriptor),
|
||||
.bDescriptorType = USB_DT_ENDPOINT,
|
||||
.bEndpointAddress = EP1_OUT_ADDR, // EP number 1, OUT from host (rx to device)
|
||||
.bmAttributes = USB_TRANSFER_TYPE_BULK,
|
||||
.wMaxPacketSize = 64,
|
||||
.bInterval = 0
|
||||
};
|
||||
|
||||
static const struct usb_endpoint_descriptor ep2_in = {
|
||||
.bLength = sizeof(struct usb_endpoint_descriptor),
|
||||
.bDescriptorType = USB_DT_ENDPOINT,
|
||||
.bEndpointAddress = EP2_IN_ADDR, // EP number 2, IN from host (tx from device)
|
||||
.bmAttributes = USB_TRANSFER_TYPE_BULK,
|
||||
.wMaxPacketSize = 64,
|
||||
.bInterval = 0
|
||||
.bLength = sizeof(struct usb_endpoint_descriptor),
|
||||
.bDescriptorType = USB_DT_ENDPOINT,
|
||||
.bEndpointAddress = EP2_IN_ADDR, // EP number 2, IN from host (tx from device)
|
||||
.bmAttributes = USB_TRANSFER_TYPE_BULK,
|
||||
.wMaxPacketSize = 64,
|
||||
.bInterval = 0
|
||||
};
|
||||
|
||||
static const struct usb_configuration_descriptor config_descriptor = {
|
||||
.bLength = sizeof(struct usb_configuration_descriptor),
|
||||
.bDescriptorType = USB_DT_CONFIG,
|
||||
.wTotalLength = (sizeof(config_descriptor) +
|
||||
sizeof(interface_descriptor) +
|
||||
sizeof(ccid_desc) +
|
||||
sizeof(ep1_out) +
|
||||
sizeof(ep2_in)),
|
||||
.bNumInterfaces = 1,
|
||||
.bConfigurationValue = 1, // Configuration 1
|
||||
.iConfiguration = 4, // No string
|
||||
.bmAttributes = 0xa0, // attributes: self powered, no remote wakeup
|
||||
.bMaxPower = 0x32 // 100ma
|
||||
.bLength = sizeof(struct usb_configuration_descriptor),
|
||||
.bDescriptorType = USB_DT_CONFIG,
|
||||
.wTotalLength = (sizeof(config_descriptor) +
|
||||
sizeof(interface_descriptor) +
|
||||
sizeof(ccid_desc) +
|
||||
sizeof(ep1_out) +
|
||||
sizeof(ep2_in)),
|
||||
.bNumInterfaces = 1,
|
||||
.bConfigurationValue = 1, // Configuration 1
|
||||
.iConfiguration = 4, // No string
|
||||
.bmAttributes = 0xa0, // attributes: self powered, no remote wakeup
|
||||
.bMaxPower = 0x32 // 100ma
|
||||
};
|
||||
|
||||
static const unsigned char lang_descriptor[] = {
|
||||
4, // bLength
|
||||
0x03, // bDescriptorType == String Descriptor
|
||||
0x09, 0x04 // language id = us english
|
||||
4, // bLength
|
||||
0x03, // bDescriptorType == String Descriptor
|
||||
0x09, 0x04 // language id = us english
|
||||
};
|
||||
|
||||
|
||||
@@ -322,4 +322,3 @@ extern bool usb_write_available();
|
||||
extern uint32_t usb_write_flush();
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -38,8 +38,7 @@ int msleep(long msec)
|
||||
struct timespec ts;
|
||||
int res;
|
||||
|
||||
if (msec < 0)
|
||||
{
|
||||
if (msec < 0) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
@@ -54,7 +53,8 @@ int msleep(long msec)
|
||||
return res;
|
||||
}
|
||||
|
||||
int emul_init(char *host, uint16_t port) {
|
||||
int emul_init(char *host, uint16_t port)
|
||||
{
|
||||
struct sockaddr_in serv_addr;
|
||||
fprintf(stderr, "\n Starting emulation envionrment\n");
|
||||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
@@ -72,38 +72,43 @@ int emul_init(char *host, uint16_t port) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
|
||||
if (connect(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
|
||||
fprintf(stderr, "\nConnection Failed \n");
|
||||
return -1;
|
||||
}
|
||||
int x = fcntl(sock ,F_GETFL, 0);
|
||||
int x = fcntl(sock, F_GETFL, 0);
|
||||
fcntl(sock, F_SETFL, x | O_NONBLOCK);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t *driver_prepare_response_emul() {
|
||||
uint8_t *driver_prepare_response_emul()
|
||||
{
|
||||
apdu.rdata = usb_get_tx(ITF_EMUL);
|
||||
return apdu.rdata;
|
||||
}
|
||||
|
||||
int driver_write_emul(const uint8_t *buffer, size_t buffer_size) {
|
||||
int driver_write_emul(const uint8_t *buffer, size_t buffer_size)
|
||||
{
|
||||
uint16_t size = htons(buffer_size);
|
||||
//DEBUG_PAYLOAD(buffer,buffer_size);
|
||||
int ret = 0;
|
||||
do {
|
||||
ret = send(sock, &size, sizeof(size), 0);
|
||||
if (ret < 0)
|
||||
if (ret < 0) {
|
||||
msleep(10);
|
||||
} while(ret <= 0);
|
||||
}
|
||||
} while (ret <= 0);
|
||||
do {
|
||||
ret = send(sock, buffer, (uint16_t)buffer_size, 0);
|
||||
if (ret < 0)
|
||||
ret = send(sock, buffer, (uint16_t) buffer_size, 0);
|
||||
if (ret < 0) {
|
||||
msleep(10);
|
||||
} while(ret <= 0);
|
||||
}
|
||||
} while (ret <= 0);
|
||||
return buffer_size;
|
||||
}
|
||||
|
||||
uint32_t emul_write_offset(uint16_t size, uint16_t offset) {
|
||||
uint32_t emul_write_offset(uint16_t size, uint16_t offset)
|
||||
{
|
||||
if (size > 0) {
|
||||
//DEBUG_PAYLOAD(usb_get_tx(ITF_EMUL)+offset, size);
|
||||
return usb_write_offset(ITF_EMUL, size, offset);
|
||||
@@ -111,29 +116,33 @@ uint32_t emul_write_offset(uint16_t size, uint16_t offset) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t emul_write(uint16_t size) {
|
||||
uint32_t emul_write(uint16_t size)
|
||||
{
|
||||
return emul_write_offset(size, 0);
|
||||
}
|
||||
|
||||
void driver_exec_finished_cont_emul(size_t size_next, size_t offset) {
|
||||
void driver_exec_finished_cont_emul(size_t size_next, size_t offset)
|
||||
{
|
||||
emul_write_offset(size_next, offset);
|
||||
}
|
||||
|
||||
int driver_process_usb_packet_emul(uint16_t len) {
|
||||
int driver_process_usb_packet_emul(uint16_t len)
|
||||
{
|
||||
if (len > 0) {
|
||||
uint8_t *data = usb_get_rx(ITF_EMUL), *rdata = usb_get_tx(ITF_EMUL);
|
||||
if (len == 1) {
|
||||
uint8_t c = data[0];
|
||||
if (c == 4) {
|
||||
if (ccid_atr)
|
||||
if (ccid_atr) {
|
||||
memcpy(rdata, ccid_atr+1, ccid_atr[0]);
|
||||
}
|
||||
emul_write(ccid_atr ? ccid_atr[0] : 0);
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
DEBUG_PAYLOAD(data, len);
|
||||
if (apdu_process(ITF_EMUL, data, len) > 0)
|
||||
if (apdu_process(ITF_EMUL, data, len) > 0) {
|
||||
process_apdu();
|
||||
}
|
||||
apdu_finish();
|
||||
size_t ret = apdu_next();
|
||||
DEBUG_PAYLOAD(rdata, ret);
|
||||
@@ -144,7 +153,8 @@ int driver_process_usb_packet_emul(uint16_t len) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint16_t emul_read() {
|
||||
uint16_t emul_read()
|
||||
{
|
||||
uint16_t len = 0;
|
||||
fd_set input;
|
||||
FD_ZERO(&input);
|
||||
@@ -165,8 +175,9 @@ uint16_t emul_read() {
|
||||
if (len > 0) {
|
||||
while (true) {
|
||||
valread = recv(sock, usb_get_rx(ITF_EMUL), len, 0);
|
||||
if (valread > 0)
|
||||
if (valread > 0) {
|
||||
return valread;
|
||||
}
|
||||
msleep(10);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,21 +45,21 @@ extern "C" {
|
||||
#define TYPE_CONT 0x00 // Continuation frame identifier
|
||||
|
||||
typedef struct {
|
||||
uint32_t cid; // Channel identifier
|
||||
union {
|
||||
uint8_t type; // Frame type - b7 defines type
|
||||
struct {
|
||||
uint8_t cmd; // Command - b7 set
|
||||
uint8_t bcnth; // Message byte count - high part
|
||||
uint8_t bcntl; // Message byte count - low part
|
||||
uint8_t data[HID_RPT_SIZE - 7]; // Data payload
|
||||
} init;
|
||||
struct {
|
||||
uint8_t seq; // Sequence number - b7 cleared
|
||||
uint8_t data[HID_RPT_SIZE - 5]; // Data payload
|
||||
} cont;
|
||||
};
|
||||
} __attribute__ ((__packed__)) CTAPHID_FRAME;
|
||||
uint32_t cid; // Channel identifier
|
||||
union {
|
||||
uint8_t type; // Frame type - b7 defines type
|
||||
struct {
|
||||
uint8_t cmd; // Command - b7 set
|
||||
uint8_t bcnth; // Message byte count - high part
|
||||
uint8_t bcntl; // Message byte count - low part
|
||||
uint8_t data[HID_RPT_SIZE - 7]; // Data payload
|
||||
} init;
|
||||
struct {
|
||||
uint8_t seq; // Sequence number - b7 cleared
|
||||
uint8_t data[HID_RPT_SIZE - 5]; // Data payload
|
||||
} cont;
|
||||
};
|
||||
} __attribute__((__packed__)) CTAPHID_FRAME;
|
||||
|
||||
extern CTAPHID_FRAME *ctap_req, *ctap_resp;
|
||||
|
||||
@@ -108,27 +108,27 @@ extern CTAPHID_FRAME *ctap_req, *ctap_resp;
|
||||
#define CAPFLAG_CBOR 0x04 // Device supports CBOR command
|
||||
|
||||
typedef struct {
|
||||
uint8_t nonce[INIT_NONCE_SIZE]; // Client application nonce
|
||||
} __attribute__ ((__packed__)) CTAPHID_INIT_REQ;
|
||||
uint8_t nonce[INIT_NONCE_SIZE]; // Client application nonce
|
||||
} __attribute__((__packed__)) CTAPHID_INIT_REQ;
|
||||
|
||||
typedef struct {
|
||||
uint8_t nonce[INIT_NONCE_SIZE]; // Client application nonce
|
||||
uint32_t cid; // Channel identifier
|
||||
uint8_t versionInterface; // Interface version
|
||||
uint8_t versionMajor; // Major version number
|
||||
uint8_t versionMinor; // Minor version number
|
||||
uint8_t versionBuild; // Build version number
|
||||
uint8_t capFlags; // Capabilities flags
|
||||
} __attribute__ ((__packed__)) CTAPHID_INIT_RESP;
|
||||
uint8_t nonce[INIT_NONCE_SIZE]; // Client application nonce
|
||||
uint32_t cid; // Channel identifier
|
||||
uint8_t versionInterface; // Interface version
|
||||
uint8_t versionMajor; // Major version number
|
||||
uint8_t versionMinor; // Minor version number
|
||||
uint8_t versionBuild; // Build version number
|
||||
uint8_t capFlags; // Capabilities flags
|
||||
} __attribute__((__packed__)) CTAPHID_INIT_RESP;
|
||||
|
||||
// CTAPHID_SYNC command defines
|
||||
|
||||
typedef struct {
|
||||
uint8_t nonce; // Client application nonce
|
||||
uint8_t nonce; // Client application nonce
|
||||
} CTAPHID_SYNC_REQ;
|
||||
|
||||
typedef struct {
|
||||
uint8_t nonce; // Client application nonce
|
||||
uint8_t nonce; // Client application nonce
|
||||
} CTAPHID_SYNC_RESP;
|
||||
|
||||
// Low-level error codes. Return as negatives.
|
||||
|
||||
@@ -31,7 +31,7 @@ typedef struct msg_packet {
|
||||
uint16_t len;
|
||||
uint16_t current_len;
|
||||
uint8_t data[CTAP_MAX_PACKET_SIZE];
|
||||
} __attribute__ ((__packed__)) msg_packet_t;
|
||||
} __attribute__((__packed__)) msg_packet_t;
|
||||
|
||||
msg_packet_t msg_packet = { 0 };
|
||||
|
||||
@@ -40,18 +40,20 @@ void tud_mount_cb()
|
||||
mounted = true;
|
||||
}
|
||||
|
||||
bool driver_mounted_hid() {
|
||||
bool driver_mounted_hid()
|
||||
{
|
||||
return mounted;
|
||||
}
|
||||
|
||||
CTAPHID_FRAME *ctap_req = NULL, *ctap_resp = NULL;
|
||||
|
||||
int driver_init_hid() {
|
||||
int driver_init_hid()
|
||||
{
|
||||
tud_init(BOARD_TUD_RHPORT);
|
||||
ctap_req = (CTAPHID_FRAME *)usb_get_rx(ITF_HID);
|
||||
ctap_req = (CTAPHID_FRAME *) usb_get_rx(ITF_HID);
|
||||
apdu.header = ctap_req->init.data;
|
||||
|
||||
ctap_resp = (CTAPHID_FRAME *)usb_get_tx(ITF_HID);
|
||||
ctap_resp = (CTAPHID_FRAME *) usb_get_tx(ITF_HID);
|
||||
apdu.rdata = ctap_resp->init.data;
|
||||
|
||||
usb_set_timeout_counter(ITF_HID, 200);
|
||||
@@ -66,29 +68,36 @@ int driver_init_hid() {
|
||||
// Invoked when received GET_REPORT control request
|
||||
// Application must fill buffer report's content and return its length.
|
||||
// Return zero will cause the stack to STALL request
|
||||
uint16_t tud_hid_get_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen)
|
||||
uint16_t tud_hid_get_report_cb(uint8_t itf,
|
||||
uint8_t report_id,
|
||||
hid_report_type_t report_type,
|
||||
uint8_t *buffer,
|
||||
uint16_t reqlen)
|
||||
{
|
||||
// TODO not Implemented
|
||||
(void) itf;
|
||||
(void) report_id;
|
||||
(void) report_type;
|
||||
(void) buffer;
|
||||
(void) reqlen;
|
||||
printf("get_report %d %d %d\n",itf,report_id,report_type);
|
||||
DEBUG_PAYLOAD(buffer, reqlen);
|
||||
buffer[1] = HSM_SDK_VERSION_MAJOR;
|
||||
buffer[2] = HSM_SDK_VERSION_MINOR;
|
||||
// TODO not Implemented
|
||||
(void) itf;
|
||||
(void) report_id;
|
||||
(void) report_type;
|
||||
(void) buffer;
|
||||
(void) reqlen;
|
||||
printf("get_report %d %d %d\n", itf, report_id, report_type);
|
||||
DEBUG_PAYLOAD(buffer, reqlen);
|
||||
buffer[1] = HSM_SDK_VERSION_MAJOR;
|
||||
buffer[2] = HSM_SDK_VERSION_MINOR;
|
||||
|
||||
return reqlen;
|
||||
return reqlen;
|
||||
}
|
||||
|
||||
uint32_t hid_write_offset(uint16_t size, uint16_t offset) {
|
||||
if (*usb_get_tx(ITF_HID) != 0x81)
|
||||
uint32_t hid_write_offset(uint16_t size, uint16_t offset)
|
||||
{
|
||||
if (*usb_get_tx(ITF_HID) != 0x81) {
|
||||
DEBUG_PAYLOAD(usb_get_tx(ITF_HID)+offset, size);
|
||||
}
|
||||
return usb_write_offset(ITF_HID, size, offset);
|
||||
}
|
||||
|
||||
uint32_t hid_write(uint16_t size) {
|
||||
uint32_t hid_write(uint16_t size)
|
||||
{
|
||||
return hid_write_offset(size, 0);
|
||||
}
|
||||
|
||||
@@ -101,36 +110,41 @@ static const uint8_t conv_table[128][2] = { HID_ASCII_TO_KEYCODE };
|
||||
static uint8_t keyboard_w = 0;
|
||||
static bool sent_key = false;
|
||||
|
||||
void add_keyboard_buffer(const uint8_t *data, size_t data_len) {
|
||||
void add_keyboard_buffer(const uint8_t *data, size_t data_len)
|
||||
{
|
||||
keyboard_buffer_len = MIN(sizeof(keyboard_buffer), data_len);
|
||||
memcpy(keyboard_buffer, data, keyboard_buffer_len);
|
||||
}
|
||||
|
||||
static void send_hid_report(uint8_t report_id) {
|
||||
if (!tud_hid_ready())
|
||||
static void send_hid_report(uint8_t report_id)
|
||||
{
|
||||
if (!tud_hid_ready()) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch(report_id) {
|
||||
switch (report_id) {
|
||||
case REPORT_ID_KEYBOARD: {
|
||||
if (keyboard_w < keyboard_buffer_len) {
|
||||
if (sent_key == false) {
|
||||
uint8_t keycode[6] = { 0 };
|
||||
uint8_t modifier = 0;
|
||||
uint8_t chr = keyboard_buffer[keyboard_w];
|
||||
if (conv_table[chr][0])
|
||||
if (conv_table[chr][0]) {
|
||||
modifier = KEYBOARD_MODIFIER_LEFTSHIFT;
|
||||
}
|
||||
keycode[0] = conv_table[chr][1];
|
||||
if (tud_hid_n_keyboard_report(ITF_KEYBOARD, REPORT_ID_KEYBOARD, modifier, keycode) == true)
|
||||
if (tud_hid_n_keyboard_report(ITF_KEYBOARD, REPORT_ID_KEYBOARD, modifier,
|
||||
keycode) == true) {
|
||||
sent_key = true;
|
||||
}
|
||||
else {
|
||||
if (tud_hid_n_keyboard_report(ITF_KEYBOARD, REPORT_ID_KEYBOARD, 0, NULL) == true) {
|
||||
}
|
||||
} else {
|
||||
if (tud_hid_n_keyboard_report(ITF_KEYBOARD, REPORT_ID_KEYBOARD, 0,
|
||||
NULL) == true) {
|
||||
keyboard_w++;
|
||||
sent_key = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (keyboard_w == keyboard_buffer_len && keyboard_buffer_len > 0) {
|
||||
} else if (keyboard_w == keyboard_buffer_len && keyboard_buffer_len > 0) {
|
||||
keyboard_w = keyboard_buffer_len = 0;
|
||||
}
|
||||
}
|
||||
@@ -140,66 +154,78 @@ static void send_hid_report(uint8_t report_id) {
|
||||
}
|
||||
}
|
||||
|
||||
void hid_task(void) {
|
||||
void hid_task(void)
|
||||
{
|
||||
// Poll every 10ms
|
||||
const uint32_t interval_ms = 10;
|
||||
static uint32_t start_ms = 0;
|
||||
|
||||
if (board_millis() - start_ms < interval_ms)
|
||||
if (board_millis() - start_ms < interval_ms) {
|
||||
return;
|
||||
}
|
||||
start_ms += interval_ms;
|
||||
|
||||
// Remote wakeup
|
||||
if ( tud_suspended() && keyboard_buffer_len > 0) {
|
||||
if (tud_suspended() && keyboard_buffer_len > 0) {
|
||||
tud_remote_wakeup();
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
send_hid_report(REPORT_ID_KEYBOARD);
|
||||
}
|
||||
}
|
||||
|
||||
void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, /*uint16_t*/ uint8_t len) {
|
||||
void tud_hid_report_complete_cb(uint8_t instance, uint8_t const *report, /*uint16_t*/ uint8_t len)
|
||||
{
|
||||
if (send_buffer_size > 0 && instance == ITF_HID) {
|
||||
uint8_t seq = report[4] & TYPE_MASK ? 0 : report[4] + 1;
|
||||
if (last_write_result == true) {
|
||||
ctap_resp->cid = ctap_req->cid;
|
||||
ctap_resp->cont.seq = seq;
|
||||
}
|
||||
if (hid_write_offset(64, (uint8_t *)ctap_resp - (usb_get_tx(ITF_HID))) > 0) {
|
||||
if (hid_write_offset(64, (uint8_t *) ctap_resp - (usb_get_tx(ITF_HID))) > 0) {
|
||||
send_buffer_size -= MIN(64 - 5, send_buffer_size);
|
||||
ctap_resp = (CTAPHID_FRAME *)((uint8_t *)ctap_resp + 64 - 5);
|
||||
ctap_resp = (CTAPHID_FRAME *) ((uint8_t *) ctap_resp + 64 - 5);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int driver_write_hid(const uint8_t *buffer, size_t buffer_size) {
|
||||
int driver_write_hid(const uint8_t *buffer, size_t buffer_size)
|
||||
{
|
||||
last_write_result = tud_hid_n_report(ITF_HID, 0, buffer, buffer_size);
|
||||
printf("result %d\n", last_write_result);
|
||||
if (last_write_result == false)
|
||||
if (last_write_result == false) {
|
||||
return 0;
|
||||
}
|
||||
return MIN(64, buffer_size);
|
||||
}
|
||||
|
||||
size_t driver_read_hid(uint8_t *buffer, size_t buffer_size) {
|
||||
size_t driver_read_hid(uint8_t *buffer, size_t buffer_size)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Invoked when received SET_REPORT control request or
|
||||
// received data on OUT endpoint ( Report ID = 0, Type = 0 )
|
||||
void tud_hid_set_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize) {
|
||||
void tud_hid_set_report_cb(uint8_t itf,
|
||||
uint8_t report_id,
|
||||
hid_report_type_t report_type,
|
||||
uint8_t const *buffer,
|
||||
uint16_t bufsize)
|
||||
{
|
||||
// This example doesn't use multiple report and report ID
|
||||
(void) itf;
|
||||
(void) report_id;
|
||||
(void) report_type;
|
||||
printf("set_report %d %d %d\n",itf,report_id,report_type);
|
||||
if (itf == ITF_KEYBOARD)
|
||||
DEBUG_PAYLOAD(buffer,bufsize);
|
||||
printf("set_report %d %d %d\n", itf, report_id, report_type);
|
||||
if (itf == ITF_KEYBOARD) {
|
||||
DEBUG_PAYLOAD(buffer, bufsize);
|
||||
}
|
||||
usb_rx(itf, buffer, bufsize);
|
||||
}
|
||||
|
||||
uint32_t last_cmd_time = 0, last_packet_time = 0;
|
||||
int ctap_error(uint8_t error) {
|
||||
ctap_resp = (CTAPHID_FRAME *)usb_get_tx(ITF_HID);
|
||||
int ctap_error(uint8_t error)
|
||||
{
|
||||
ctap_resp = (CTAPHID_FRAME *) usb_get_tx(ITF_HID);
|
||||
memset(ctap_resp, 0, sizeof(CTAPHID_FRAME));
|
||||
ctap_resp->cid = ctap_req->cid;
|
||||
ctap_resp->init.cmd = CTAPHID_ERROR;
|
||||
@@ -220,7 +246,8 @@ uint8_t thread_type = 0; //1 is APDU, 2 is CBOR
|
||||
extern void cbor_thread();
|
||||
extern bool cancel_button;
|
||||
|
||||
int driver_process_usb_nopacket_hid() {
|
||||
int driver_process_usb_nopacket_hid()
|
||||
{
|
||||
if (last_packet_time > 0 && last_packet_time+500 < board_millis()) {
|
||||
ctap_error(CTAP1_ERR_MSG_TIMEOUT);
|
||||
last_packet_time = 0;
|
||||
@@ -231,32 +258,38 @@ int driver_process_usb_nopacket_hid() {
|
||||
|
||||
extern const uint8_t fido_aid[];
|
||||
|
||||
int driver_process_usb_packet_hid(uint16_t read) {
|
||||
int driver_process_usb_packet_hid(uint16_t read)
|
||||
{
|
||||
int apdu_sent = 0;
|
||||
if (read >= 5) {
|
||||
driver_init_hid();
|
||||
last_packet_time = board_millis();
|
||||
DEBUG_PAYLOAD(usb_get_rx(ITF_HID),64);
|
||||
DEBUG_PAYLOAD(usb_get_rx(ITF_HID), 64);
|
||||
memset(ctap_resp, 0, sizeof(CTAPHID_FRAME));
|
||||
if (ctap_req->cid == 0x0 || (ctap_req->cid == CID_BROADCAST && ctap_req->init.cmd != CTAPHID_INIT))
|
||||
if (ctap_req->cid == 0x0 ||
|
||||
(ctap_req->cid == CID_BROADCAST && ctap_req->init.cmd != CTAPHID_INIT)) {
|
||||
return ctap_error(CTAP1_ERR_INVALID_CHANNEL);
|
||||
if (board_millis() < lock && ctap_req->cid != last_req.cid && last_cmd_time+100 > board_millis())
|
||||
}
|
||||
if (board_millis() < lock && ctap_req->cid != last_req.cid &&
|
||||
last_cmd_time+100 > board_millis()) {
|
||||
return ctap_error(CTAP1_ERR_CHANNEL_BUSY);
|
||||
if (FRAME_TYPE(ctap_req) == TYPE_INIT)
|
||||
{
|
||||
if (MSG_LEN(ctap_req) > CTAP_MAX_PACKET_SIZE)
|
||||
}
|
||||
if (FRAME_TYPE(ctap_req) == TYPE_INIT) {
|
||||
if (MSG_LEN(ctap_req) > CTAP_MAX_PACKET_SIZE) {
|
||||
return ctap_error(CTAP1_ERR_INVALID_LEN);
|
||||
if (msg_packet.len > 0 && last_cmd_time+100 > board_millis() && ctap_req->init.cmd != CTAPHID_INIT) {
|
||||
if (last_req.cid != ctap_req->cid) //We are in a transaction
|
||||
}
|
||||
if (msg_packet.len > 0 && last_cmd_time+100 > board_millis() &&
|
||||
ctap_req->init.cmd != CTAPHID_INIT) {
|
||||
if (last_req.cid != ctap_req->cid) { //We are in a transaction
|
||||
return ctap_error(CTAP1_ERR_CHANNEL_BUSY);
|
||||
else
|
||||
} else {
|
||||
return ctap_error(CTAP1_ERR_INVALID_SEQ);
|
||||
}
|
||||
}
|
||||
printf("command %x\n", FRAME_CMD(ctap_req));
|
||||
printf("len %d\n", MSG_LEN(ctap_req));
|
||||
msg_packet.len = msg_packet.current_len = 0;
|
||||
if (MSG_LEN(ctap_req) > 64 - 7)
|
||||
{
|
||||
if (MSG_LEN(ctap_req) > 64 - 7) {
|
||||
msg_packet.len = MSG_LEN(ctap_req);
|
||||
memcpy(msg_packet.data + msg_packet.current_len, ctap_req->init.data, 64-7);
|
||||
msg_packet.current_len += 64 - 7;
|
||||
@@ -265,29 +298,31 @@ int driver_process_usb_packet_hid(uint16_t read) {
|
||||
last_cmd = ctap_req->init.cmd;
|
||||
last_seq = 0;
|
||||
last_cmd_time = board_millis();
|
||||
}
|
||||
else {
|
||||
if (msg_packet.len == 0) //Received a cont with a prior init pkt
|
||||
} else {
|
||||
if (msg_packet.len == 0) { //Received a cont with a prior init pkt
|
||||
return 0;
|
||||
if (last_seq != ctap_req->cont.seq)
|
||||
}
|
||||
if (last_seq != ctap_req->cont.seq) {
|
||||
return ctap_error(CTAP1_ERR_INVALID_SEQ);
|
||||
}
|
||||
if (last_req.cid == ctap_req->cid) {
|
||||
memcpy(msg_packet.data + msg_packet.current_len, ctap_req->cont.data, MIN(64 - 5, msg_packet.len - msg_packet.current_len));
|
||||
memcpy(msg_packet.data + msg_packet.current_len, ctap_req->cont.data,
|
||||
MIN(64 - 5, msg_packet.len - msg_packet.current_len));
|
||||
msg_packet.current_len += MIN(64 - 5, msg_packet.len - msg_packet.current_len);
|
||||
memcpy(&last_req, ctap_req, sizeof(CTAPHID_FRAME));
|
||||
last_seq++;
|
||||
}
|
||||
else if (last_cmd_time+100 > board_millis())
|
||||
} else if (last_cmd_time+100 > board_millis()) {
|
||||
return ctap_error(CTAP1_ERR_CHANNEL_BUSY);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (ctap_req->init.cmd == CTAPHID_INIT) {
|
||||
init_fido();
|
||||
ctap_resp = (CTAPHID_FRAME *)usb_get_tx(ITF_HID);
|
||||
ctap_resp = (CTAPHID_FRAME *) usb_get_tx(ITF_HID);
|
||||
memset(ctap_resp, 0, 64);
|
||||
CTAPHID_INIT_REQ *req = (CTAPHID_INIT_REQ *)ctap_req->init.data;
|
||||
CTAPHID_INIT_RESP *resp = (CTAPHID_INIT_RESP *)ctap_resp->init.data;
|
||||
CTAPHID_INIT_REQ *req = (CTAPHID_INIT_REQ *) ctap_req->init.data;
|
||||
CTAPHID_INIT_RESP *resp = (CTAPHID_INIT_RESP *) ctap_resp->init.data;
|
||||
memcpy(resp->nonce, req->nonce, sizeof(resp->nonce));
|
||||
resp->cid = 0x01000000;
|
||||
resp->versionInterface = CTAPHID_IF_VERSION;
|
||||
@@ -302,25 +337,24 @@ int driver_process_usb_packet_hid(uint16_t read) {
|
||||
hid_write(64);
|
||||
msg_packet.len = msg_packet.current_len = 0;
|
||||
last_packet_time = 0;
|
||||
}
|
||||
else if (ctap_req->init.cmd == CTAPHID_WINK) {
|
||||
} else if (ctap_req->init.cmd == CTAPHID_WINK) {
|
||||
if (MSG_LEN(ctap_req) != 0) {
|
||||
return ctap_error(CTAP1_ERR_INVALID_LEN);
|
||||
}
|
||||
ctap_resp = (CTAPHID_FRAME *)usb_get_tx(ITF_HID);
|
||||
ctap_resp = (CTAPHID_FRAME *) usb_get_tx(ITF_HID);
|
||||
memcpy(ctap_resp, ctap_req, sizeof(CTAPHID_FRAME));
|
||||
sleep_ms(1000); //For blinking the device during 1 seg
|
||||
hid_write(64);
|
||||
msg_packet.len = msg_packet.current_len = 0;
|
||||
last_packet_time = 0;
|
||||
}
|
||||
else if ((last_cmd == CTAPHID_PING || last_cmd == CTAPHID_SYNC) && (msg_packet.len == 0 || (msg_packet.len == msg_packet.current_len && msg_packet.len > 0))) {
|
||||
ctap_resp = (CTAPHID_FRAME *)usb_get_tx(ITF_HID);
|
||||
} else if ((last_cmd == CTAPHID_PING || last_cmd == CTAPHID_SYNC) &&
|
||||
(msg_packet.len == 0 ||
|
||||
(msg_packet.len == msg_packet.current_len && msg_packet.len > 0))) {
|
||||
ctap_resp = (CTAPHID_FRAME *) usb_get_tx(ITF_HID);
|
||||
if (msg_packet.current_len == msg_packet.len && msg_packet.len > 0) {
|
||||
memcpy(ctap_resp->init.data, msg_packet.data, msg_packet.len);
|
||||
driver_exec_finished_hid(msg_packet.len);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
memcpy(ctap_resp->init.data, ctap_req->init.data, MSG_LEN(ctap_req));
|
||||
ctap_resp->cid = ctap_req->cid;
|
||||
ctap_resp->init.cmd = last_cmd;
|
||||
@@ -330,24 +364,27 @@ int driver_process_usb_packet_hid(uint16_t read) {
|
||||
}
|
||||
msg_packet.len = msg_packet.current_len = 0;
|
||||
last_packet_time = 0;
|
||||
}
|
||||
else if (ctap_req->init.cmd == CTAPHID_LOCK) {
|
||||
if (MSG_LEN(ctap_req) != 1)
|
||||
} else if (ctap_req->init.cmd == CTAPHID_LOCK) {
|
||||
if (MSG_LEN(ctap_req) != 1) {
|
||||
return ctap_error(CTAP1_ERR_INVALID_LEN);
|
||||
if (ctap_req->init.data[0] > 10)
|
||||
}
|
||||
if (ctap_req->init.data[0] > 10) {
|
||||
return ctap_error(CTAP1_ERR_INVALID_PARAMETER);
|
||||
}
|
||||
lock = board_millis() + ctap_req->init.data[0] * 1000;
|
||||
ctap_resp = (CTAPHID_FRAME *)usb_get_tx(ITF_HID);
|
||||
ctap_resp = (CTAPHID_FRAME *) usb_get_tx(ITF_HID);
|
||||
memset(ctap_resp, 0, 64);
|
||||
ctap_resp->cid = ctap_req->cid;
|
||||
ctap_resp->init.cmd = ctap_req->init.cmd;
|
||||
hid_write(64);
|
||||
msg_packet.len = msg_packet.current_len = 0;
|
||||
last_packet_time = 0;
|
||||
}
|
||||
else if (last_cmd == CTAPHID_MSG && (msg_packet.len == 0 || (msg_packet.len == msg_packet.current_len && msg_packet.len > 0))) {
|
||||
} else if (last_cmd == CTAPHID_MSG &&
|
||||
(msg_packet.len == 0 ||
|
||||
(msg_packet.len == msg_packet.current_len && msg_packet.len > 0))) {
|
||||
|
||||
if (current_app == NULL || memcmp(current_app->aid, fido_aid+1, MIN(current_app->aid[0], fido_aid[0])) != 0) {
|
||||
if (current_app == NULL ||
|
||||
memcmp(current_app->aid, fido_aid+1, MIN(current_app->aid[0], fido_aid[0])) != 0) {
|
||||
for (int a = 0; a < num_apps; a++) {
|
||||
if ((current_app = apps[a].select_aid(&apps[a], fido_aid+1, fido_aid[0]))) {
|
||||
break;
|
||||
@@ -355,40 +392,44 @@ int driver_process_usb_packet_hid(uint16_t read) {
|
||||
}
|
||||
}
|
||||
//if (thread_type != 1)
|
||||
card_start(apdu_thread);
|
||||
card_start(apdu_thread);
|
||||
thread_type = 1;
|
||||
|
||||
if (msg_packet.current_len == msg_packet.len && msg_packet.len > 0)
|
||||
if (msg_packet.current_len == msg_packet.len && msg_packet.len > 0) {
|
||||
apdu_sent = apdu_process(ITF_HID, msg_packet.data, msg_packet.len);
|
||||
else
|
||||
} else {
|
||||
apdu_sent = apdu_process(ITF_HID, ctap_req->init.data, MSG_LEN(ctap_req));
|
||||
DEBUG_PAYLOAD(apdu.data, (int)apdu.nc);
|
||||
}
|
||||
DEBUG_PAYLOAD(apdu.data, (int) apdu.nc);
|
||||
msg_packet.len = msg_packet.current_len = 0;
|
||||
last_packet_time = 0;
|
||||
}
|
||||
else if ((last_cmd == CTAPHID_CBOR || (last_cmd >= CTAPHID_VENDOR_FIRST && last_cmd <= CTAPHID_VENDOR_LAST)) && (msg_packet.len == 0 || (msg_packet.len == msg_packet.current_len && msg_packet.len > 0))) {
|
||||
} else if ((last_cmd == CTAPHID_CBOR ||
|
||||
(last_cmd >= CTAPHID_VENDOR_FIRST && last_cmd <= CTAPHID_VENDOR_LAST)) &&
|
||||
(msg_packet.len == 0 ||
|
||||
(msg_packet.len == msg_packet.current_len && msg_packet.len > 0))) {
|
||||
|
||||
//if (thread_type != 2)
|
||||
card_start(cbor_thread);
|
||||
card_start(cbor_thread);
|
||||
thread_type = 2;
|
||||
if (msg_packet.current_len == msg_packet.len && msg_packet.len > 0)
|
||||
if (msg_packet.current_len == msg_packet.len && msg_packet.len > 0) {
|
||||
apdu_sent = cbor_process(last_cmd, msg_packet.data, msg_packet.len);
|
||||
else
|
||||
} else {
|
||||
apdu_sent = cbor_process(last_cmd, ctap_req->init.data, MSG_LEN(ctap_req));
|
||||
}
|
||||
msg_packet.len = msg_packet.current_len = 0;
|
||||
last_packet_time = 0;
|
||||
if (apdu_sent < 0)
|
||||
if (apdu_sent < 0) {
|
||||
return ctap_error(-apdu_sent);
|
||||
}
|
||||
else if (ctap_req->init.cmd == CTAPHID_CANCEL) {
|
||||
}
|
||||
} else if (ctap_req->init.cmd == CTAPHID_CANCEL) {
|
||||
ctap_error(0x2D);
|
||||
msg_packet.len = msg_packet.current_len = 0;
|
||||
last_packet_time = 0;
|
||||
cancel_button = true;
|
||||
}
|
||||
else {
|
||||
if (msg_packet.len == 0)
|
||||
} else {
|
||||
if (msg_packet.len == 0) {
|
||||
return ctap_error(CTAP1_ERR_INVALID_CMD);
|
||||
}
|
||||
}
|
||||
// echo back anything we received from host
|
||||
//tud_hid_report(0, buffer, bufsize);
|
||||
@@ -398,8 +439,9 @@ int driver_process_usb_packet_hid(uint16_t read) {
|
||||
return apdu_sent;
|
||||
}
|
||||
|
||||
void send_keepalive() {
|
||||
CTAPHID_FRAME *resp = (CTAPHID_FRAME *)(usb_get_tx(ITF_HID) + 4096);
|
||||
void send_keepalive()
|
||||
{
|
||||
CTAPHID_FRAME *resp = (CTAPHID_FRAME *) (usb_get_tx(ITF_HID) + 4096);
|
||||
//memset(ctap_resp, 0, sizeof(CTAPHID_FRAME));
|
||||
resp->cid = ctap_req->cid;
|
||||
resp->init.cmd = CTAPHID_KEEPALIVE;
|
||||
@@ -409,39 +451,45 @@ void send_keepalive() {
|
||||
hid_write_offset(64, 4096);
|
||||
}
|
||||
|
||||
void driver_exec_timeout_hid() {
|
||||
if (thread_type == 2)
|
||||
void driver_exec_timeout_hid()
|
||||
{
|
||||
if (thread_type == 2) {
|
||||
send_keepalive();
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t *driver_prepare_response_hid() {
|
||||
ctap_resp = (CTAPHID_FRAME *)usb_get_tx(ITF_HID);
|
||||
uint8_t *driver_prepare_response_hid()
|
||||
{
|
||||
ctap_resp = (CTAPHID_FRAME *) usb_get_tx(ITF_HID);
|
||||
apdu.rdata = ctap_resp->init.data;
|
||||
send_buffer_size = 0;
|
||||
memset(usb_get_tx(ITF_HID), 0, 4096);
|
||||
return ctap_resp->init.data;
|
||||
}
|
||||
|
||||
void driver_exec_finished_hid(size_t size_next) {
|
||||
void driver_exec_finished_hid(size_t size_next)
|
||||
{
|
||||
if (size_next > 0) {
|
||||
if (thread_type == 2 && apdu.sw != 0)
|
||||
if (thread_type == 2 && apdu.sw != 0) {
|
||||
ctap_error(apdu.sw & 0xff);
|
||||
else
|
||||
} else {
|
||||
driver_exec_finished_cont_hid(size_next, 7);
|
||||
}
|
||||
}
|
||||
apdu.sw = 0;
|
||||
}
|
||||
|
||||
void driver_exec_finished_cont_hid(size_t size_next, size_t offset) {
|
||||
void driver_exec_finished_cont_hid(size_t size_next, size_t offset)
|
||||
{
|
||||
offset -= 7;
|
||||
ctap_resp = (CTAPHID_FRAME *)(usb_get_tx(ITF_HID) + offset);
|
||||
ctap_resp = (CTAPHID_FRAME *) (usb_get_tx(ITF_HID) + offset);
|
||||
ctap_resp->cid = ctap_req->cid;
|
||||
ctap_resp->init.cmd = last_cmd;
|
||||
ctap_resp->init.bcnth = size_next >> 8;
|
||||
ctap_resp->init.bcntl = size_next & 0xff;
|
||||
send_buffer_size = size_next;
|
||||
if (hid_write_offset(64, offset) > 0) {
|
||||
ctap_resp = (CTAPHID_FRAME *)((uint8_t *)ctap_resp + 64 - 5);
|
||||
ctap_resp = (CTAPHID_FRAME *) ((uint8_t *) ctap_resp + 64 - 5);
|
||||
|
||||
send_buffer_size -= MIN(64-7, send_buffer_size);
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
#include "usb.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
@@ -55,7 +55,8 @@
|
||||
#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 || \
|
||||
#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
|
||||
@@ -88,7 +89,7 @@
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUSB_MEM_ALIGN
|
||||
#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4)))
|
||||
#define CFG_TUSB_MEM_ALIGN __attribute__((aligned(4)))
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
@@ -121,8 +122,7 @@
|
||||
#define CFG_TUD_HID_EP_BUFSIZE 64
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_CONFIG_H_ */
|
||||
|
||||
|
||||
148
src/usb/usb.c
148
src/usb/usb.c
@@ -1,4 +1,3 @@
|
||||
|
||||
/*
|
||||
* This file is part of the Pico HSM SDK distribution (https://github.com/polhenarejos/pico-hsm-sdk).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
@@ -34,76 +33,89 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
// Device specific functions
|
||||
static uint8_t rx_buffer[ITF_TOTAL][4096] = {0}, tx_buffer[ITF_TOTAL][4096+64] = {0};
|
||||
static uint16_t w_offset[ITF_TOTAL] = {0}, r_offset[ITF_TOTAL] = {0};
|
||||
static uint16_t w_len[ITF_TOTAL] = {0}, tx_r_offset[ITF_TOTAL] = {0};
|
||||
static uint32_t timeout_counter[ITF_TOTAL] = {0};
|
||||
static uint8_t rx_buffer[ITF_TOTAL][4096] = { 0 }, tx_buffer[ITF_TOTAL][4096+64] = { 0 };
|
||||
static uint16_t w_offset[ITF_TOTAL] = { 0 }, r_offset[ITF_TOTAL] = { 0 };
|
||||
static uint16_t w_len[ITF_TOTAL] = { 0 }, tx_r_offset[ITF_TOTAL] = { 0 };
|
||||
static uint32_t timeout_counter[ITF_TOTAL] = { 0 };
|
||||
uint8_t card_locked_itf = ITF_TOTAL; // no locked
|
||||
|
||||
void usb_set_timeout_counter(uint8_t itf, uint32_t v) {
|
||||
void usb_set_timeout_counter(uint8_t itf, uint32_t v)
|
||||
{
|
||||
timeout_counter[itf] = v;
|
||||
}
|
||||
|
||||
uint32_t usb_write_offset(uint8_t itf, uint16_t len, uint16_t offset) {
|
||||
uint32_t usb_write_offset(uint8_t itf, uint16_t len, uint16_t offset)
|
||||
{
|
||||
#ifndef ENABLE_EMULATION
|
||||
uint8_t pkt_max = 64;
|
||||
#endif
|
||||
int w = 0;
|
||||
if (len > sizeof(tx_buffer[itf]))
|
||||
if (len > sizeof(tx_buffer[itf])) {
|
||||
len = sizeof(tx_buffer[itf]);
|
||||
}
|
||||
w_len[itf] = len;
|
||||
tx_r_offset[itf] = offset;
|
||||
#ifdef USB_ITF_HID
|
||||
if (itf == ITF_HID)
|
||||
if (itf == ITF_HID) {
|
||||
w = driver_write_hid(tx_buffer[itf]+offset, MIN(len, pkt_max));
|
||||
}
|
||||
#endif
|
||||
#ifdef USB_ITF_CCID
|
||||
if (itf == ITF_CCID)
|
||||
if (itf == ITF_CCID) {
|
||||
w = driver_write_ccid(tx_buffer[itf]+offset, MIN(len, pkt_max));
|
||||
}
|
||||
#endif
|
||||
#ifdef ENABLE_EMULATION
|
||||
if (itf == ITF_EMUL)
|
||||
if (itf == ITF_EMUL) {
|
||||
w = driver_write_emul(tx_buffer[itf]+offset, len);
|
||||
}
|
||||
#endif
|
||||
w_len[itf] -= w;
|
||||
tx_r_offset[itf] += w;
|
||||
return w;
|
||||
}
|
||||
|
||||
size_t usb_rx(uint8_t itf, const uint8_t *buffer, size_t len) {
|
||||
size_t usb_rx(uint8_t itf, const uint8_t *buffer, size_t len)
|
||||
{
|
||||
uint16_t size = MIN(sizeof(rx_buffer[itf]) - w_offset[itf], len);
|
||||
if (size > 0) {
|
||||
if (buffer == NULL) {
|
||||
#ifdef USB_ITF_HID
|
||||
if (itf == ITF_HID)
|
||||
if (itf == ITF_HID) {
|
||||
size = driver_read_hid(rx_buffer[itf] + w_offset[itf], size);
|
||||
}
|
||||
#endif
|
||||
#ifdef USB_ITF_CCID
|
||||
if (itf == ITF_CCID)
|
||||
if (itf == ITF_CCID) {
|
||||
size = driver_read_ccid(rx_buffer[itf] + w_offset[itf], size);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
else
|
||||
} else {
|
||||
memcpy(rx_buffer[itf] + w_offset[itf], buffer, size);
|
||||
}
|
||||
w_offset[itf] += size;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
uint32_t usb_write_flush(uint8_t itf) {
|
||||
uint32_t usb_write_flush(uint8_t itf)
|
||||
{
|
||||
int w = 0;
|
||||
if (w_len[itf] > 0) {
|
||||
#ifdef USB_ITF_HID
|
||||
if (itf == ITF_HID)
|
||||
if (itf == ITF_HID) {
|
||||
w = driver_write_hid(tx_buffer[itf]+tx_r_offset[itf], MIN(w_len[itf], 64));
|
||||
}
|
||||
#endif
|
||||
#ifdef USB_ITF_CCID
|
||||
if (itf == ITF_CCID)
|
||||
if (itf == ITF_CCID) {
|
||||
w = driver_write_ccid(tx_buffer[itf]+tx_r_offset[itf], MIN(w_len[itf], 64));
|
||||
}
|
||||
#endif
|
||||
#ifdef ENABLE_EMULATION
|
||||
if (itf == ITF_EMUL)
|
||||
if (itf == ITF_EMUL) {
|
||||
w = driver_write_emul(tx_buffer[itf]+tx_r_offset[itf], w_len[itf]);
|
||||
}
|
||||
#endif
|
||||
tx_r_offset[itf] += w;
|
||||
w_len[itf] -= w;
|
||||
@@ -111,27 +123,33 @@ uint32_t usb_write_flush(uint8_t itf) {
|
||||
return w;
|
||||
}
|
||||
|
||||
uint32_t usb_write(uint8_t itf, uint16_t len) {
|
||||
uint32_t usb_write(uint8_t itf, uint16_t len)
|
||||
{
|
||||
return usb_write_offset(itf, len, 0);
|
||||
}
|
||||
|
||||
uint16_t usb_read_available(uint8_t itf) {
|
||||
uint16_t usb_read_available(uint8_t itf)
|
||||
{
|
||||
return w_offset[itf] - r_offset[itf];
|
||||
}
|
||||
|
||||
uint16_t usb_write_available(uint8_t itf) {
|
||||
uint16_t usb_write_available(uint8_t itf)
|
||||
{
|
||||
return w_len[itf] > 0;
|
||||
}
|
||||
|
||||
uint8_t *usb_get_rx(uint8_t itf) {
|
||||
uint8_t *usb_get_rx(uint8_t itf)
|
||||
{
|
||||
return rx_buffer[itf];
|
||||
}
|
||||
|
||||
uint8_t *usb_get_tx(uint8_t itf) {
|
||||
uint8_t *usb_get_tx(uint8_t itf)
|
||||
{
|
||||
return tx_buffer[itf];
|
||||
}
|
||||
|
||||
void usb_clear_rx(uint8_t itf) {
|
||||
void usb_clear_rx(uint8_t itf)
|
||||
{
|
||||
w_offset[itf] = r_offset[itf] = 0;
|
||||
}
|
||||
|
||||
@@ -149,7 +167,8 @@ queue_t usb_to_card_q;
|
||||
queue_t card_to_usb_q;
|
||||
#endif
|
||||
|
||||
void usb_init() {
|
||||
void usb_init()
|
||||
{
|
||||
#ifndef ENABLE_EMULATION
|
||||
queue_init(&card_to_usb_q, sizeof(uint32_t), 64);
|
||||
queue_init(&usb_to_card_q, sizeof(uint32_t), 64);
|
||||
@@ -159,7 +178,8 @@ void usb_init() {
|
||||
extern int driver_process_usb_nopacket();
|
||||
extern uint32_t timeout;
|
||||
|
||||
static int usb_event_handle(uint8_t itf) {
|
||||
static int usb_event_handle(uint8_t itf)
|
||||
{
|
||||
#ifndef ENABLE_EMULATION
|
||||
uint16_t rx_read = usb_read_available(itf);
|
||||
#else
|
||||
@@ -167,16 +187,19 @@ static int usb_event_handle(uint8_t itf) {
|
||||
#endif
|
||||
int proc_packet = 0;
|
||||
#ifdef USB_ITF_HID
|
||||
if (itf == ITF_HID)
|
||||
if (itf == ITF_HID) {
|
||||
proc_packet = driver_process_usb_packet_hid(rx_read);
|
||||
}
|
||||
#endif
|
||||
#ifdef USB_ITF_CCID
|
||||
if (itf == ITF_CCID)
|
||||
if (itf == ITF_CCID) {
|
||||
proc_packet = driver_process_usb_packet_ccid(rx_read);
|
||||
}
|
||||
#endif
|
||||
#ifdef ENABLE_EMULATION
|
||||
if (itf == ITF_EMUL)
|
||||
if (itf == ITF_EMUL) {
|
||||
proc_packet = driver_process_usb_packet_emul(rx_read);
|
||||
}
|
||||
#endif
|
||||
if (proc_packet > 0) {
|
||||
card_locked_itf = itf;
|
||||
@@ -185,22 +208,24 @@ static int usb_event_handle(uint8_t itf) {
|
||||
queue_add_blocking(&usb_to_card_q, &flag);
|
||||
#endif
|
||||
timeout_start();
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
#ifdef USB_ITF_HID
|
||||
if (itf == ITF_HID)
|
||||
if (itf == ITF_HID) {
|
||||
driver_process_usb_nopacket_hid();
|
||||
}
|
||||
#endif
|
||||
#ifdef USB_ITF_CCID
|
||||
if (itf == ITF_CCID)
|
||||
if (itf == ITF_CCID) {
|
||||
driver_process_usb_nopacket_ccid();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
extern void low_flash_init();
|
||||
void card_init_core1() {
|
||||
void card_init_core1()
|
||||
{
|
||||
#ifndef ENABLE_EMULATION
|
||||
low_flash_init_core1();
|
||||
#endif
|
||||
@@ -208,16 +233,19 @@ void card_init_core1() {
|
||||
|
||||
size_t finished_data_size = 0;
|
||||
|
||||
void card_start(void (*func)(void)) {
|
||||
void card_start(void (*func)(void))
|
||||
{
|
||||
#ifndef ENABLE_EMULATION
|
||||
uint32_t m = 0;
|
||||
while (queue_is_empty(&usb_to_card_q) == false) {
|
||||
if (queue_try_remove(&usb_to_card_q, &m) == false)
|
||||
if (queue_try_remove(&usb_to_card_q, &m) == false) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
while (queue_is_empty(&card_to_usb_q) == false) {
|
||||
if (queue_try_remove(&card_to_usb_q, &m) == false)
|
||||
if (queue_try_remove(&card_to_usb_q, &m) == false) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
multicore_reset_core1();
|
||||
multicore_launch_core1(func);
|
||||
@@ -225,7 +253,8 @@ void card_start(void (*func)(void)) {
|
||||
#endif
|
||||
}
|
||||
|
||||
void card_exit() {
|
||||
void card_exit()
|
||||
{
|
||||
#ifndef ENABLE_EMULATION
|
||||
uint32_t flag = EV_EXIT;
|
||||
queue_try_add(&usb_to_card_q, &flag);
|
||||
@@ -234,7 +263,8 @@ void card_exit() {
|
||||
card_locked_itf = ITF_TOTAL;
|
||||
}
|
||||
extern void hid_task();
|
||||
void usb_task() {
|
||||
void usb_task()
|
||||
{
|
||||
#ifndef ENABLE_EMULATION
|
||||
bool mounted = false;
|
||||
#else
|
||||
@@ -242,12 +272,14 @@ void usb_task() {
|
||||
#endif
|
||||
for (uint8_t itf = 0; itf < ITF_TOTAL; itf++) {
|
||||
#ifdef USB_ITF_HID
|
||||
if (itf == ITF_HID)
|
||||
if (itf == ITF_HID) {
|
||||
mounted = driver_mounted_hid();
|
||||
}
|
||||
#endif
|
||||
#ifdef USB_ITF_CCID
|
||||
if (itf == ITF_CCID)
|
||||
if (itf == ITF_CCID) {
|
||||
mounted = driver_mounted_ccid();
|
||||
}
|
||||
#endif
|
||||
|
||||
if (mounted == true) {
|
||||
@@ -265,31 +297,33 @@ void usb_task() {
|
||||
if (m == EV_EXEC_FINISHED) {
|
||||
timeout_stop();
|
||||
#ifdef USB_ITF_HID
|
||||
if (itf == ITF_HID)
|
||||
if (itf == ITF_HID) {
|
||||
driver_exec_finished_hid(finished_data_size);
|
||||
}
|
||||
#endif
|
||||
#ifdef USB_ITF_CCID
|
||||
if (itf == ITF_CCID)
|
||||
if (itf == ITF_CCID) {
|
||||
driver_exec_finished_ccid(finished_data_size);
|
||||
}
|
||||
#endif
|
||||
led_set_blink(BLINK_MOUNTED);
|
||||
card_locked_itf = ITF_TOTAL;
|
||||
}
|
||||
else if (m == EV_PRESS_BUTTON) {
|
||||
} else if (m == EV_PRESS_BUTTON) {
|
||||
uint32_t flag = wait_button() ? EV_BUTTON_TIMEOUT : EV_BUTTON_PRESSED;
|
||||
queue_try_add(&usb_to_card_q, &flag);
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (timeout > 0) {
|
||||
if (timeout + timeout_counter[itf] < board_millis()) {
|
||||
#ifdef USB_ITF_HID
|
||||
if (itf == ITF_HID)
|
||||
if (itf == ITF_HID) {
|
||||
driver_exec_timeout_hid();
|
||||
}
|
||||
#endif
|
||||
#ifdef USB_ITF_CCID
|
||||
if (itf == ITF_CCID)
|
||||
if (itf == ITF_CCID) {
|
||||
driver_exec_timeout_ccid();
|
||||
}
|
||||
#endif
|
||||
timeout = board_millis();
|
||||
}
|
||||
@@ -305,18 +339,22 @@ void usb_task() {
|
||||
}
|
||||
|
||||
|
||||
uint8_t *usb_prepare_response(uint8_t itf) {
|
||||
uint8_t *usb_prepare_response(uint8_t itf)
|
||||
{
|
||||
#ifdef USB_ITF_HID
|
||||
if (itf == ITF_HID)
|
||||
if (itf == ITF_HID) {
|
||||
return driver_prepare_response_hid();
|
||||
}
|
||||
#endif
|
||||
#ifdef USB_ITF_CCID
|
||||
if (itf == ITF_CCID)
|
||||
if (itf == ITF_CCID) {
|
||||
return driver_prepare_response_ccid();
|
||||
}
|
||||
#endif
|
||||
#ifdef ENABLE_EMULATION
|
||||
if (itf == ITF_EMUL)
|
||||
if (itf == ITF_EMUL) {
|
||||
return driver_prepare_response_emul();
|
||||
}
|
||||
#endif
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -56,8 +56,7 @@ enum {
|
||||
ITF_TOTAL
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
enum {
|
||||
REPORT_ID_KEYBOARD = 1,
|
||||
REPORT_ID_COUNT
|
||||
};
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
|
||||
#define USB_CONFIG_ATT_ONE TU_BIT(7)
|
||||
|
||||
#define MAX_USB_POWER 1
|
||||
#define MAX_USB_POWER 1
|
||||
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
@@ -60,7 +60,7 @@ tusb_desc_device_t const desc_device =
|
||||
.bNumConfigurations = 1
|
||||
};
|
||||
|
||||
uint8_t const * tud_descriptor_device_cb(void)
|
||||
uint8_t const *tud_descriptor_device_cb(void)
|
||||
{
|
||||
return (uint8_t const *) &desc_device;
|
||||
}
|
||||
@@ -73,20 +73,21 @@ uint8_t const * tud_descriptor_device_cb(void)
|
||||
tusb_desc_configuration_t const desc_config =
|
||||
{
|
||||
.bLength = sizeof(tusb_desc_configuration_t),
|
||||
.bDescriptorType = TUSB_DESC_CONFIGURATION,
|
||||
.bDescriptorType = TUSB_DESC_CONFIGURATION,
|
||||
.wTotalLength = (sizeof(tusb_desc_configuration_t)
|
||||
#ifdef USB_ITF_CCID
|
||||
+ sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) + 2*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
|
||||
+ 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),
|
||||
),
|
||||
.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
|
||||
@@ -97,8 +98,8 @@ static const struct ccid_class_descriptor desc_ccid = {
|
||||
.bMaxSlotIndex = 0,
|
||||
.bVoltageSupport = 0x01, // 5.0V
|
||||
.dwProtocols = (
|
||||
0x01| // T=0
|
||||
0x02), // T=1
|
||||
0x01| // T=0
|
||||
0x02), // T=1
|
||||
.dwDefaultClock = (0xDFC),
|
||||
.dwMaximumClock = (0xDFC),
|
||||
.bNumClockSupport = 0,
|
||||
@@ -133,64 +134,65 @@ tusb_desc_interface_t const desc_interface =
|
||||
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
|
||||
.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
|
||||
.bDescriptorType = TUSB_DESC_ENDPOINT,
|
||||
.bEndpointAddress = 2,
|
||||
.bmAttributes.xfer = TUSB_XFER_BULK,
|
||||
.wMaxPacketSize.size = (64),
|
||||
.bInterval = 0
|
||||
};
|
||||
#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) + 2*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
|
||||
+ TUD_HID_INOUT_DESC_LEN + TUD_HID_DESC_LEN
|
||||
#endif
|
||||
];
|
||||
|
||||
#ifdef USB_ITF_HID
|
||||
#define HID_USAGE_PAGE_FIDO 0xF1D0
|
||||
|
||||
enum
|
||||
{
|
||||
HID_USAGE_FIDO_U2FHID = 0x01, // U2FHID usage for top-level collection
|
||||
HID_USAGE_FIDO_DATA_IN = 0x20, // Raw IN data report
|
||||
HID_USAGE_FIDO_DATA_OUT = 0x21 // Raw OUT data report
|
||||
enum {
|
||||
HID_USAGE_FIDO_U2FHID = 0x01,// U2FHID usage for top-level collection
|
||||
HID_USAGE_FIDO_DATA_IN = 0x20,// Raw IN data report
|
||||
HID_USAGE_FIDO_DATA_OUT = 0x21 // Raw OUT data report
|
||||
};
|
||||
|
||||
#define TUD_HID_REPORT_DESC_FIDO_U2F(report_size, ...) \
|
||||
HID_USAGE_PAGE_N ( HID_USAGE_PAGE_FIDO, 2 ) ,\
|
||||
HID_USAGE ( HID_USAGE_FIDO_U2FHID ) ,\
|
||||
HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\
|
||||
HID_USAGE_PAGE_N(HID_USAGE_PAGE_FIDO, 2), \
|
||||
HID_USAGE(HID_USAGE_FIDO_U2FHID), \
|
||||
HID_COLLECTION(HID_COLLECTION_APPLICATION), \
|
||||
/* Report ID if any */ \
|
||||
__VA_ARGS__ \
|
||||
/* Usage Data In */ \
|
||||
HID_USAGE ( HID_USAGE_FIDO_DATA_IN ) ,\
|
||||
HID_LOGICAL_MIN ( 0 ) ,\
|
||||
HID_LOGICAL_MAX_N ( 0xff, 2 ) ,\
|
||||
HID_REPORT_SIZE ( 8 ) ,\
|
||||
HID_REPORT_COUNT ( report_size ) ,\
|
||||
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
|
||||
HID_USAGE(HID_USAGE_FIDO_DATA_IN), \
|
||||
HID_LOGICAL_MIN(0), \
|
||||
HID_LOGICAL_MAX_N(0xff, 2), \
|
||||
HID_REPORT_SIZE(8), \
|
||||
HID_REPORT_COUNT(report_size), \
|
||||
HID_INPUT(HID_DATA | HID_VARIABLE | HID_ABSOLUTE), \
|
||||
/* Usage Data Out */ \
|
||||
HID_USAGE ( HID_USAGE_FIDO_DATA_OUT ) ,\
|
||||
HID_LOGICAL_MIN ( 0 ) ,\
|
||||
HID_LOGICAL_MAX_N ( 0xff, 2 ) ,\
|
||||
HID_REPORT_SIZE ( 8 ) ,\
|
||||
HID_REPORT_COUNT ( report_size ) ,\
|
||||
HID_OUTPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
|
||||
HID_COLLECTION_END \
|
||||
HID_USAGE(HID_USAGE_FIDO_DATA_OUT), \
|
||||
HID_LOGICAL_MIN(0), \
|
||||
HID_LOGICAL_MAX_N(0xff, 2), \
|
||||
HID_REPORT_SIZE(8), \
|
||||
HID_REPORT_COUNT(report_size), \
|
||||
HID_OUTPUT(HID_DATA | HID_VARIABLE | HID_ABSOLUTE), \
|
||||
HID_COLLECTION_END \
|
||||
|
||||
uint8_t const desc_hid_report[] =
|
||||
{
|
||||
@@ -203,46 +205,57 @@ uint8_t const desc_hid_report_kb[] =
|
||||
#define EPNUM_HID 0x03
|
||||
|
||||
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)
|
||||
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)
|
||||
};
|
||||
|
||||
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)
|
||||
TUD_HID_DESCRIPTOR(ITF_KEYBOARD, ITF_KEYBOARD+5, HID_ITF_PROTOCOL_NONE,
|
||||
sizeof(desc_hid_report_kb), 0x80 | (EPNUM_HID+1), 16, 5)
|
||||
};
|
||||
|
||||
uint8_t const * tud_hid_descriptor_report_cb(uint8_t itf)
|
||||
uint8_t const *tud_hid_descriptor_report_cb(uint8_t itf)
|
||||
{
|
||||
printf("report_cb %d\n", itf);
|
||||
if (itf == ITF_HID)
|
||||
if (itf == ITF_HID) {
|
||||
return desc_hid_report;
|
||||
else if (itf == ITF_KEYBOARD)
|
||||
} else if (itf == ITF_KEYBOARD) {
|
||||
return desc_hid_report_kb;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
|
||||
uint8_t const *tud_descriptor_configuration_cb(uint8_t index)
|
||||
{
|
||||
(void) index; // for multiple configurations
|
||||
|
||||
static uint8_t initd = 0;
|
||||
if (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_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_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);
|
||||
#endif
|
||||
initd = 1;
|
||||
}
|
||||
return (const uint8_t *)desc_config_extended;
|
||||
return (const uint8_t *) desc_config_extended;
|
||||
}
|
||||
|
||||
#define BOS_TOTAL_LEN (TUD_BOS_DESC_LEN)
|
||||
@@ -255,7 +268,7 @@ uint8_t const desc_bos[] =
|
||||
TUD_BOS_DESCRIPTOR(BOS_TOTAL_LEN, 2)
|
||||
};
|
||||
|
||||
uint8_t const * tud_descriptor_bos_cb(void)
|
||||
uint8_t const *tud_descriptor_bos_cb(void)
|
||||
{
|
||||
return desc_bos;
|
||||
}
|
||||
@@ -265,7 +278,7 @@ uint8_t const * tud_descriptor_bos_cb(void)
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// array of pointer to string descriptors
|
||||
char const* string_desc_arr [] =
|
||||
char const *string_desc_arr [] =
|
||||
{
|
||||
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
||||
"Pol Henarejos", // 1: Manufacturer
|
||||
@@ -273,17 +286,17 @@ char const* string_desc_arr [] =
|
||||
"11223344", // 3: Serials, should use chip ID
|
||||
"Pico Key Config" // 4: Vendor Interface
|
||||
#ifdef USB_ITF_HID
|
||||
,"Pico Key HID Interface"
|
||||
,"Pico Key HID Keyboard Interface"
|
||||
, "Pico Key HID Interface"
|
||||
, "Pico Key HID Keyboard Interface"
|
||||
#endif
|
||||
#ifdef USB_ITF_CCID
|
||||
,"Pico Key CCID Interface"
|
||||
, "Pico Key CCID Interface"
|
||||
#endif
|
||||
};
|
||||
|
||||
static uint16_t _desc_str[32];
|
||||
|
||||
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
||||
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
||||
{
|
||||
(void) langid;
|
||||
|
||||
@@ -292,15 +305,15 @@ uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
||||
if (index == 0) {
|
||||
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
||||
chr_count = 1;
|
||||
}
|
||||
else {
|
||||
} 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])) )
|
||||
if (!(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0]))) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char* str = string_desc_arr[index];
|
||||
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;
|
||||
@@ -310,16 +323,17 @@ uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
||||
}
|
||||
|
||||
chr_count = strlen(str);
|
||||
if ( chr_count > 31 )
|
||||
if (chr_count > 31) {
|
||||
chr_count = 31;
|
||||
}
|
||||
|
||||
// Convert ASCII string into UTF-16
|
||||
for(uint8_t i=0; i<chr_count; i++) {
|
||||
for (uint8_t i = 0; i < chr_count; i++) {
|
||||
_desc_str[1+i] = str[i];
|
||||
}
|
||||
}
|
||||
|
||||
_desc_str[0] = (TUSB_DESC_STRING << 8 ) | (2*chr_count + 2);
|
||||
_desc_str[0] = (TUSB_DESC_STRING << 8) | (2*chr_count + 2);
|
||||
|
||||
return _desc_str;
|
||||
}
|
||||
|
||||
@@ -19,28 +19,28 @@
|
||||
#define USB_DESCRIPTORS_H_
|
||||
|
||||
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__));
|
||||
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_ */
|
||||
|
||||
Reference in New Issue
Block a user