diff --git a/firmware/src_simtrace/card_emu.c b/firmware/src_simtrace/card_emu.c index 5044bea9..e458f1ce 100644 --- a/firmware/src_simtrace/card_emu.c +++ b/firmware/src_simtrace/card_emu.c @@ -17,7 +17,7 @@ * */ -#define TRACE_LEVEL 6 +//#define TRACE_LEVEL 6 #include #include @@ -177,11 +177,10 @@ static void flush_rx_buffer(struct card_handle *ch) rd->data_len = rctx->idx; rd->hdr.msg_len = sizeof(*rd) + rd->data_len; - llist_add_tail(&rctx->list, &ch->usb_tx_queue); req_ctx_set_state(rctx, RCTX_S_USB_TX_PENDING); - - /* FIXME: call into USB code to see if this buffer can - * be transmitted now */ + /* no need for irqsafe operation, as the usb_tx_queue is + * processed only by the main loop context */ + llist_add_tail(&rctx->list, &ch->usb_tx_queue); } /* convert a non-contiguous PTS request/responsei into a contiguous @@ -234,11 +233,10 @@ static void flush_pts(struct card_handle *ch) ptsi->pts_len = serialize_pts(ptsi->req, ch->pts.req); serialize_pts(ptsi->resp, ch->pts.resp); - llist_add_tail(&rctx->list, &ch->usb_tx_queue); req_ctx_set_state(rctx, RCTX_S_USB_TX_PENDING); - - /* FIXME: call into USB code to see if this buffer can - * be transmitted now */ + /* no need for irqsafe operation, as the usb_tx_queue is + * processed only by the main loop context */ + llist_add_tail(&rctx->list, &ch->usb_tx_queue); } static void emu_update_fidi(struct card_handle *ch) @@ -406,6 +404,9 @@ process_byte_pts(struct card_handle *ch, uint8_t byte) flush_pts(ch); /* activate UART TX to transmit PTS response */ card_emu_uart_enable(ch->uart_chan, ENABLE_TX); + /* don't fall-through to the 'return ISO_S_IN_PTS' + * below, rather keep ISO7816 state as-is, it will be + * further updated by the tx-completion handler */ return -1; } @@ -669,6 +670,8 @@ static int tx_byte_tpdu(struct card_handle *ch) /* ensure we are aware of any data that might be pending for * transmit */ if (!ch->uart_tx_ctx) { + /* uart_tx_queue is filled from main loop, so no need + * for irq-safe operations */ if (llist_empty(&ch->uart_tx_queue)) return 0; @@ -715,12 +718,8 @@ static int tx_byte_tpdu(struct card_handle *ch) /* we have transmitted all bytes */ if (td->flags & CEMU_DATA_F_FINAL) { /* this was the final part of the APDU, go - * back to state one*/ + * back to state one */ card_set_state(ch, ISO_S_WAIT_TPDU); - } else { - /* FIXME: call into USB code to chec if we need - * to submit a free buffer to accept - * further data on bulk out endpoint */ } } req_ctx_set_state(rctx, RCTX_S_FREE); @@ -746,7 +745,7 @@ void card_emu_process_rx_byte(struct card_handle *ch, uint8_t byte) case ISO_S_WAIT_CLK: case ISO_S_WAIT_RST: case ISO_S_WAIT_ATR: - TRACE_DEBUG("Received UART char in 7816 state %u\r\n", + TRACE_ERROR("Received UART char in invalid 7816 state %u\r\n", ch->state); /* we shouldn't receive any data from the reader yet! */ break; diff --git a/firmware/src_simtrace/host_communication.c b/firmware/src_simtrace/host_communication.c index dde54033..1c892239 100644 --- a/firmware/src_simtrace/host_communication.c +++ b/firmware/src_simtrace/host_communication.c @@ -79,7 +79,8 @@ static void usb_read_cb(uint8_t *arg, uint8_t status, uint32_t transferred, struct req_ctx *rctx = (struct req_ctx *) arg; struct llist_head *queue = (struct llist_head *) usbep_in_progress[rctx->ep]; - TRACE_DEBUG("%s (EP=%u)\r\n", __func__, rctx->ep); + TRACE_DEBUG("%s (EP=%u, len=%u, q=%p)\r\n", __func__, + rctx->ep, transferred, queue); usbep_in_progress[rctx->ep] = 0; diff --git a/firmware/src_simtrace/mode_cardemu.c b/firmware/src_simtrace/mode_cardemu.c index 0f0d7dc0..82127e17 100644 --- a/firmware/src_simtrace/mode_cardemu.c +++ b/firmware/src_simtrace/mode_cardemu.c @@ -1,4 +1,4 @@ -#define TRACE_LEVEL 6 +//#define TRACE_LEVEL 6 #include "board.h" #include "simtrace.h" @@ -145,16 +145,18 @@ int card_emu_uart_tx(uint8_t uart_chan, uint8_t byte) Usart_info *ui = &usart_info[uart_chan]; ISO7816_SendChar(byte, ui); #else - int i = 0; + int i = 1; while ((usart->US_CSR & (US_CSR_TXRDY)) == 0) { if (!(i%1000000)) { - printf("s: %x %02X", usart->US_CSR, usart->US_RHR & 0xFF); + TRACE_ERROR("s: %x %02X\r\n", + usart->US_CSR, usart->US_RHR & 0xFF); usart->US_CR = US_CR_RSTTX; usart->US_CR = US_CR_RSTRX; } + i++; } usart->US_THR = byte; - TRACE_ERROR("Sx%02x\r\n", byte); + //TRACE_ERROR("Sx%02x\r\n", byte); #endif return 1; } @@ -460,8 +462,6 @@ static void dispatch_received_rctx(struct req_ctx *rctx, struct cardem_inst *ci) * one message. USB endpoints are streams that don't * preserve the message boundaries */ mh = (struct cardemu_usb_msg_hdr *) rctx->data; - TRACE_DEBUG("rctx->tot_len=%d, mh->msg_len=%d\r\n", - rctx->tot_len, mh->msg_len); if (mh->msg_len == rctx->tot_len) { /* fast path: only one message in buffer */ dispatch_usb_command(rctx, ci); @@ -473,8 +473,6 @@ static void dispatch_received_rctx(struct req_ctx *rctx, struct cardem_inst *ci) for (mh = (struct cardemu_usb_msg_hdr *) rctx->data; (uint8_t *)mh < rctx->data + rctx->tot_len; mh = (struct cardemu_usb_msg_hdr * ) ((uint8_t *)mh + mh->msg_len)) { - TRACE_DEBUG("Segment %d, offs=%d, len=%d\r\n", i, - (uint8_t *)mh - rctx->data, mh->msg_len); segm = req_ctx_find_get(0, RCTX_S_FREE, RCTX_S_MAIN_PROCESSING); if (!segm) { TRACE_ERROR("ENOMEM during rctx segmentation\r\n"); @@ -532,13 +530,13 @@ void mode_cardemu_run(void) uint8_t byte = rbuf_read(&ci->rb); __enable_irq(); card_emu_process_rx_byte(ci->ch, byte); - TRACE_ERROR("Rx%02x\r\n", byte); + //TRACE_ERROR("Rx%02x\r\n", byte); } queue = card_emu_get_usb_tx_queue(ci->ch); int usb_pending = llist_count(queue); if (usb_pending != ci->usb_pending_old) { -// printf("usb_pending=%d\r\n", usb_pending); + TRACE_DEBUG("usb_pending=%d\r\n", usb_pending); ci->usb_pending_old = usb_pending; } usb_refill_to_host(queue, ci->ep_in);